forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-deploy_web_static.py
executable file
·97 lines (83 loc) · 2.69 KB
/
3-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
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
97
#!/usr/bin/python3
"""
Fabric file
"""
from datetime import datetime
import os
from fabric.api import local, run, put, env
env.hosts = ["18.234.105.201", "100.26.162.114"]
env.user = "ubuntu"
env.key_filename = '/AirBnB_clone_v2/my_key'
def do_pack():
"""
method that generate a .tgz archive
"""
try:
if os.path.isdir("versions") is False:
local("mkdir versions")
print('versions is created')
date = datetime.now().strftime("%Y%m%d%H%M%S")
archive_name = f"versions/web_static_{date}.tgz"
local("tar -cvzf {} web_static".format(archive_name))
print('compression done')
print(archive_name)
return archive_name
except CommandFailed:
return None
def do_deploy(archive_path):
"""
method for deploy web static into servers
"""
if os.path.exists(archive_path) is False:
print("Warning no file to compress")
return False
# Get the name
new_path = archive_path.split('/')[-1]
# Get the name of the archive without extension
folder = new_path.split('.')[0]
# upload the archive
if put(archive_path, "/tmp/{}".
format(new_path)).failed is True:
return False
# Creating the right path
if run("rm -rf /data/web_static/releases/{}/".
format(folder)).failed is True:
return False
if run("mkdir -p /data/web_static/releases/{}/".
format(folder)).failed is True:
return False
# uncompress
if run("tar -xzf /tmp/{} -C /data/web_static/releases/{}/".
format(new_path, folder)).failed is True:
return False
# remove the archive
if run("rm /tmp/{}".format(new_path)).failed is True:
return False
# move the content of web_static from uncompress folder
if run("mv /data/web_static/releases/{}/web_static/*\
/data/web_static/releases/{}".
format(folder, folder)).failed is True:
return False
# delete the symbolic link
if run("rm -rf /data/web_static/current").failed is True:
return False
# Remove content of web_static from uncompress folder
if run("rm -rf /data/web_static/releases/{}/web_static".
format(folder)).failed is True:
return False
# create a new symoblic link
if run("ln -sf /data/web_static/releases/{} /data/web_static/current".
format(folder)).failed is True:
return False
print("New version deployed!")
return True
def deploy():
"""
creates and distributes an archive to your web servers,
using the function deploy
"""
path = do_pack()
if path is None:
return False
else:
return do_deploy(path)