This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
114 lines (92 loc) · 2.76 KB
/
fabfile.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from fabric.api import env
from fabric.decorators import task
from fabric.context_managers import cd, prefix, warn_only
from fabric.operations import run, local, get, put, prompt, sudo
from fabric.colors import red, green, blue, yellow, white, magenta, cyan
from contextlib import contextmanager as _contextmanager
from app.utils.standard import StandardODM
import yaml
import arrow
config = None
with open("fabconfig.yaml", "r") as f:
config = StandardODM(**yaml.load(f.read()))
if not config:
raise Exception("Failed to load fabconfig.yaml!")
env.key_name = config.ssh_key
env.hosts = config.hosts
env.path = config.path
env.user = "josh"
@_contextmanager
def virtualenv(virt=config.virtualenv_name):
with cd(env.path):
with prefix("source {}/virt/{}/bin/activate".format(env.path, virt)):
yield
def get_service(action):
ans = prompt(cyan("What services do you want to {}? (Case sensitive, space separated or `all` or `none`)".format(action)))
if ans and not ans.lower() == "none":
return ans
return None
def services(action, ans):
actions = {
"restart": "Restarting",
"start": "Starting",
"stop": "Stopping"
}
print cyan("{} services [{}]...".format(actions[action], ans))
with cd(env.path), virtualenv():
with cd("app/"):
run("supervisorctl {} {}".format(action, ans))
def git_pull():
print cyan("Updating git repository...")
with cd(env.path):
run("git pull origin master")
@task
def static():
git_pull()
print cyan("Rebuilding static files...")
with cd(env.path):
run("grunt")
run("cp -r interface/build/* {}/static/".format(config.html))
@task
def status():
with cd(env.path), virtualenv():
with cd("app/"):
run("supervisorctl status")
@task
def restart():
status()
ans = get_service("restart")
if ans is not None:
services("restart", ans)
@task
def start():
status()
ans = get_service("start")
if ans is not None:
services("start", ans)
@task
def stop():
status()
ans = get_service("stop")
if ans is not None:
services("stop", ans)
@task
def backup():
time = arrow.utcnow().format("YYYY-MM-DD_HH-mm-ss")
db_backup = "transientbug_rethinkdb_backup_{}.tar.gz".format(time)
images_backup = "transientbug_static_backup_{}.tar.gz".format(time)
with cd(env.path), virtualenv():
sudo("rethinkdb dump -f {}".format(db_backup))
run("tar -czf {} {}".format(images_backup, config.html))
get(db_backup)
get(images_backup)
@task
def reindex():
with cd(env.path), virtualenv():
with cd("app"):
run("python reindex.py")
@task
def deploy():
git_pull()
static()
services("restart", "all")