-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3-deploy_web_static.py
56 lines (48 loc) · 1.62 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
#!/usr/bin/python3
"""Module to create a tar pack with web static files to deploy"""
from fabric.api import *
from datetime import datetime
import os
env.hosts = ['35.237.183.49', '52.201.229.129']
env.user = 'ubuntu'
def do_pack():
"""Create pack"""
try:
if not os.path.exists("versions"):
local("mkdir versions")
date = datetime.now()
date_format = date.strftime("%Y%m%d%H%M%S")
filename = "versions/web_static_" + date_format + ".tgz"
local("tar -czvf {} web_static".format(filename))
return filename
except:
return None
def do_deploy(archive_path):
"""Distributes an archive to the web servers"""
if not os.path.exists(archive_path):
return False
try:
name_ext = archive_path.split('/')
name_ext = name_ext[-1]
name = name_ext.split('.')
name = name[0]
path = "/data/web_static/releases/"
put(archive_path, "/tmp/")
run("mkdir -p {}{}/".format(path, name))
run("tar -xzf /tmp/{} -C {}{}/".format(name_ext, path, name))
run("rm /tmp/{}".format(name_ext))
run("mv {}{}/web_static/* {}{}/".format(path, name, path, name))
run("rm -rf {}{}/web_static".format(path, name))
run("rm -rf /data/web_static/current")
run("ln -s {}{}/ /data/web_static/current".format(path, name))
print("New version deployed!")
return (True)
except:
return (False)
def deploy():
""" combines do_deploy and do_pack functions """
path_file = do_pack()
if path_file:
return (do_deploy(path_file))
else:
return False