forked from ElkinAMG/AirBnB_clone_v2
/
3-deploy_web_static.py
executable file
·57 lines (45 loc) · 5.26 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
#!/usr/bin/python3
"""Distributes an archive to your web servers, using the function do_deploy"""
from fabric.contrib import files
from fabric.api import env, put, run, local
import time
import os
env.hosts = ['34.73.222.49', '54.82.121.70']
def do_pack():
"""Gerenate tgz."""
timestamp = time.strftime("%Y%m%d%H%M%S")
try:
local("mkdir -p versions")
local("tar -cvzf versions/web_static_{:s}.tgz web_static/".
format(timestamp))
return ("versions/web_static_{:s}.tgz".format(timestamp))
except:
return None
def do_deploy(archive_path):
"""Function for deploy."""
if not os.path.exists(archive_path):
return False
data_path = '/data/web_static/releases/'
tmp = archive_path.split('.')[0]
name = tmp.split('/')[1]
dest = data_path + name
try:
put(archive_path, '/tmp')
run('mkdir -p {}'.format(dest))
run('tar -xzf /tmp/{}.tgz -C {}'.format(name, dest))
run('rm -f /tmp/{}.tgz'.format(name))
run('mv {}/web_static/* {}/'.format(dest, dest))
run('rm -rf {}/web_static'.format(dest))
run('rm -rf /data/web_static/current'.format(name))
run('ln -s {} /data/web_static/current'.format(dest))
return True
except:
return False
def deploy():
"""Compress and upload files to remote server."""
path = do_pack()
print(path)
if path is None:
return False
return do_deploy(path)
deploy()