-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-clean_web_static.py
91 lines (64 loc) · 2.25 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
#!/usr/bin/python3
from datetime import datetime
from fabric.api import *
from os import path
env.hosts = ['35.229.93.37', '54.196.213.127']
@runs_once
def do_pack():
"""Generates a .tgz archive from the contents
of the web_static folder of this repository.
"""
d = datetime.now()
now = d.strftime('%Y%m%d%H%M%S')
path = "versions/web_static_{}.tgz".format(now)
local("mkdir -p versions")
local("tar -czvf {} web_static".format(path))
return path
def do_deploy(archive_path):
"""Distributes a .tgz archive through web servers
"""
if path.exists(archive_path):
archive = archive_path.split('/')[1]
a_path = "/tmp/{}".format(archive)
folder = archive.split('.')[0]
f_path = "/data/web_static/releases/{}/".format(folder)
put(archive_path, a_path)
run("mkdir -p {}".format(f_path))
run("tar -xzf {} -C {}".format(a_path, f_path))
run("rm {}".format(a_path))
run("mv -f {}web_static/* {}".format(f_path, f_path))
run("rm -rf {}web_static".format(f_path))
run("rm -rf /data/web_static/current")
run("ln -s {} /data/web_static/current".format(f_path))
return True
return False
def gets_out_of_date(number, _type):
"""Gets a number of content that is out of date
"""
if number == 0:
number = 1
if _type == 'local':
content = local("ls -td web_static_*", capture=True)
elif _type == 'remote':
content = run("ls -td web_static_*")
content_list = content.split()
out_of_date = content_list[number:]
return out_of_date
def do_clean(number=0):
"""Deletes out-of-date .tgz archives of web servers
"""
number = int(number)
if number >= 0:
with lcd("versions"):
_files = gets_out_of_date(number, 'local')
for _file in _files:
local("rm -f {file}".format(file=_file))
with cd("/data/web_static/releases"):
_folders = gets_out_of_date(number, 'remote')
for _folder in _folders:
run("rm -rf {folder}".format(folder=_folder))
def deploy():
"""Creates and Distributes a .tgz archive through web servers
"""
archive = do_pack()
return do_deploy(archive)