forked from HHS/pillbox-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
210 lines (148 loc) · 5.85 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import subprocess
import time
from fabric.api import local
from fabric.context_managers import shell_env
from fabric.operations import prompt
def initial_setup():
""" Initial database creation and fixtures creation """
with shell_env(DJANGO_CONFIGURATION='Production'):
try:
choice = int(prompt('What database engine you plan to use? \n' +
'If you choose, Mysql or Postgres, you have to make sure they are' +
' installed on your computer before proceeding further \n' +
'(1) Sqlite3 \n' +
'(2) MySql \n' +
'(3) Postgres (recommended) \n' +
': '))
if choice == 1:
_sync_db()
elif choice == 2:
response = _db_questions(0, '3306')
with shell_env(DATABASE_URL=response):
_install_mysql()
_sync_db()
elif choice == 3:
response = _db_questions(1, '5432')
with shell_env(DATABASE_URL=response):
_install_postgres()
_sync_db()
except ValueError:
print 'Try again! You should enter a number.'
local('python pillbox-engine/manage.py collectstatic --noinput')
def push():
""" Push master branch to github """
local('git push origin master')
def pull():
""" Pull master branch from github """
local('git pull origin master')
def serve():
""" Run the server in production mode """
try:
print 'Launching Pillbox Engine ...'
posix = local('uname', capture=True)
# Only for Mac
if posix == 'Darwin':
foreman = subprocess.Popen(['honcho', 'start'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait for 3 seconds to ensure the process is launched
time.sleep(3)
local('open "http://localhost:5000"')
print 'To exit Pillbox Engine use Control + C'
print foreman.stdout.read()
else:
local('honcho start')
except KeyboardInterrupt:
if posix == 'Darwin':
foreman.terminate()
print 'Goodbye'
def test():
""" Run the server in development mode """
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py runserver')
def shell():
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py shell')
def migrate():
""" Migrate database in development mode """
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py makemigrations')
local('python pillbox-engine/manage.py migrate')
def makemigrations(app):
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py makemigrations %s' % app)
def collect():
""" Collect Static Files """
with shell_env(DJANGO_CONFIGURATION='Production'):
local('python pillbox-engine/manage.py collectstatic')
def update():
""" Fetch the latest updates from the repo"""
local('git pull origin master')
local('pip install -r requirements.txt')
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py migrate')
local('python pillbox-engine/manage.py collectstatic --noinput')
local('python pillbox-engine/manage.py makeusers')
def spl(choice=None):
"""Sync SPL Data. Choices are products | pills | all"""
if choice is None:
choice = 'all'
kwarg = _check_env()
with shell_env(**kwarg):
if choice in ['products', 'pills', 'all']:
local('python pillbox-engine/manage.py syncspl %s' % choice)
else:
print 'wrong choice'
def loaddata():
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py loaddata spl_sources')
local('python pillbox-engine/manage.py loaddata color_shape')
def makeuser():
kwarg = _check_env()
with shell_env(**kwarg):
local('python pillbox-engine/manage.py makeusers')
def _install_mysql():
local('pip install mysql-connector-python --allow-external mysql-connector-python')
def _install_postgres():
local('pip install psycopg2')
def _db_questions(type, port):
db_types = ['mysql-connector', 'postgres']
output = {}
output['username'] = prompt('Database Username: ')
output['password'] = prompt('Database Password: ')
output['host'] = prompt('The host (localhost): ')
output['port'] = prompt('The post (%s): ' % port)
output['db_name'] = prompt('Database Name: ')
output['host'] = output['host'] if output['host'] else 'localhost'
output['port'] = output['port'] if output['port'] else port
db_url = '%s://%s:%s@%s:%s/%s' % (db_types[type],
output['username'],
output['password'],
output['host'],
output['port'],
output['db_name'])
local('echo "DATABASE_URL=%s" > .env' % db_url)
return db_url
def _sync_db():
local('python pillbox-engine/manage.py migrate')
local('python pillbox-engine/manage.py loaddata spl_sources')
local('python pillbox-engine/manage.py loaddata color_shape')
local('python pillbox-engine/manage.py makeusers')
def _check_env():
kwarg = {}
try:
with open('.env', 'r') as env:
content = env.readlines()
for item in content:
split = item.split('=')
kwarg[split[0]] = split[1][:-1]
except IOError:
# Ignore if the .env file doesn't exist
pass
return kwarg
if __name__ == "__main__":
print _check_env()