forked from oltpbenchmark/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
175 lines (136 loc) · 4.66 KB
/
fabfile.py
File metadata and controls
175 lines (136 loc) · 4.66 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'''
Admin tasks
@author: dvanaken
'''
import os.path
from collections import namedtuple
from fabric.api import env, execute, local, quiet, settings, sudo, task
from fabric.state import output as fabric_output
from website.settings import PRELOAD_DIR, PROJECT_ROOT
# Fabric environment settings
env.hosts = ['localhost']
fabric_output.update({
'running': False,
'stdout': True,
})
Status = namedtuple('Status', ['RUNNING', 'STOPPED'])
STATUS = Status(0, 1)
if local('hostname', capture=True).strip() == 'ottertune':
PREFIX = 'sudo -u celery '
SUPERVISOR_CONFIG = '-c config/prod_supervisord.conf'
else:
PREFIX = ''
SUPERVISOR_CONFIG = '-c config/supervisord.conf'
# Setup and base commands
SUPERVISOR_CMD = (PREFIX + 'supervisorctl ' + SUPERVISOR_CONFIG +
' {action} celeryd').format
RABBITMQ_CMD = 'sudo rabbitmqctl {action}'.format
# Make sure supervisor is initialized
with settings(warn_only=True), quiet():
local(PREFIX + 'supervisord ' + SUPERVISOR_CONFIG)
@task
def start_rabbitmq(detached=True):
detached = parse_bool(detached)
cmd = 'sudo rabbitmq-server' + (' -detached' if detached else '')
local(cmd)
@task
def stop_rabbitmq():
with settings(warn_only=True):
local(RABBITMQ_CMD(action='stop'))
@task
def status_rabbitmq():
with settings(warn_only=True), quiet():
res = local(RABBITMQ_CMD(action='status'), capture=True)
if res.return_code == 2 or res.return_code == 69:
status = STATUS.STOPPED
elif res.return_code == 0:
status = STATUS.RUNNING
else:
raise Exception("Rabbitmq: unknown status " + str(res.return_code))
print status
print_status(status, 'rabbitmq')
return status
@task
def start_celery(detached=True):
if status_rabbitmq() == STATUS.STOPPED:
start_rabbitmq()
detached = parse_bool(detached)
if detached:
local(SUPERVISOR_CMD(action='start'))
else:
local(PREFIX + 'python manage.py celery worker -l info')
@task
def stop_celery():
local(SUPERVISOR_CMD(action='stop'))
@task
def status_celery():
res = local(SUPERVISOR_CMD(action='status') +
' | tr -s \' \' | cut -d \' \' -f2', capture=True)
try:
status = STATUS._asdict()[res.stdout]
except KeyError as e:
if res.stdout == 'STARTING':
status = STATUS.RUNNING
elif res.stdout == 'FATAL':
status = STATUS.STOPPED
else:
raise e
print_status(status, 'celery')
return status
@task
def start_debug_server():
if status_celery() == STATUS.STOPPED:
start_celery()
local('python manage.py runserver 0.0.0.0:8000')
@task
def stop_all():
stop_celery()
stop_rabbitmq()
def parse_bool(value):
if isinstance(value, bool):
return value
elif isinstance(value, str):
return value.lower() == 'true'
else:
raise Exception('Cannot convert {} to bool'.format(type(value)))
def print_status(status, task_name):
print "{} status: {}".format(
task_name,
STATUS._fields[STATUS.index(status)])
@task
def recreate_website_dbms():
from website.settings import DATABASES
user = DATABASES['default']['USER']
passwd = DATABASES['default']['PASSWORD']
name = DATABASES['default']['NAME']
local("mysql -u {} -p{} -N -B -e \"DROP DATABASE IF EXISTS {}\"".format(
user, passwd, name))
local("mysql -u {} -p{} -N -B -e \"CREATE DATABASE {}\"".format(
user, passwd, name))
local('rm -rf ./website/migrations/')
local('python manage.py makemigrations website')
local('python manage.py migrate website')
local('python manage.py migrate')
local(("echo \"from django.contrib.auth.models import User; "
"User.objects.filter(email='user@email.com').delete(); "
"User.objects.create_superuser('user', 'user@email.com', '123')\" "
"| python manage.py shell"))
local('python manage.py loaddata {}'.format(
os.path.join(PRELOAD_DIR, '*')))
@task
def aggregate_results():
cmd = 'from website.tasks import aggregate_results; aggregate_results()'
local(('export PYTHONPATH={}\:$PYTHONPATH; '
'django-admin shell --settings=website.settings '
'-c\"{}\"').format(PROJECT_ROOT, cmd))
@task
def create_workload_mapping_data():
cmd = ('from website.tasks import create_workload_mapping_data; '
'create_workload_mapping_data()')
local(('export PYTHONPATH={}\:$PYTHONPATH; '
'django-admin shell --settings=website.settings '
'-c\"{}\"').format(PROJECT_ROOT, cmd))
@task
def process_data():
execute(aggregate_results)
execute(create_workload_mapping_data)