forked from jarehec/AirBnB_clone_v3
-
Notifications
You must be signed in to change notification settings - Fork 45
/
3-deploy_web_static.py
executable file
·66 lines (59 loc) · 1.88 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
#!/usr/bin/python3
"""
Fabric script that creates and distributes an archive
on my web servers, using deploy function
"""
from fabric.api import *
from fabric.operations import run, put, sudo, local
from datetime import datetime
import os
env.hosts = ['66.70.184.249', '54.210.138.75']
created_path = None
def do_pack():
"""
generates a .tgz archine from contents of web_static
"""
time = datetime.utcnow().strftime('%Y%m%d%H%M%S')
file_name = "versions/web_static_{}.tgz".format(time)
try:
local("mkdir -p ./versions")
local("tar --create --verbose -z --file={} ./web_static"
.format(file_name))
return file_name
except:
return None
def do_deploy(archive_path):
"""
using fabric to distribute archive
"""
if os.path.isfile(archive_path) is False:
return False
try:
archive = archive_path.split("/")[-1]
path = "/data/web_static/releases"
put("{}".format(archive_path), "/tmp/{}".format(archive))
folder = archive.split(".")
run("mkdir -p {}/{}/".format(path, folder[0]))
new_archive = '.'.join(folder)
run("tar -xzf /tmp/{} -C {}/{}/"
.format(new_archive, path, folder[0]))
run("rm /tmp/{}".format(archive))
run("mv {}/{}/web_static/* {}/{}/"
.format(path, folder[0], path, folder[0]))
run("rm -rf {}/{}/web_static".format(path, folder[0]))
run("rm -rf /data/web_static/current")
run("ln -sf {}/{} /data/web_static/current"
.format(path, folder[0]))
return True
except:
return False
def deploy():
"""
deploy function that creates/distributes an archive
"""
global created_path
if created_path is None:
created_path = do_pack()
if created_path is None:
return False
return do_deploy(created_path)