-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile_django.py
225 lines (170 loc) · 4.61 KB
/
fabfile_django.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import os
from fabric import Connection
from invoke import task
from fabric import Config
__prod = None
def get_prod(c):
global __prod
if not __prod:
config = Config(overrides={"run": {"echo": True}})
__prod = Connection(host=c.default.host, user=c.default.user, config=config)
return __prod
def app_ssh_path(c):
return f'{c.settings.prod_server}:{c.settings.app_dir}/{c.settings.app_django_dir}'
################ GIT ################
@task
def commit(c):
"""
Commit changes
"""
conn = get_prod(c)
message = input('Enter a git commit message: ')
conn.local(f'git add -A && git commit -m "{message}"')
print('Changes have been pushed to remote repository...')
@task
def push(c):
"""
Push and pull
"""
conn = get_prod(c)
conn.local(f'git push origin {c.settings.main_branch}')
with conn.prefix(c.settings.venv_script):
conn.run('git pull')
conn.run('git submodule update --init --recursive')
################ DEPLOY ################
@task
def pip(c):
"""
Install requirements
"""
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
conn.run('pip install --upgrade pip')
conn.run('pip install -r requirements.txt')
@task
def collect_static(c):
"""
Collect static files
"""
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
conn.run('python manage.py collectstatic --noinput')
@task
def migrate(c):
"""
Execute migrations
"""
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
migrate_apps = c.settings.get('migrate_apps', None)
conn.run('python manage.py migrate' + (f' {migrate_apps}' if migrate_apps else ''))
@task
def restart(c):
"""
Restart app on the server
"""
conn = get_prod(c)
conn.run(c.settings.restart_script)
@task
def deploy(c):
"""
Push, pull, collect static, restart
"""
push(c)
collect_static(c)
migrate(c)
restart(c)
################ DATA ################
@task
def sync_media(c):
"""
Download production media files to local computer
"""
conn = get_prod(c)
conn.local(f'rsync -avz {app_ssh_path(c)}/media/ media/')
@task
def db_dump(c):
"""
Dump entire db on server and retrieve it
"""
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
conn.run('mkdir -p data', hide=True)
conn.run(f'./manage.py dumpdata {c.settings.dump_data_models} --indent=2 > data/db.json')
conn.run('tar cvfz data/db.tgz data/db.json')
conn.run('rm data/db.json', hide=True)
conn.local('mkdir -p data', hide=True)
conn.local(f'scp {app_ssh_path(c)}/data/db.tgz data')
@task
def db_load(c):
"""
Load the dumped db on local computer
"""
conn = get_prod(c)
conn.local('tar xvfz data/db.tgz')
conn.local('./manage.py django_clear_tables')
conn.local('./manage.py loaddata data/db.json')
conn.local('rm data/db.json')
################ INITIAL DB ################
@task
def initial_dump(c):
"""
Dump initial data on server and retrieve it
"""
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
conn.run(f'./manage.py dumpdata {c.settings.dump_initial} --indent=2 > data/initial.json')
conn.local(f'scp {app_ssh_path(c)}/data/initial.json data')
@task
def initial_load(c):
"""
Load initial data on local computer
"""
conn = get_prod(c)
conn.local('./manage.py loaddata data/initial.json')
if os.path.exists('data/initial_local.json'):
conn.local('./manage.py loaddata data/initial_local.json')
@task
def reset_local_db(c):
"""
resets local db
"""
conn = get_prod(c)
conn.local('rm -f db.sqlite3')
conn.local('./manage.py migrate')
print('\n\n\nEnter admin password:\n\n\n')
conn.local(f'./manage.py createsuperuser --username {c.settings.superuser_user} --email {c.settings.superuser_mail}', pty=True)
initial_load(c)
################ RUN JOBS ################
def __run_jobs(c, job_type):
conn = get_prod(c)
with conn.prefix(c.settings.venv_script):
conn.run(f'python manage.py runjobs {job_type}')
@task
def hourly(c):
"""
Run hourly jobs
"""
__run_jobs(c, 'hourly')
@task
def daily(c):
"""
Run daily jobs
"""
__run_jobs(c, 'daily')
################ CELERY ################
@task
def celery(c):
"""
Restarts celery
"""
conn = get_prod(c)
conn.run('monit restart celery')
@task
def monit(c):
"""
Monit status
"""
conn = get_prod(c)
conn.run('monit')
conn.run('monit status')