forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-clean_web_static.py
executable file
·96 lines (71 loc) · 2.44 KB
/
100-clean_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3
"""
A Fabric script that generates a .tgz archive from the
contents of the web_static folder of AirBnB Clone repo, using
the function `do_pack`.
and does other stuffs.
"""
from fabric.api import local, env, run, put
from datetime import datetime
import os
env.hosts = ['52.87.153.4', '100.25.137.85']
env.user = 'ubuntu'
def do_pack():
"""Compress the contents of web_static"""
# create `versions` dir if not exists
local('mkdir -p versions')
# create compressed tgz file
time_stamp = datetime.now().strftime('%Y%m%d%H%M%S')
path = 'versions/web_static_' + time_stamp + '.tgz'
result = local('tar -cvzf {} web_static/'.format(path))
if result.succeeded:
return path
else:
return None
def do_deploy(archive_path):
"""distributes an archive to env.hosts web servers"""
# if empty argument passed
if not os.path.exists(archive_path):
return False
basename = os.path.basename(archive_path)
path = basename.replace('.tgz', '')
path = '/data/web_static/releases/{}'.format(path)
# upload archive to server
put(archive_path, '/tmp/')
run('mkdir -p {}'.format(path))
run('tar -xvzf /tmp/{} -C {}'.format(basename, path))
run('mv {}/web_static/* {}'.format(path, path))
run('rm -rf {}/web_static/'.format(path))
run('rm /data/web_static/current')
run('ln -s {} /data/web_static/current'.format(path))
return True
def deploy():
"""creates and distributes an archive to web servers"""
path = do_pack()
if path is None:
return False
return do_deploy(path)
def do_clean(number=0):
""" Deletes out-of-date archives."""
# check if path `versions/` exists
if not os.path.exists('versions/'):
return
# resolve least number of archives to keep
if number == 0:
number = 1
# capture list of archives : local
archives = local('ls -t versions/', capture=True)
archives = archives.split('\n')
archives = archives[int(number):]
# remove local archives
for archive in archives:
local('rm versions/{}'.format(archive))
# capture list of archives : remote
archives = run('ls -t /data/web_static/releases')
archives = archives.split('\n')
archives = archives[int(number):]
if 'test' in archives:
archives.remove('test')
for archive in archives:
run('rm -rf /data/web_static/releases/{}'.format(
archive))