-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
442 lines (341 loc) · 14.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from invoke import run as local
from invoke.exceptions import Exit
from invoke.tasks import task
PRODUCTION_APP_INSTANCE = 'norwegianehealth-production'
STAGING_APP_INSTANCE = 'norwegianehealth-staging'
STAGING_APP_DB_INSTANCE = 'norwegian_ehealth'
STAGING_REMOTE = 'dokku@staging.torchbox.com'
# Currently no dev instance
# DEV_APP_INSTANCE = 'norwegian_ehealth-dev'
# DEV_APP_DB_INSTANCE = 'norwegian_ehealth-dev'
# DEV_REMOTE = 'dokku@staging.torchbox.com'
LOCAL_MEDIA_FOLDER = '/vagrant/media'
LOCAL_DATABASE_NAME = 'website'
############
# Production
############
@task
def pull_production_media(c):
pull_media_from_s3_heroku(c, PRODUCTION_APP_INSTANCE)
# @task
# def push_production_media(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_media_to_s3_heroku(c, PRODUCTION_APP_INSTANCE)
@task
def pull_production_data(c):
pull_database_from_heroku(c, PRODUCTION_APP_INSTANCE)
# @task
# def push_production_data(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_database_to_heroku(c, PRODUCTION_APP_INSTANCE)
# @task
# def deploy_production(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# deploy_to_heroku(c, PRODUCTION_APP_INSTANCE, local_branch='master',
# remote_branch='master')
@task
def production_shell(c):
open_heroku_shell(c, PRODUCTION_APP_INSTANCE)
#########
# Staging
#########
@task
def pull_staging_data(c):
pull_database_from_dokku(c, STAGING_REMOTE, STAGING_APP_DB_INSTANCE)
@task
def pull_staging_media(c):
pull_media_from_s3_dokku(c, STAGING_REMOTE, STAGING_APP_INSTANCE)
# @task
# def push_staging_media(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_media_to_s3_dokku(c, STAGING_REMOTE, STAGING_APP_INSTANCE)
# @task
# def push_staging_data(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_database_to_dokku(c, STAGING_REMOTE, STAGING_APP_INSTANCE,
# STAGING_APP_DB_INSTANCE)
# @task
# def deploy_staging(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# deploy_to_dokku(c, STAGING_REMOTE, STAGING_APP_INSTANCE,
# local_branch='staging', remote_branch='staging')
@task
def staging_shell(c):
open_dokku_shell(c, STAGING_REMOTE, STAGING_APP_INSTANCE)
#####
# Dev
#####
# @task
# def pull_dev_data(c):
# pull_database_from_dokku(c, DEV_REMOTE, DEV_APP_DB_INSTANCE)
# @task
# def pull_dev_media(c):
# pull_media_from_s3_dokku(c, DEV_REMOTE, DEV_APP_INSTANCE)
# @task
# def push_dev_data(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_database_to_dokku(c, DEV_REMOTE, DEV_APP_INSTANCE,
# DEV_APP_DB_INSTANCE)
# @task
# def push_dev_media(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# push_media_to_s3_dokku(c, DEV_REMOTE, DEV_APP_INSTANCE)
# @task
# def deploy_dev(c):
# raise RuntimeError('Please check the configuration of the fabfile before using it.')
# deploy_to_dokku(c, DEV_REMOTE, DEV_APP_INSTANCE,
# local_branch='develop', remote_branch='develop')
# @task
# def dev_shell(c):
# open_dokku_shell(c, DEV_REMOTE, DEV_APP_INSTANCE)
#######
# Local
#######
def clean_local_database(c, local_database_name=LOCAL_DATABASE_NAME):
local(
'sudo -u postgres psql -d {database_name} -c "DROP SCHEMA public '
'CASCADE; CREATE SCHEMA public;"'.format(
database_name=local_database_name
)
)
def delete_local_database(c, local_database_name=LOCAL_DATABASE_NAME):
local('dropdb --if-exists {database_name}'.format(
database_name=LOCAL_DATABASE_NAME
))
########
# Heroku
########
def check_if_logged_in_to_heroku(c):
if not local('heroku auth:whoami', warn=True):
raise Exit(
'Log-in with the "heroku login -i" command before running this '
'command.'
)
def get_heroku_variable(c, app_instance, variable):
check_if_logged_in_to_heroku(c)
return local('heroku config:get {var} --app {app}'.format(
app=app_instance,
var=variable,
)).stdout.strip()
def pull_media_from_s3_heroku(c, app_instance):
check_if_logged_in_to_heroku(c)
aws_access_key_id = get_heroku_variable(c, app_instance,
'AWS_ACCESS_KEY_ID')
aws_secret_access_key = get_heroku_variable(c, app_instance,
'AWS_SECRET_ACCESS_KEY')
aws_storage_bucket_name = get_heroku_variable(c, app_instance,
'AWS_STORAGE_BUCKET_NAME')
pull_media_from_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name)
def push_media_to_s3_heroku(c, app_instance):
check_if_logged_in_to_heroku(c)
prompt_msg = 'You are about to push your media folder contents to the ' \
'S3 bucket. It\'s a destructive operation. \n' \
'Please type the application name "{app_instance}" to ' \
'proceed:\n>>> '.format(app_instance=make_bold(app_instance))
if input(prompt_msg) != app_instance:
raise Exit("Aborted")
aws_access_key_id = get_heroku_variable(c, app_instance,
'AWS_ACCESS_KEY_ID')
aws_secret_access_key = get_heroku_variable(c, app_instance,
'AWS_SECRET_ACCESS_KEY')
aws_storage_bucket_name = get_heroku_variable(c, app_instance,
'AWS_STORAGE_BUCKET_NAME')
push_media_to_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name)
def pull_database_from_heroku(c, app_instance):
check_if_logged_in_to_heroku(c)
delete_local_database(c)
local('heroku pg:pull --app {app} DATABASE_URL {local_database}'.format(
app=app_instance,
local_database=LOCAL_DATABASE_NAME
))
def push_database_to_heroku(c, app_instance):
check_if_logged_in_to_heroku(c)
prompt_msg = 'You are about to push your local database to Heroku. ' \
'It\'s a destructive operation and will override the ' \
'database on the server. \n' \
'Please type the application name "{app_instance}" to ' \
'proceed:\n>>> '.format(app_instance=make_bold(app_instance))
if input(prompt_msg) != app_instance:
raise Exit("Aborted")
local('heroku maintenance:on --app {app}'.format(app=app_instance))
local('heroku ps:stop --app {app} web'.format(app=app_instance))
local('heroku pg:backups:capture --app {app}'.format(app=app_instance))
local('heroku pg:reset --app {app} --confirm {app}'.format(app=app_instance))
local('heroku pg:push --app {app} {local_db} DATABASE_URL'.format(
app=app_instance,
local_db=LOCAL_DATABASE_NAME
))
local('heroku ps:restart --app {app}'.format(app=app_instance))
local('heroku maintenance:off --app {app}'.format(app=app_instance))
def setup_heroku_git_remote(c, app_instance):
check_if_logged_in_to_heroku(c)
remote_name = 'heroku-{app}'.format(app=app_instance)
local('heroku git:remote --app {app} --remote {remote}'.format(
app=app_instance, remote=remote_name
))
return remote_name
def deploy_to_heroku(c, app_instance, local_branch='master',
remote_branch='master'):
check_if_logged_in_to_heroku(c)
print(
'This will push your local "{local_branch}" branch to remote '
'"{remote_branch}" branch.'.format(
local_branch=local_branch,
remote_branch=remote_branch
)
)
deploy_prompt(c, app_instance)
remote_name = setup_heroku_git_remote(c, app_instance)
local('git push {remote} {local_branch}:{remote_branch}'.format(
remote=remote_name,
local_branch=local_branch,
remote_branch=remote_branch,
))
def open_heroku_shell(c, app_instance, shell_command='bash'):
check_if_logged_in_to_heroku(c)
local('heroku run --app {app} {command}'.format(
app=app_instance,
command=shell_command,
))
#######
# Dokku
#######
def pull_database_from_dokku(c, dokku_remote, app_db_instance):
clean_local_database(c)
local('ssh {remote} postgres:export {db_instance} | '
'pg_restore -d {local_database}'.format(
remote=dokku_remote,
db_instance=app_db_instance,
local_database=LOCAL_DATABASE_NAME,
))
def push_database_to_dokku(c, dokku_remote, app_instance, db_instance):
prompt_msg = 'You are about to push your local database to Dokku. ' \
'It\'s a destructive operation and will override the ' \
'database on the server. \n' \
'Please type the database name "{db_instance}" to ' \
'proceed:\n>>> '.format(db_instance=make_bold(db_instance))
if input(prompt_msg) != db_instance:
raise Exit("Aborted")
local('ssh {remote} ps:stop {app}'.format(remote=dokku_remote,
app=app_instance))
clean_dokku_database(c, dokku_remote, db_instance)
local(
'pg_dump -Fc --no-acl --no-owner -w {local_db} | '
'ssh -t {remote} postgres:import {db_instance} '
'|| true'.format(
local_db=LOCAL_DATABASE_NAME,
remote=dokku_remote,
db_instance=db_instance,
)
)
local('ssh {remote} ps:start {app}'.format(remote=dokku_remote,
app=app_instance))
def pull_media_from_s3_dokku(c, dokku_remote, app_instance):
aws_access_key_id = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_ACCESS_KEY_ID')
aws_secret_access_key = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_SECRET_ACCESS_KEY')
aws_storage_bucket_name = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_STORAGE_BUCKET_NAME')
pull_media_from_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name)
def push_media_to_s3_dokku(c, dokku_remote, app_instance):
prompt_msg = 'You are about to push your media folder contents to the ' \
'S3 bucket. It\'s a destructive operation. \n' \
'Please type the application name "{app_instance}" to ' \
'proceed:\n>>> '.format(app_instance=make_bold(app_instance))
if input(prompt_msg) != app_instance:
raise Exit("Aborted")
aws_access_key_id = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_ACCESS_KEY_ID')
aws_secret_access_key = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_SECRET_ACCESS_KEY')
aws_storage_bucket_name = get_dokku_variable(c, dokku_remote, app_instance,
'AWS_STORAGE_BUCKET_NAME')
push_media_to_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name)
def get_dokku_variable(c, dokku_remote, app_instance, variable):
return local('ssh {remote} config:get {app} {var}'.format(
remote=dokku_remote,
app=app_instance,
var=variable,
)).stdout.strip()
def deploy_to_dokku(c, dokku_remote, app_instance, local_branch='master',
remote_branch=None):
if remote_branch is None:
remote_branch = local_branch
print(
'This will push your local "{local_branch}" branch to remote '
'"{remote_branch}" branch.'.format(
local_branch=local_branch,
remote_branch=remote_branch
)
)
deploy_prompt(c, app_instance)
local('git push {remote}:{app} {local_branch}:{remote_branch}'.format(
remote=dokku_remote,
app=app_instance,
local_branch=local_branch,
remote_branch=remote_branch,
))
def open_dokku_shell(c, dokku_remote, app_instance):
local('ssh -t {remote} enter {app}'.format(remote=dokku_remote,
app=app_instance), pty=True)
def clean_dokku_database(c, dokku_remote, db_instance):
local(
'ssh -t {remote} postgres:export {db_instance} > '
'{db_instance}-backup.pg'.format(
remote=dokku_remote,
db_instance=db_instance,
)
)
local(
'echo "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" | '
'ssh -t {remote} postgres:connect {db_instance}'.format(
remote=dokku_remote,
db_instance=db_instance,
)
)
####
# S3
####
def aws(c, command, aws_access_key_id, aws_secret_access_key, **kwargs):
return local(
'AWS_ACCESS_KEY_ID={access_key_id} AWS_SECRET_ACCESS_KEY={secret_key} '
'aws {command}'.format(
access_key_id=aws_access_key_id,
secret_key=aws_secret_access_key,
command=command,
),
**kwargs
)
def pull_media_from_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name,
local_media_folder=LOCAL_MEDIA_FOLDER):
aws_cmd = 's3 sync --delete s3://{bucket_name} {local_media}'.format(
bucket_name=aws_storage_bucket_name,
local_media=local_media_folder,
)
aws(c, aws_cmd, aws_access_key_id, aws_secret_access_key)
def push_media_to_s3(c, aws_access_key_id, aws_secret_access_key,
aws_storage_bucket_name,
local_media_folder=LOCAL_MEDIA_FOLDER):
aws_cmd = 's3 sync --delete {local_media} s3://{bucket_name}/'.format(
bucket_name=aws_storage_bucket_name,
local_media=local_media_folder,
)
aws(c, aws_cmd, aws_access_key_id, aws_secret_access_key)
###########
# Utilities
###########
def deploy_prompt(c, app_instance):
prompt_msg = 'You are about to do a manual deployment. You probably ' \
'should use automatic deployments on CI. \nPlease type ' \
'the application name "{app_instance}" in order to ' \
'proceed:\n>>> '.format(app_instance=make_bold(app_instance))
if input(prompt_msg) != app_instance:
raise Exit("Aborted")
def make_bold(msg):
return "\033[1m{}\033[0m".format(msg)