-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpushycatd
executable file
·78 lines (55 loc) · 1.81 KB
/
pushycatd
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
#!/usr/bin/env python
import pushycat.config
import pushycat.http_listener
import pushycat.client
import os
import pwd
import sys
import argparse
def setup(conf):
"""Configure clients and http listener"""
clients = {}
listener = pushycat.http_listener.HttpListener(conf.listen(), conf.path())
for hook in conf.hooks():
user = hook["user"]
repository = hook["repository"]
branch = hook["branch"]
run = hook["run"]
c = clients.get(user, None)
if c is None:
c = clients[user] = pushycat.client.Client(user, os.pipe())
c.add(repository, branch, run)
listener.add(
repository,
branch,
lambda sha, c=c, r=repository, b=branch: c.notify(r, b, sha))
return (listener, clients.values())
def chuser(username):
"""Change the uid/gid of the current process to that of given the username"""
uid = pwd.getpwnam(username).pw_uid
if uid != 0:
os.setgid(uid)
os.setuid(uid)
def run(confpath):
"""Launch webserver and clients, and wait forever"""
conf = pushycat.config.Config(confpath)
listener, clients = setup(conf)
if os.fork() == 0:
chuser(conf.user())
listener.run()
sys.exit(-1)
for client in clients:
if os.fork() == 0:
chuser(client.user)
client.run()
sys.exit(-1)
for i in range(0, 1 + len(clients)):
os.wait()
def main():
parser = argparse.ArgumentParser(
description='webservice that listens for github webhooks and executes arbitrary commands when triggered.')
parser.add_argument('--conf', default=pushycat.config.CONFIG_PATH)
options = parser.parse_args()
run(options.conf)
if __name__ == "__main__":
main()