forked from deanbirnie/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-deploy_web_static.py
executable file
·63 lines (51 loc) · 1.63 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
#!/usr/bin/python3
""" Function that deploys """
from datetime import datetime
from fabric.api import *
import os
import shlex
env.hosts = ['35.231.33.237', '34.74.155.163']
env.user = "ubuntu"
def deploy():
""" DEPLOYS """
try:
archive_path = do_pack()
except:
return False
return do_deploy(archive_path)
def do_pack():
try:
if not os.path.exists("versions"):
local('mkdir versions')
t = datetime.now()
f = "%Y%m%d%H%M%S"
archive_path = 'versions/web_static_{}.tgz'.format(t.strftime(f))
local('tar -cvzf {} web_static'.format(archive_path))
return archive_path
except:
return None
def do_deploy(archive_path):
""" Deploys """
if not os.path.exists(archive_path):
return False
try:
name = archive_path.replace('/', ' ')
name = shlex.split(name)
name = name[-1]
wname = name.replace('.', ' ')
wname = shlex.split(wname)
wname = wname[0]
releases_path = "/data/web_static/releases/{}/".format(wname)
tmp_path = "/tmp/{}".format(name)
put(archive_path, "/tmp/")
run("mkdir -p {}".format(releases_path))
run("tar -xzf {} -C {}".format(tmp_path, releases_path))
run("rm {}".format(tmp_path))
run("mv {}web_static/* {}".format(releases_path, releases_path))
run("rm -rf {}web_static".format(releases_path))
run("rm -rf /data/web_static/current")
run("ln -s {} /data/web_static/current".format(releases_path))
print("New version deployed!")
return True
except:
return False