-
Notifications
You must be signed in to change notification settings - Fork 108
/
2-do_deploy_web_static.py
executable file
·43 lines (36 loc) · 1.41 KB
/
2-do_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
#!/usr/bin/python3
"""Deploy an archive of static html to my web servers with Fabric3"""
from fabric import api
from fabric.contrib import files
import os
api.env.hosts = ['142.44.167.235', '144.217.246.199']
api.env.user = 'ubuntu'
api.env.key_filename = '~/.ssh/holberton'
def do_deploy(archive_path):
"""Function to transfer `archive_path` to web servers.
Args:
archive_path (str): path of the .tgz file to transfer
Returns: True on success, False otherwise.
"""
if not os.path.isfile(archive_path):
return False
with api.cd('/tmp'):
basename = os.path.basename(archive_path)
root, ext = os.path.splitext(basename)
outpath = '/data/web_static/releases/{}'.format(root)
try:
putpath = api.put(archive_path)
if files.exists(outpath):
api.run('rm -rdf {}'.format(outpath))
api.run('mkdir -p {}'.format(outpath))
api.run('tar -xzf {} -C {}'.format(putpath[0], outpath))
api.run('rm -f {}'.format(putpath[0]))
api.run('mv -u {}/web_static/* {}'.format(outpath, outpath))
api.run('rm -rf {}/web_static'.format(outpath))
api.run('rm -rf /data/web_static/current')
api.run('ln -sf {} /data/web_static/current'.format(outpath))
print('New version deployed!')
except:
return False
else:
return True