forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-deploy_web_static.py
executable file
·54 lines (44 loc) · 1.68 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
#!/usr/bin/python3
"""Deploy a new version of the web_static code"""
from fabric.api import local, run, env
from datetime import datetime # Add this line to import datetime module
import os
env.hosts = ['54.162.37.159', '18.234.169.238']
env.user = 'ubuntu'
def do_pack():
"""Create a tar gzipped archive of the web_static folder"""
time_now = datetime.now().strftime("%Y%m%d%H%M%S")
file_name = "versions/web_static_" + time_now + ".tgz"
local("mkdir -p versions")
result = local("tar -cvzf {} web_static".format(file_name))
if result.failed:
return None
return file_name
def do_deploy(archive_path):
"""Deploy the archive to the servers"""
if not os.path.exists(archive_path):
return False
try:
file_name = archive_path.split("/")[-1]
folder_name = file_name.split(".")[0]
put(archive_path, "/tmp/")
run("mkdir -p /data/web_static/releases/{}/".format(folder_name))
run("tar -xzf /tmp/{} -C /data/web_static/releases/{}/"
.format(file_name, folder_name))
run("rm /tmp/{}".format(file_name))
run("mv /data/web_static/releases/{}/web_static/* "
"/data/web_static/releases/{}/".format(folder_name, folder_name))
run("rm -rf /data/web_static/releases/{}/web_static"
.format(folder_name))
run("rm -rf /data/web_static/current")
run("ln -s /data/web_static/releases/{}/ "
"/data/web_static/current".format(folder_name))
return True
except:
return False
def deploy():
"""Deploy the archive"""
archive_path = do_pack()
if archive_path is None:
return False
return do_deploy(archive_path)