forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
100-clean_web_static.py
executable file
·92 lines (82 loc) · 2.74 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
#!/usr/bin/python3
"""Generate a .tgz archive, distribute it and clean"""
from fabric.operations import local, run, put
from fabric.api import env, runs_once
from datetime import datetime
from os.path import exists, basename, getsize
from os import listdir
env.hosts = ['18.212.205.195', '50.17.11.48']
@runs_once
def do_pack():
"""Generate the .tgz archive"""
now = datetime.now()
file = 'web_static_{}{:02d}{:02d}{:02d}{:02d}{:02d}.tgz'\
.format(now.year, now.month, now.day, now.hour, now.minute, now.second)
local('mkdir -p versions')
output = local('tar -cvzf versions/{} web_static'.format(file))
if output.succeeded:
print('web_static packed: versions/{} -> {}Bytes'
.format(file, getsize('versions/{}'.format(file))))
return 'versions/{}'.format(file)
else:
return None
def do_deploy(archive_path):
"""distribute the archive to the web servers"""
if not exists(archive_path):
return False
archive = basename(archive_path)
dir = archive.split('.')[0]
res = put(archive_path, '/tmp/')
if not res.succeeded:
return False
res = run('mkdir -p /data/web_static/releases/{}/'.format(dir))
if not res.succeeded:
return False
res = run('tar -xzf /tmp/{} -C /data/web_static/releases/{}/'
.format(archive, dir))
if not res.succeeded:
return False
res = run('rm /tmp/{}'.format(archive))
if not res.succeeded:
return False
res = run('mv /data/web_static/releases/{}/web_static/* '.format(dir) +
'/data/web_static/releases/{}/'.format(dir))
if not res.succeeded:
return False
res = run('rm -rf /data/web_static/releases/{}/web_static'.format(dir))
if not res.succeeded:
return False
res = run('rm -rf /data/web_static/current')
if not res.succeeded:
return False
res = run('ln -s /data/web_static/releases/{}/ /data/web_static/current'
.format(dir))
if not res.succeeded:
return False
print('New version deployed!')
return True
def deploy():
"""generate the archive and deploy it"""
path = do_pack()
if path is None:
return False
return do_deploy(path)
def do_clean(number=0):
"""Do some cleaning"""
i = 0
nb = len(listdir('versions/'))
nbr = int(number)
if nbr == 0:
nbr = 1
files = local('ls -1tr versions/', capture=True).split('\n')
while nb > nbr:
local('rm -f versions/{}'.format(files[i]))
i += 1
nb -= 1
i = 0
dir = run('ls -1tr /data/web_static/releases/').split('\n')
nb_dir = len(dir)
while nb_dir > nbr:
run('rm -rf /data/web_static/releases/{}'.format(dir[i][:-1]))
i += 1
nb_dir -= 1