forked from mfcrespo/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-clean_web_static.py
executable file
·89 lines (81 loc) · 2.72 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
#!/usr/bin/python3
"""
Fab module to create a targ file of all my static files
"""
from fabric.api import local, run, env, put, sudo
from datetime import datetime
from os import path
from io import StringIO
env.hosts = ['35.196.49.136', '107.23.126.192']
def do_pack():
"""
function to pack in .tgz
"""
local("mkdir -p versions")
now = datetime.today()
try:
file_name = "web_static_{}{}{}{}{}{}.tgz".format(now.year, now.month,
now.day, now.hour,
now.minute,
now.second)
local("tar -cvzf versions/{} web_static".format(file_name))
return (file_name)
except:
return (None)
def do_deploy(archive_path):
"""
logic to deploy into ssh servers
"""
if not path.isfile(archive_path):
return False
try:
put(archive_path, "/tmp/")
directory_path = archive_path.split(".")[0]
directory_path = directory_path.split("/")[-1]
archive_path = archive_path.split("/")[-1]
sudo("mkdir -p /data/web_static/releases/{}/".format(directory_path))
full_path = "/data/web_static/releases/{}".format(directory_path)
sudo("tar -xvzf /tmp/{} -C {}".format(archive_path, full_path))
sudo("rm -rf /tmp/{}".format(archive_path))
sudo("mv -f {}/web_static/* {}".format(full_path, full_path))
sudo("rm -rf /data/web_static/current")
sudo("ln -sf {} /data/web_static/current".format(full_path))
return(True)
except:
return(False)
def deploy():
"""
automatizate creation and deploy
"""
file_name = do_pack()
if (not file_name):
return False
worked = do_deploy("versions/{}".format(file_name))
return worked
def do_clean(number=0):
"""
Cleans old versions from folder
"""
fh = StringIO()
my_names = []
number = int(number)
if (number == 0):
number = 1
revl = "ls -ltr versions | rev | cut -d ' ' -f1 | rev"
value = local(revl, capture=True)
for line in value.splitlines():
my_names.append(line)
my_names.pop(0)
for i in range(len(my_names) - number):
local("rm -rf versions/{}".format(my_names[i]))
# For remote server
code = "ls -ltr /data/web_static/releases | rev | cut -d ' ' -f1 | rev"
fk_value = sudo(code, stdout=fh)
fh.seek(0)
my_fk_names = []
for line in fh.readlines():
data = line.split()[-1]
if data.startswith("web_static"):
my_fk_names.append(data)
for i in range(len(my_fk_names) - number):
sudo("rm -rf /data/web_static/releases/{}".format(my_fk_names[i]))