-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-deploy_web_static.py
61 lines (54 loc) · 1.82 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
#!/usr/bin/python3
""" module doc
"""
from fabric.api import task, local, env, put, run, runs_once
from datetime import datetime
import os
env.hosts = ['100.25.15.47', '52.91.121.153']
@runs_once
def do_pack():
""" method doc
sudo fab -f 1-pack_web_static.py do_pack
"""
formatted_dt = datetime.now().strftime('%Y%m%d%H%M%S')
mkdir = "mkdir -p versions"
path = "versions/web_static_{}.tgz".format(formatted_dt)
print("Packing web_static to {}".format(path))
if local("{} && tar -cvzf {} web_static".format(mkdir, path)).succeeded:
return path
return None
@task
def do_deploy(archive_path):
""" method doc
fab -f 2-do_deploy_web_static.py do_deploy:
archive_path=versions/web_static_20231004201306.tgz
-i ~/.ssh/id_rsa -u ubuntu
"""
try:
if not os.path.exists(archive_path):
return False
fn_with_ext = os.path.basename(archive_path)
fn_no_ext, ext = os.path.splitext(fn_with_ext)
dpath = "/data/web_static/releases/"
put(archive_path, "/tmp/")
run("rm -rf {}{}/".format(dpath, fn_no_ext))
run("mkdir -p {}{}/".format(dpath, fn_no_ext))
run("tar -xzf /tmp/{} -C {}{}/".format(fn_with_ext, dpath, fn_no_ext))
run("rm /tmp/{}".format(fn_with_ext))
run("mv {0}{1}/web_static/* {0}{1}/".format(dpath, fn_no_ext))
run("rm -rf {}{}/web_static".format(dpath, fn_no_ext))
run("rm -rf /data/web_static/current")
run("ln -s {}{}/ /data/web_static/current".format(dpath, fn_no_ext))
print("New version deployed!")
return True
except Exception:
return False
@task
def deploy():
""" method doc
sudo fab -f 1-pack_web_static.py do_pack
"""
path = do_pack()
if path is None:
return False
return do_deploy(path)