-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
182 lines (139 loc) · 4.86 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
import os
import random
import string
from fabric.api import env, local, require, lcd
from fabric.colors import cyan
from fabric.operations import prompt
current_dir = os.getcwd()
env.project_name = 'rugbystat'
env.branch = 'master'
env.environments = ['dev',
'qa',
'prod']
def serve():
local('python {}/manage.py runserver'.format(env.project_name))
def test():
"""
Runs nose test suite
"""
local('flake8 {}'.format(env.project_name))
print cyan('flake8 passed!', bold=True)
local('python {}/manage.py test'.format(env.project_name))
def init():
"""
Deploys servers
"""
print cyan('Initializing...', bold=True)
set_remotes()
ask_for_aws_keys()
for environment in env.environments:
env.environment = environment
env.server_name = '{}-{}'.format(env.project_name, env.environment)
create_standard_server()
deploy_docs()
def set_remotes():
"""
Sets git remotes based on project structure
"""
require('project_name')
print cyan('Setting git remotes...')
local('git remote add dev [email protected]:{}-dev.git'.format(env.project_name))
local('git remote add qa [email protected]:{}-qa.git'.format(env.project_name))
local('git remote add prod [email protected]:{}-prod.git'.format(env.project_name))
def ask_for_aws_keys():
"""
Gets AWS keys from user
"""
env.aws_access = prompt('AWS_ACCESS_KEY_ID?')
env.aws_secret = prompt('AWS_SECRET_ACCESS_KEY?')
def create_standard_server():
"""
Creates a sever with a standard build
"""
create_server()
configure_sever()
push()
migrate()
create_superuser()
ps()
open_heroku()
def create_server():
"""
Creates a new server on heroku
"""
require('environment')
require('project_name')
print cyan('Creating new server'.format(env.project_name, env.environment))
require('environment')
local('heroku create {}-{} --buildpack https://github.com/heroku/heroku-buildpack-python'
.format(env.project_name, env.environment))
def configure_sever():
"""
Configures server with a general configuration
"""
require('environment')
local('heroku addons:create heroku-postgresql --remote {}'.format(env.environment))
local('heroku pg:backups schedule DATABASE --at "04:00 UTC" --remote {}'.format(env.environment))
local('heroku pg:promote DATABASE_URL --remote {}'.format(env.environment))
local('heroku addons:create redistogo:nano --remote {}'.format(env.environment))
local('heroku addons:create newrelic:wayne --remote {}'.format(env.environment))
local('heroku config:set NEW_RELIC_APP_NAME="{}" --remote {}'.format(env.project_name, env.environment))
local('heroku config:set DJANGO_CONFIGURATION=Production --remote {}'.format(env.environment))
local('heroku config:set DJANGO_SECRET_KEY="{}" --remote {}'.format(create_secret_key(), env.environment))
set_aws_keys()
def deploy_docs():
print cyan('Deploying docs...')
local('mkdocs gh-deploy')
def push():
require('environment')
require('branch')
print cyan('Pushing to Heroku...')
require('environment')
local('git push {} {}:master'.format(env.environment, env.branch))
def migrate():
require('environment')
local('heroku run python {}/manage.py migrate --remote {}'.format(env.project_name,
env.environment))
def create_superuser():
require('environment')
local('heroku run python {}/manage.py '
'createsuperuser --remote {}'.format(env.project_name, env.environment))
def ps():
"""
Scales a web dyno on Heroku
"""
require('environment')
local('heroku ps:scale web=1:hobby --remote {}'.format(env.environment))
def open_heroku():
require('environment')
local('heroku open --remote {}'.format(env.environment))
def set_aws_keys():
"""
Configures S3 Keys
"""
require('aws_access')
require('aws_secret')
require('project_name')
local('heroku config:set DJANGO_AWS_ACCESS_KEY_ID={} --remote {}'
.format(env.aws_access, env.environment))
local('heroku config:set DJANGO_AWS_SECRET_ACCESS_KEY={} --remote {}'
.format(env.aws_secret, env.environment))
local('heroku config:set DJANGO_AWS_STORAGE_BUCKET_NAME={0}-{1} --remote {1}'
.format(env.project_name, env.environment))
def create_secret_key():
"""
Creates a random string of letters and numbers
"""
return ''.join(random.choice(string.ascii_letters + string.digits) for i in range(30))
def dev():
"""fab dev [command]"""
env.environment = 'dev'
env.branch = 'master'
def qa():
"""fab staging [command]"""
env.environment = 'qa'
env.branch = 'qa'
def prod():
"""fab staging [command]"""
env.environment = 'prod'
env.branch = 'prod'