This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjames.py
executable file
·298 lines (224 loc) · 8.96 KB
/
james.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
#!/usr/bin/env python
"""
james.py - Chief CLI.
USAGE: james.py ENV REF
ENV - Environment defined in the config file to deploy to.
REF - A git reference (like a SHA) to deploy.
Config: james.ini in the current directory should be an ini file with
one section per environment. Each environment should have a
`revision_url`, `chief_url`, and `password`. A special section,
`general`, may exist, which will can have one key: `username`. If no
username is given in general, the result of the command "whoami" will be
used.
Example Config:
[general]
username = bob
github = bobloblaw/lawblog
[prod]
revision_url = http://example.com/media/revision.txt
chief_url = http://chief.example.com/example.prod
password = lolpassword
[stage]
revision_url = http://stage.example.com/media/revision.txt
chief_url = http://chief.example.com/example.stage
password = omgsecret
Then you can use james.py like this:
./james.py stage fa0594dc16df3be505592b6346412c0a03cfe5bf
Answer the questions, and wait a bit, and a deploy will happen! You will
see the same output that you would if you deployed using the website.
Dependencies: requests
"""
import argparse
import os
import random
import re
import subprocess
import sys
import time
import traceback
import webbrowser
try:
from ConfigParser import ConfigParser, NoOptionError, NoSectionError
except ImportError:
from configparser import ConfigParser, NoOptionError, NoSectionError
import requests
URL_TEMPLATE = 'https://github.com/{repo}/compare/{rev}...{branch}'
HASH_LEN = 8
def get_random_desc():
return random.choice([
'No bugfixes--must be adding infinite loops.',
'No bugfixes--must be removing infinite loops.',
'No bugfixes--must be rot13ing function names for code security.',
'No bugfixes--must be unrot13ing function names for code clarity.',
'No bugfixes--must be demonstrating our elite push technology.',
'No bugfixes--must be testing james.',
'No bugfixes--must be making the blinkenlichten flash.',
])
def git(*args, **kwargs):
args = ['git'] + list(args)
if kwargs.pop('out', None) == 'print':
subprocess.check_call(args, **kwargs)
return None
else:
return subprocess.check_output(args, **kwargs)
def config(environment, key, required=False, memo={}):
if 'config' not in memo:
memo['config'] = ConfigParser()
memo['config'].read('james.ini')
try:
return memo['config'].get(environment, key)
except NoSectionError:
if required:
print('No such environment %s' % environment)
sys.exit(2)
except NoOptionError:
if required:
print('Missing key %s in environment %s' % (key, environment))
sys.exit(4)
return None
def usage():
print('USAGE: %s ENV [REF]'
' ENV - Environment defined in the config file to deploy to.'
' REF - A git reference (like a SHA) to deploy (default HEAD)' %
os.path.split(sys.argv[0])[-1])
def check_ancestry(older, newer):
commits = git('rev-list', newer).split('\n')
return older in commits
def yes_no(prompt):
sys.stdout.write(prompt + ' ')
ret = raw_input('[y/n] ')
while ret not in ['y', 'n']:
ret = raw_input('Please choose "y" or "n" [y/n] ')
return ret == 'y'
def username():
username = config('general', 'username')
if username is None:
username = subprocess.check_output(['whoami'])
return username.strip()
def get_environment_commit(environment):
revision_url = config(environment, 'revision_url', required=True)
if not revision_url.startswith('http'):
revision_url = 'http://' + revision_url
return requests.get(revision_url).text.strip()
def get_compare_url(env_rev, new_rev=None):
repo = config('general', 'github', required=True)
return URL_TEMPLATE.format(rev=env_rev[:HASH_LEN],
branch=new_rev[:HASH_LEN],
repo=repo)
def extract_bugs(changelog):
"""Takes output from git log --oneline and extracts bug numbers"""
bug_regexp = re.compile(r'\bbug (\d+)\b', re.IGNORECASE)
bugs = set()
for line in changelog:
for bug in bug_regexp.findall(line):
bugs.add(bug)
return sorted(list(bugs))
def generate_desc(from_commit, to_commit, changelog):
# Figure out a good description based on what we're pushing
# out.
if from_commit.startswith(to_commit):
desc = 'Pushing {0} again'.format(to_commit)
else:
bugs = extract_bugs(changelog.split('\n'))
if bugs:
bugs = ['bug #{0}'.format(bug) for bug in bugs]
desc = 'Fixing: {0}'.format(', '.join(bugs))
else:
desc = get_random_desc()
return desc
def webhooks(env, environment_commit, local_commit):
if config(env, 'newrelic'):
print('Running New Relic deploy hook...'),
log_spec = '{0}..{1}'.format(environment_commit, local_commit)
changelog = git('log', '--pretty=oneline', log_spec)
desc = generate_desc(environment_commit, local_commit, changelog)
rev = changelog.split('\n')[0].split(' ')[0]
data = {
'app_name': config('newrelic', 'app_name', required=True),
'application_id': config('newrelic', 'application_id',
required=True),
'description': desc,
'revision': rev,
'changelog': changelog,
'user': username(),
}
url = 'https://rpm.newrelic.com/deployments.xml'
data = dict(('deployment[%s]' % k, v) for k, v in data.items())
headers = {'x-api-key': config('newrelic', 'api_key', required=True)}
res = requests.post(url, data=data, headers=headers)
print(res.status_code, res.text)
print('done')
def main():
parser = argparse.ArgumentParser(description='Push code using Chief.')
parser.add_argument('env', metavar='ENV',
help='Environment defined in the config file to '
'deploy to.')
parser.add_argument('ref', metavar='REF', nargs='?', default='HEAD',
help='A git reference (like a SHA) to deploy (default '
'HEAD)')
parser.add_argument('-g', '--github', action='store_true',
help='Open a browser to the Github compare url for '
'the diff.')
parser.add_argument('-p', '--print', action='store_true',
dest='print_only',
help='Only print the git log (or Github URL with -g), '
'nothing more.')
args = parser.parse_args()
environment = args.env
commit = args.ref
environment_commit = get_environment_commit(environment)
local_commit = git('rev-parse', commit).strip()
if args.github:
url = get_compare_url(environment_commit, local_commit)
print(url)
if not args.print_only:
webbrowser.open(url)
return 0
chief_url = config(environment, 'chief_url', required=True)
password = config(environment, 'password', required=True)
if not chief_url.startswith('http'):
chief_url = 'http://' + chief_url
print('Environment: {0}'.format(environment))
print('Pushing as : {0}'.format(username()))
print('Pushing : {0} ({1})'.format(commit, local_commit[:HASH_LEN]))
print('On server : {0}'.format(environment_commit[:HASH_LEN]))
log_spec = environment_commit + '..' + local_commit
if environment_commit.startswith(local_commit):
print('Pushing out (again):')
git('log', '--oneline', '-n', '1', local_commit, out='print')
elif not check_ancestry(environment_commit, local_commit):
print('Pushing from different branch:')
git('log', '--oneline', '-n', '1', local_commit, out='print')
else:
print('Pushing out:')
git('log', '--oneline', log_spec, out='print')
if args.print_only:
return 0
print('')
if yes_no('Proceed?'):
payload = {
'who': username(),
'password': password,
'ref': local_commit,
}
print('Logs at: {0}/logs/{1}'.format(chief_url, local_commit))
start_time = time.time()
try:
res = requests.post(chief_url, data=payload, stream=True)
except requests.RequestException:
traceback.print_exc()
print('Error connecting to Chief. Did you connect to the VPN?')
return 1
for chunk in res.iter_content():
sys.stdout.write(chunk)
sys.stdout.flush()
# Chief doesn't finish with a newline. Rude.
print('')
end_time = time.time()
print('Total time: {0}'.format(end_time - start_time))
webhooks(environment, environment_commit, local_commit)
else:
print('Canceled!')
return 1
if __name__ == '__main__':
sys.exit(main())