forked from Wyrd00/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-deploy_web_static.py
78 lines (64 loc) · 2.08 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
77
78
#!/usr/bin/python3
"""
Fabric script that distributes an archive to your web servers
"""
import os
from fabric.api import local, run, env, put
from datetime import datetime
import logging
logger = logging.getLogger('ftpuploader')
env.hosts = ['35.237.237.122', '35.237.87.28']
env.user = "ubuntu"
env.key_filename = "~/.ssh/holberton"
def do_pack():
"""
Generates a .tgz archive from the contents of the web_static
Archive:
web_static_<year><month><day><hour><minute><second>.tgz
Return:
Return the archive path if the archive has been correctly generated
Else, return None
"""
from fabric.api import local
from datetime import datetime
time = datetime.now().strftime("%Y%m%d%H%M%S")
local("mkdir -p versions")
name = "versions/web_static_{}.tgz".format(time)
tar_file = local("tar -cvzf {} web_static".format(name))
if tar_file.succeeded:
return name
else:
return None
def do_deploy(archive_path):
"""
Distributes an archive to your web servers
Return:
False if the file at the path archive_path doesn’t exist
"""
fp = archive_path.split('/')[1]
try:
put(archive_path, '/tmp/{}'.format(fp))
run('mkdir -p /data/web_static/releases/{}'.format(fp))
run('tar -xzf /tmp/{} -C /data/web_static/releases/{}'.format(fp, fp))
run('rm /tmp/{}'.format(fp))
run('mv -f /data/web_static/releases/{}/web_static/* \
/data/web_static/releases/{}/'.format(fp, fp))
run('rm -rf /data/web_static/releases/{}/web_static'.format(fp))
run('rm -rf /data/web_static/current')
run('ln -s /data/web_static/releases/{} \
/data/web_static/current'.format(fp))
print('New version deployed!')
return True
except Exception as e:
# Checks for errors
logger.error(str(e))
print('New version has not been deployed...')
return False
def deploy():
"""
Automates the deployment of archives
"""
dws = do_pack()
if dws is None:
return False
return do_deploy(dws)