-
Notifications
You must be signed in to change notification settings - Fork 0
/
piehole.py
executable file
·425 lines (386 loc) · 13.4 KB
/
piehole.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
#!/usr/bin/env python3
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 textwidth=79
'''
piehole: it's always open
Replicate Git repositories using etcd.
'''
import argparse
import cgi
from datetime import datetime
import fcntl
import filecmp
import http.server
import json
import locale
import os
import shutil
import socketserver
import subprocess
import sys
import time
import urllib.parse
import urllib.request
GIT = '/usr/bin/git'
CONFIG_PREFIX = 'piehole'
ETCD_PREFIX = 'piehole'
ETCD_ROOT = 'http://127.0.0.1:4001'
DAEMON_PORT = 3690
BLANK = '0000000000000000000000000000000000000000' # don't change
class GitFailure(Exception):
pass
class SanityCheckFailure(Exception):
pass
def log(message='', to=sys.stdout, cache={}):
to = cache['to'] = cache.get('to', to)
if hasattr(to, 'writable') and to.writable:
print(message, file=to)
return
try:
with open(to, 'a+') as logfd:
fcntl.lockf(logfd, fcntl.LOCK_EX)
for item in str(message).splitlines():
line = "%s %s %s\n" % (os.getpid(), datetime.now().isoformat(), item.strip())
logfd.write(str(line))
except FileNotFoundError:
pass
def log_error(line):
log(line, to=sys.stderr)
def fail(message):
log_error(message)
sys.exit(1)
class ForkingHTTPServer(socketserver.ForkingMixIn, http.server.HTTPServer):
pass
class TransferRequestHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, format, *args):
log("%s\n" % (format % args))
def do_POST(self):
try:
ctype, pdict = cgi.parse_header(
self.headers.get('content-type'))
if ctype != 'application/x-www-form-urlencoded':
raise Exception("bad content type")
length = int(self.headers.get('content-length'))
content = self.rfile.read(length).decode('utf-8')
params = urllib.parse.parse_qs(content)
ref = None
action = params['action'][0]
if action == 'ping':
pass
else:
os.chdir(params['repo'][0])
sanity_check()
ref = params['ref'][0]
out = ''
code = 200
except SanityCheckFailure as err:
out = str(err) + "\n"
code = 400
except KeyError as err:
out = "Error in request: missing parameter %s" % str(err)
self.log_error(out)
code = 400
except Exception as err:
out = str(err)
self.log_error(out)
code = 500
self.send_response(code)
self.send_header('Content-type', 'text/plain; charset="UTF-8"')
self.send_header('Content-length', str(len(out)))
self.end_headers()
self.wfile.write(out.encode('utf-8'))
if code == 200 and action and ref:
try:
self.log_message("Transferring %s from %s" % (ref, reporoot()))
start_transfer(ref, action)
except Exception as err:
self.log_error(str(err))
def start_daemon(logpath):
serveraddr = ('127.0.0.1', DAEMON_PORT)
try:
os.setsid()
daemon = ForkingHTTPServer(serveraddr, TransferRequestHandler)
log('', to=logpath)
except OSError as err:
if 98 == err.errno:
fail(str(err))
else:
raise
daemon.serve_forever()
def run_git(*args):
encoding = locale.getpreferredencoding()
lines = []
try:
args = [GIT] + list(args)
gitcmd = subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in gitcmd.stdout:
lines.append(line.decode(encoding))
gitcmd.stdout.close()
code = gitcmd.wait()
if code != 0:
raise GitFailure(''.join(lines))
return ''.join(lines)
except subprocess.CalledProcessError:
raise GitFailure(''.join(lines))
def list_refs():
result = []
try:
for line in run_git('show-ref', '--tags', '--heads').splitlines():
refname = line.split()[1]
result.append(refname)
except GitFailure as err:
if '' == str(err):
pass
else:
raise
return result
def reporoot():
git_dir = run_git('rev-parse', '--git-dir').strip()
return os.path.abspath(git_dir)
def reporef(ref):
try:
res = run_git('show-ref', '--hash', ref)
return res.strip()
except GitFailure:
return BLANK
def guess_repourl():
return urllib.parse.urljoin("file:///",
urllib.request.pathname2url(reporoot()))
def guess_reponame():
name = os.path.split(reporoot())[1]
if name[-4:] == '.git':
return name[:-4]
else:
return name
def config(key, value=None, cache={}):
git_key = key if '.' in key else '.'.join((CONFIG_PREFIX, key))
if value is None:
if key in cache:
return cache[key]
try:
res = run_git('config', '--local', git_key).strip()
except GitFailure:
res = None
cache[key] = res
return res
else:
run_git('config', '--local', git_key, value)
cache[key] = value
return value
def etcd_loc(key):
return "%s/v1/keys/%s/%s" % (config('etcdroot'), config('etcdprefix'),
urllib.parse.quote(key))
def etcd_read(key):
loc = etcd_loc(key)
try:
res = urllib.request.urlopen(loc).read().decode('ascii').strip()
data = json.loads(res)
return data['value']
except urllib.error.HTTPError as err:
if err.code >= 400 and err.code < 500:
return None
else:
raise
def etcd_write(key, value, prev=None):
loc = etcd_loc(key)
params = {'value': value}
if prev is not None:
params['prevValue'] = prev
postdata = urllib.parse.urlencode(params).encode('ascii')
try:
res = urllib.request.urlopen(loc, postdata)
charset = res.headers.get_param('charset')
data = json.loads(res.read().decode(charset))
return True if data.get('action') == 'SET' else False
except urllib.error.HTTPError as err:
charset = err.headers.get_param('charset')
data = json.loads(err.read().decode(charset))
log(data.get('message'))
log(data.get('cause'))
return False
def invoke_daemon(repo, ref, action):
params = {'repo': repo, 'ref': ref, 'action': action}
try:
postdata = urllib.parse.urlencode(params).encode('ascii')
loc = "http://127.0.0.1:%s" % DAEMON_PORT
res = urllib.request.urlopen(loc, postdata)
content = res.read().decode('utf-8')
return content
except urllib.error.HTTPError as err:
log_error(str(err))
def sanity_check(installed=True):
try:
if config('core.bare') != 'true':
raise SanityCheckFailure("%s is not a bare Git repository." % os.getcwd())
#TODO check that repo has permissions for the piehole group
except GitFailure as e:
raise SanityCheckFailure("%s does not seem to be a Git repository" % os.getcwd())
if installed and config('core.logAllRefUpdates') != 'true':
raise SanityCheckFailure("core.logAllRefUpdates is off")
for item in ('etcdprefix', 'etcdroot', 'repourl', 'repogroup'):
if installed and not config(item):
raise SanityCheckFailure("%s.%s not set" % (CONFIG_PREFIX, item))
for hook in ('update', 'post-update'):
path = os.path.join(reporoot(), 'hooks', hook)
if os.path.isfile(path) and os.path.isfile(__file__):
if not filecmp.cmp(__file__, path):
raise SanityCheckFailure("Hook already exists at %s" % path)
if not os.access(path, os.X_OK):
raise SanityCheckFailure("%s is not executable" % path)
def repogroup_members():
members = etcd_read(config('repogroup'))
if members is None:
present = []
else:
present = list(members.split(' '))
present.sort()
return present
def add_to_repogroup():
while True:
present = repogroup_members()
if config('repourl') in present:
break
oldmembers = ' '.join(present)
present.append(config('repourl'))
present.sort()
newmembers = ' '.join(present)
newvalue = etcd_write(config('repogroup'), newmembers, oldmembers)
if newvalue:
break
def register(fn):
"Check that this repo is enrolled in its group, and enroll it if not."
def wrapped(*args):
sanity_check()
add_to_repogroup()
fn(*args)
return wrapped
def install(repogroup, repourl, etcdroot, etcdprefix):
try:
sanity_check(installed=False)
except SanityCheckFailure as err:
fail(str(err))
for hook in ('update', 'post-update'):
path = os.path.join(reporoot(), 'hooks', hook)
shutil.copyfile(__file__, path)
os.chmod(path, 0o755)
config('core.logAllRefUpdates', 'true')
config('etcdroot', etcdroot)
config('etcdprefix', etcdprefix)
config('repogroup', repogroup)
config('repourl', repourl)
if repourl.startswith('file'):
log("Using %s for repo URL." % repourl)
log("You probably want an ssh URL instead.")
add_to_repogroup()
@register
def start_transfer(ref, command):
'''
Start transferring objects to or from the repos
in the repogroup.
'''
if command not in ['fetch', 'push']:
raise NotImplementedError("Unknown command: %s" % command)
here = config('repourl')
if ref.startswith('refs/heads/'):
refname = ref[11:]
elif ref.startswith('refs/tags/'):
refname = ref[10:]
else:
raise NotImplementedError("%s of unknown item %s" % (command, ref))
target = "%s:%s" % (refname, refname) if command == 'fetch' else refname
for remote in repogroup_members():
if remote == here:
continue
try:
log(run_git(command, remote, target))
except GitFailure as f:
log_error(str(f))
@register
def post_update():
'''
When run as a post-update hook, just start pushing
everything that changed to the other members of
the repogroup.
'''
for ref in sys.argv[1:]:
invoke_daemon(reporoot(), ref, 'push')
sys.exit(0)
@register
def update():
'''
Accept or reject changes to refs.
'''
ref, old, new = sys.argv[1:4]
repogroup = config('repogroup')
current = etcd_read("%s %s" % (repogroup, ref))
if current == new:
# This is safe even if the ref just changed since reading from etcd.
log("Accepting replication of %s from %s to %s" % (ref, old, new))
sys.exit(0)
oldval = '' if old == BLANK else old
if etcd_write("%s %s" % (repogroup, ref), new, oldval):
log("Updating %s from %s to %s." % (ref, old, new))
sys.exit(0)
try:
run_git('update-ref', ref, current)
log("Setting %s to known commit %s" % (ref, current))
except GitFailure:
invoke_daemon(reporoot(), ref, 'fetch')
log("Started fetch of %s" % ref)
log("Failed to update %s. Replication in progress." % ref)
log("Please try your push again.")
sys.exit(1)
def clobber():
for ref in list_refs():
etcd_write("%s %s" % (config('repogroup'), ref), reporef(ref))
sys.exit(0)
if __name__ == '__main__':
epilog = '''
help: this help
install: Run inside a Git repo to add the hooks and configuration items.
check: Verify correct installation
daemon: Start the piehole daemon. Only one needs to run per host.
clobber: Set the consensus refs to match this repository.
'''
if sys.argv[0] == 'hooks/update':
update()
elif sys.argv[0] == 'hooks/post-update':
post_update()
parser = argparse.ArgumentParser(epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("--repogroup",
help="repogroup to join", default=guess_reponame())
parser.add_argument("--repourl",
help="URL for this repo", default=guess_repourl())
parser.add_argument("--etcdroot",
help="etcd root", default=ETCD_ROOT)
parser.add_argument("--etcdprefix",
help="prefix for etcd keys", default=ETCD_PREFIX)
parser.add_argument("--logfile",
help="file to log to in daemon mode", default="piehole.log")
parser.add_argument("command", choices=['help', 'install', 'check', 'daemon', 'clobber'],
help="command")
args = parser.parse_args()
if args.command == 'daemon':
start_daemon(args.logfile)
elif args.command == 'clobber':
clobber()
elif args.command == 'install':
install(args.repogroup, args.repourl, args.etcdroot, args.etcdprefix)
elif args.command == 'check':
try:
sanity_check()
add_to_repogroup()
except SanityCheckFailure as err:
fail(str(err))
try:
invoke_daemon(reporoot(), 'master', 'ping')
except:
fail("Cannot connect to piehole daemon")
#TODO: check that refs here match etcd
else:
parser.print_help()
#TODO: add commands to let you run piehole from existing hook scripts?
#TODO: have "check" also check that refs are up to date