-
Notifications
You must be signed in to change notification settings - Fork 2
/
3-deploy_web_static.py
executable file
·76 lines (68 loc) · 2.06 KB
/
3-deploy_web_static.py
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
68
69
70
71
72
73
74
75
76
#!/usr/bin/python3
"""
Creates and distributes an archive to your web servers
"""
import os
from datetime import datetime
from fabric.api import *
env.user = "ubuntu"
env.hosts = ["54.144.46.157", "54.167.85.19"]
def do_pack():
"""Compress files from web_static directory"""
try:
if not os.path.isdir("versions"):
os.makedirs("versions")
date = datetime.now()
file = "versions/web_static_{0}{1}{2}{3}{4}{5}".format(
date.year,
date.month,
date.day,
date.hour,
date.minute,
date.second
)
file += ".tgz"
local("tar -cvzf {} web_static".format(file))
return file
except Exception:
return None
def do_deploy(archive_path):
"""
Deploy archive
Args:
- archive_path(str, optional): Path of the archive
"""
try:
if not os.path.isfile(archive_path):
return False
path = archive_path.split("/")[1]
name = path.split(".")[0]
put(archive_path, "/tmp/{0}".format(path))
run("sudo mkdir -p /data/web_static/releases/{}/".format(name))
source = "sudo tar -xzf /tmp/{0} -C".format(path)
dest = "/data/web_static/releases/{0}/".format(name)
run(source + " " + dest)
run("sudo rm /tmp/{0}".format(path))
source = (
"sudo mv /data/web_static/releases/{0}/web_static/*".format(name)
)
dest = "/data/web_static/releases/{0}/".format(name)
run(source + " " + dest)
run(
"sudo rm -rf /data/web_static/releases/{0}/web_static".format(name)
)
run("sudo rm -rf /data/web_static/current")
source = "sudo ln -s /data/web_static/releases/{0}/".format(name)
dest = "/data/web_static/current"
run(source + " " + dest)
return True
except Exception:
return False
def deploy():
"""
Full deployment based on do_pack & do_deploy (task 2)
"""
path = do_pack()
if path is None:
return False
return do_deploy(path)