forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3-deploy_web_static.py
More file actions
executable file
·57 lines (49 loc) · 1.91 KB
/
Copy path3-deploy_web_static.py
File metadata and controls
executable file
·57 lines (49 loc) · 1.91 KB
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
#!/usr/bin/python3
"""
Fabric script (based on the file 2-do_deploy_web_static.py) that creates and
distributes an archive to your web servers, using the function deploy
"""
from fabric.api import env
from os.path import exists
from fabric.api import local
from datetime import datetime
from fabric.api import put, run
env.hosts = ['54.161.251.141', '54.237.108.159']
def do_pack():
"""Generates a .tgz archive from the contents of the web_static"""
date = datetime.now().strftime("%Y%m%d%H%M%S")
local("mkdir -p versions")
result = local("tar -cvzf versions/web_static_{}.tgz web_static".
format(date))
if result.failed:
return None
return "versions/web_static_{}.tgz".format(date)
def do_deploy(archive_path):
"""Distributes an archive to your web servers"""
if exists(archive_path):
try:
put(archive_path, "/tmp/")
file_name = archive_path.split("/")[-1]
no_ext = file_name.split(".")[0]
run("mkdir -p /data/web_static/releases/{}/".format(no_ext))
run("tar -xzf /tmp/{} -C /data/web_static/releases/{}/".
format(file_name, no_ext))
run("rm /tmp/{}".format(file_name))
run("mv /data/web_static/releases/{}/web_static/* \
/data/web_static/releases/{}/".format(no_ext, no_ext))
run("rm -rf /data/web_static/releases/{}/web_static".
format(no_ext))
run("rm -rf /data/web_static/current")
run("ln -s /data/web_static/releases/{}/ /data/web_static/current".
format(no_ext))
return True
except Exception as e:
print("Exception occurred: {}".format(e))
return False
return False
def deploy():
"""Creates and distributes an archive to your web servers"""
path = do_pack()
if path is None:
return False
return do_deploy(path)