/
fabfile.py
204 lines (152 loc) · 4.58 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
#!/usr/bin/env python
import os
from fabric.api import *
from jinja2 import Template
import app_config
"""
Base configuration
"""
env.project_name = 'totebot3'
env.deployed_name = 'totebot3'
env.deploy_to_servers = False
env.repo_url = 'git://github.com/nprapps/totebot3.git'
env.alt_repo_url = None
env.user = 'ubuntu'
env.python = 'python2.7'
env.path = '/home/%(user)s/apps/%(project_name)s' % env
env.repo_path = '%(path)s' % env
env.forward_agent = True
env.user = 'ubuntu'
SERVICES = [('%(project_name)s' % env, '/etc/init/', 'conf')]
"""
Environments
"""
def utils():
env.settings = 'utils'
env.s3_buckets = ['apps.npr.org', 'apps2.npr.org']
env.hosts = ['52.89.61.87']
"""
Branches
"""
def stable():
"""
Work on stable branch.
"""
env.branch = 'stable'
def master():
"""
Work on development branch.
"""
env.branch = 'master'
def branch(branch_name):
"""
Work on any specified branch.
"""
env.branch = branch_name
def _confirm_branch():
"""
Confirm a production deployment.
"""
if (env.settings == 'utils' and env.branch != 'stable'):
answer = prompt("You are trying to deploy the '%(branch)s' branch to production.\nYou should really only deploy a stable branch.\nDo you know what you're doing?" % env, default="Not at all")
if answer not in ('y','Y','yes','Yes','buzz off','screw you'):
exit()
"""
Setup
"""
def setup():
"""
Setup servers for deployment.
"""
require('settings', provided_by=[utils])
require('branch', provided_by=[stable, master, branch])
setup_directories()
clone_repo()
checkout_latest()
install_requirements()
deploy_confs()
def setup_directories():
"""
Create server directories.
"""
require('settings', provided_by=[utils])
run('mkdir -p %(path)s' % env)
def clone_repo():
"""
Clone the source repository.
"""
require('settings', provided_by=[utils])
run('git clone %(repo_url)s %(repo_path)s' % env)
if env.get('alt_repo_url', None):
run('git remote add bitbucket %(alt_repo_url)s' % env)
def checkout_latest(remote='origin'):
"""
Checkout the latest source.
"""
require('settings', provided_by=[utils])
env.remote = remote
run('cd %(repo_path)s; git fetch %(remote)s' % env)
run('cd %(repo_path)s; git checkout %(branch)s; git pull %(remote)s %(branch)s' % env)
def install_requirements():
"""
Install the latest requirements.
"""
require('settings', provided_by=[utils])
run('cd %(repo_path)s; npm install' % env)
def render_confs():
"""
Renders server configurations.
"""
require('settings', provided_by=[utils])
with settings(warn_only=True):
local('mkdir confs/rendered')
context = app_config.get_secrets()
context['PROJECT_SLUG'] = app_config.PROJECT_SLUG
context['PROJECT_NAME'] = app_config.PROJECT_NAME
context['REPOSITORY_NAME'] = app_config.REPOSITORY_NAME
context['DEPLOYMENT_TARGET'] = env.settings
for service, remote_path, extension in SERVICES:
file_path = 'confs/rendered/%s.%s' % (service, extension)
with open('confs/%s.%s' % (service, extension), 'r') as read_template:
with open(file_path, 'wb') as write_template:
payload = Template(read_template.read())
write_template.write(payload.render(**context))
def deploy_confs():
"""
Deploys rendered server configurations to the specified server.
"""
require('settings', provided_by=[utils])
render_confs()
with settings(warn_only=True):
for service, remote_path, extension in SERVICES:
file_name = '%s.%s' % (service, extension)
local_path = 'confs/rendered/%s' % file_name
put(local_path, remote_path, use_sudo=True)
sudo('initctl reload-configuration')
"""
Deployment
"""
def restart_init():
require('settings', provided_by=[utils])
sudo('service totebot3 restart')
def deploy(remote='origin'):
require('settings', provided_by=[utils])
require('branch', provided_by=[stable, master, branch])
_confirm_branch()
checkout_latest(remote)
install_requirements()
restart_init()
"""
Destruction
"""
def shiva_the_destroyer():
"""
Deletes the app from s3
"""
with settings(warn_only=True):
s3cmd = 's3cmd del --recursive %s'
for bucket in env.s3_buckets:
env.s3_bucket = bucket
local(s3cmd % ('s3://%(s3_bucket)s/%(deployed_name)s' % env))
if env.get('deploy_to_servers', False):
run('rm -rf %(path)s' % env)