forked from ioos/service-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
129 lines (103 loc) · 3.68 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fabric.api import *
from fabric.contrib.files import *
import os
from copy import copy
import time
import urlparse
import shutil
"""
Call this with fab -c .fab TASK to pick up deploy variables
Required variables in .fab file:
mail_server = x
mail_port = x
mail_username = x
mail_password = x
mail_default_sender = x
mailer_debug = x
mail_default_to = x
mail_default_list = x
webpass = x
secret_key = x
mongo_db = x
redis_db = x
"""
env.user = "monitoring"
code_dir = "/home/monitoring/ioos-service-monitor"
env.hosts = ['198.199.75.160']
def admin():
env.user = "dfoster"
def monitoring():
env.user = "monitoring"
def deploy():
stop_supervisord()
monitoring()
with cd(code_dir):
run("git pull origin master")
update_supervisord()
update_libs()
update_crontab()
#create_index()
start_supervisord()
run("supervisorctl -c ~/supervisord.conf start all")
def update_supervisord():
monitoring()
run("pip install supervisor")
upload_template('deploy/supervisord.conf', '/home/monitoring/supervisord.conf', context=copy(env), use_jinja=True, use_sudo=False, backup=False, mirror_local_mode=True)
def update_libs(paegan=None):
monitoring()
with cd(code_dir):
with settings(warn_only=True):
run("pip install -r requirements.txt")
def update_crontab():
# .env
upload_template('deploy/dotenv', '/home/monitoring/.env', context=copy(env), use_jinja=True, use_sudo=False, backup=False, mirror_local_mode=True)
# manage.sh
put('deploy/manage.sh', '/home/monitoring/manage.sh')
# crontab
put('deploy/catalog_crontab.txt', '/home/monitoring/crontab.txt')
run("crontab %s" % "/home/monitoring/crontab.txt")
def restart_nginx():
admin()
sudo("/etc/init.d/nginx restart")
def supervisord_restart():
stop_supervisord()
start_supervisord()
def stop_supervisord():
monitoring()
with cd(code_dir):
with settings(warn_only=True):
run("supervisorctl -c ~/supervisord.conf stop all")
run("kill -QUIT $(ps aux | grep supervisord | grep monitoring | grep -v grep | awk '{print $2}')")
kill_pythons()
def kill_pythons():
admin()
with settings(warn_only=True):
sudo("kill -QUIT $(ps aux | grep python | grep monitoring | grep -v supervisord | awk '{print $2}')")
def start_supervisord():
monitoring()
with cd(code_dir):
with settings(warn_only=True):
run("supervisord -c ~/supervisord.conf")
def create_index():
MONGO_URI = env.get('mongo_db')
url = urlparse.urlparse(MONGO_URI)
MONGODB_DATABASE = url.path[1:]
# @TODO: this will likely error on first run as the collection won't exist
run('mongo "%s" --eval "db.getCollection(\'stats\').ensureIndex({\'created\':-1})"' % MONGODB_DATABASE)
run('mongo "%s" --eval "db.getCollection(\'metadatas\').ensureIndex({\'ref_id\':1, \'ref_type\':1})"' % MONGODB_DATABASE)
def db_snapshot():
admin()
MONGO_URI = env.get('mongo_db')
url = urlparse.urlparse(MONGO_URI)
MONGODB_DATABASE = url.path[1:]
backup_name = time.strftime('%Y-%m-%d')
tmp = run('mktemp -d').strip()
with cd(tmp):
run('mongodump -d %s -o %s' % (MONGODB_DATABASE, backup_name))
run('tar cfvz %s.tar.gz %s' % (backup_name, backup_name))
get(remote_path="%s.tar.gz" % backup_name, local_path='db/%(path)s')
run('rm -r %s' % tmp)
# local restore
with lcd('db'):
local('tar xfvz %s.tar.gz' % backup_name)
local('mongorestore -d catalog-%s %s' % (backup_name, os.path.join(backup_name, MONGODB_DATABASE)))