-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy path3-deploy_web_static.py
More file actions
executable file
·67 lines (61 loc) · 1.85 KB
/
3-deploy_web_static.py
File metadata and controls
executable file
·67 lines (61 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python3
"""pack and deploy content to server
"""
from fabric.api import local, env, run, put
from datetime import datetime
import os
env.hosts = ['35.231.156.161', '34.73.64.44']
env.user = 'ubuntu'
def do_pack():
"""pack all content within web_static
into a .tgz archive
The archive will be put in versions/
"""
if not os.path.exists("versions"):
local("mkdir versions")
now = datetime.now()
name = "versions/web_static_{}.tgz".format(
now.strftime("%Y%m%d%H%M%S")
)
cmd = "tar -cvzf {} {}".format(name, "web_static")
result = local(cmd)
if not result.failed:
return name
def do_deploy(archive_path):
"""deploy package to remote server
Arguments:
archive_path: path to archive to deploy
"""
if not archive_path or not os.path.exists(archive_path):
return False
put(archive_path, '/tmp')
ar_name = archive_path[archive_path.find("/") + 1: -4]
try:
run('mkdir -p /data/web_static/releases/{}/'.format(ar_name))
run('tar -xzf /tmp/{}.tgz -C /data/web_static/releases/{}/'.format(
ar_name, ar_name
))
run('rm /tmp/{}.tgz'.format(ar_name))
run('mv /data/web_static/releases/{}/web_static/* \
/data/web_static/releases/{}/'.format(
ar_name, ar_name
))
run('rm -rf /data/web_static/releases/{}/web_static'.format(
ar_name
))
run('rm -rf /data/web_static/current')
run('ln -s /data/web_static/releases/{}/ \
/data/web_static/current'.format(
ar_name
))
print("New version deployed!")
return True
except:
return False
def deploy():
"""pack web_static content and deploy it to web servers
"""
pack = do_pack()
if pack:
return do_deploy(pack)
return False