forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-deploy_web_static.py
executable file
·53 lines (46 loc) · 1.67 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
#!/usr/bin/python3
"""
Fabric script based on the file 2-do_deploy_web_static.py that creates and
distributes an archive to the web servers
execute: fab -f 3-deploy_web_static.py deploy -i ~/.ssh/id_rsa -u ubuntu
"""
from fabric.api import env, local, put, run
from datetime import datetime
from os.path import exists, isdir
env.hosts = ['52.91.124.43', '54.237.52.91']
def do_pack():
"""generates a tgz archive"""
try:
date = datetime.now().strftime("%Y%m%d%H%M%S")
if isdir("versions") is False:
local("mkdir versions")
file_name = "versions/web_static_{}.tgz".format(date)
local("tar -cvzf {} web_static".format(file_name))
return file_name
except:
return None
def do_deploy(archive_path):
"""distributes an archive to the web servers"""
if exists(archive_path) is False:
return False
try:
file_n = archive_path.split("/")[-1]
no_ext = file_n.split(".")[0]
path = "/data/web_static/releases/"
put(archive_path, '/tmp/')
run('mkdir -p {}{}/'.format(path, no_ext))
run('tar -xzf /tmp/{} -C {}{}/'.format(file_n, path, no_ext))
run('rm /tmp/{}'.format(file_n))
run('mv {0}{1}/web_static/* {0}{1}/'.format(path, no_ext))
run('rm -rf {}{}/web_static'.format(path, no_ext))
run('rm -rf /data/web_static/current')
run('ln -s {}{}/ /data/web_static/current'.format(path, no_ext))
return True
except:
return False
def deploy():
"""creates and distributes an archive to the web servers"""
archive_path = do_pack()
if archive_path is None:
return False
return do_deploy(archive_path)