forked from ubuntu-si/ubuntu-si
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_builder.py
222 lines (178 loc) · 6.93 KB
/
webhook_builder.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
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
import threading
import logging
import signal
import sys
from datetime import datetime
from pastebin import PastebinAPI
from Queue import Queue
import irc.bot
import irc.strings
try:
from flask import Flask, request
except Exception, e:
print "pip install flask"
try:
import envoy
except Exception, e:
print "pip install envoy"
try:
import json
json_decode = json.loads
except ImportError:
import simplejson
json_decode = simplejson.loads
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
app = Flask(__name__)
zaposlitve = Queue()
class Jobber(threading.Thread):
aktivno = None
def koncaj(self):
zaposlitve.task_done()
self.aktivno = None
def run(self):
while True:
if self.aktivno is None:
delo = zaposlitve.get()
delo.handler = self
self.aktivno = delo
delo.start()
job_queue = Jobber()
class BuildBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
self.do_command(e, e.arguments[0])
def on_pubmsg(self, c, e):
a = e.arguments[0]
if len(a) > 1 and a.startswith("."):
self.do_command(e, a.strip())
return
def sayall(self, msg):
self.connection.privmsg(self.channel, msg)
def do_command(self, e, cmd):
nick = e.source.nick
chan = e.target
if '.isostatus' in cmd:
if job_queue.aktivno is None:
self.connection.privmsg(nick, 'Mirujem')
self.connection.privmsg(nick, 'Prenos: (32bit) http://185.14.186.232/dist32/binary-hybrid.iso [.torent|.zsync]')
self.connection.privmsg(nick, 'Prenos: (64bit) http://185.14.186.232/dist/binary-hybrid.iso [.torent|.zsync]')
else:
self.connection.privmsg(nick, 'Delam')
for res in job_queue.aktivno.history:
self.connection.privmsg(nick, res)
if "dz0ny" == nick:
if '.isobuild32' in cmd:
new_build = Delo('32')
zaposlitve.put(new_build)
self.connection.privmsg(nick, u'Začel z izgradnjo 32bit iso')
if '.isobuild64' in cmd:
new_build = Delo('64')
zaposlitve.put(new_build)
self.connection.privmsg(nick, u'Začel z izgradnjo 64bit iso')
def irc_notify(self, thread, point, text=""):
url = ""
if len(text) > 1:
url = PastebinAPI().paste("94c4c6e3faf2271b9175bb7601738b79", text)
if len(url) > 1:
msg = "Napaka pri gradnji > {0}: {1} {2}".format(thread.encode("utf-8"), point.encode("utf-8"), url.encode("utf-8"))
else:
msg = "{0}: {1}".format(thread.encode("utf-8"), point.encode("utf-8"))
print msg
self.sayall(msg)
bot = BuildBot('#ubuntu-si', "Owcica", 'irc.freenode.net')
class Delo(threading.Thread):
"""Delo je abstrakcija make build zadeve"""
def __init__(self, flavor):
self.flavor = flavor
self.history = []
self.start_time = datetime.now()
threading.Thread.__init__(self)
self.name = "Delo-build-" + flavor
def run(self):
self.build_all()
return
def check_and_report(self, cmd):
self.delta = datetime.now() - self.start_time
if cmd.status_code is 0:
self.history.append(u"Command: {0} ✓ ({1})".format(
" ".join(cmd.command),
str(self.delta)
))
return True
else:
bot.irc_notify(self.getName(), " ".join(cmd.command), 'Napaka: {0}\nLog: {1}'.format(
cmd.std_err,
cmd.std_out
))
self.history.append(u"Command: {0} ✗ ({1})".format(
" ".join(cmd.command),
str(self.delta)
))
return False
def build_all(self):
git = envoy.run("git pull")
if self.check_and_report(git):
deb = envoy.run("make deb" + self.flavor)
if self.check_and_report(deb):
iso = envoy.run("make iso" + self.flavor)
if self.check_and_report(iso):
dist = envoy.run("make dist" + self.flavor)
self.check_and_report(dist)
self.delta = datetime.now() - self.start_time
bot.irc_notify(self.getName(), "Izgradnja je trajala {0}".format(str(self.delta)))
if self.flavor is "32":
self.sayall('Prenos: (32bit) http://185.14.186.232/dist32/binary-hybrid.iso [.torent|.zsync]')
else:
self.sayall('Prenos: (64bit) http://185.14.186.232/dist/binary-hybrid.iso [.torent|.zsync]')
if self.handler:
self.handler.koncaj()
@app.route("/web_hook/<flavor>", methods=['POST'])
def web_hook(flavor):
if flavor in ["64", "32"]:
logging.debug(flavor)
if request.remote_addr in ["207.97.227.253", "50.57.128.197",
"108.171.174.178", "50.57.231.61",
"127.0.0.1"]:
if request.form["payload"]:
payload = request.form["payload"]
payload = json_decode(payload)
try:
msg = "Nov commit[" + flavor + "]: " + payload["head_commit"]["committer"]["username"] + u": " + payload["head_commit"]["message"] + " " + payload["head_commit"]["url"] + " @ " + payload["head_commit"]["timestamp"]
print msg
bot.sayall(msg)
except Exception, e:
print e
should_build = False
try:
for x in payload["head_commit"]["modified"]:
if "si-ubuntu-defaults" in x:
should_build = True
except Exception, e:
should_build = True
print e
if should_build:
new_build = Delo(flavor)
zaposlitve.put(new_build)
return "OK"
return "FAIL"
class BotThread(threading.Thread):
def run(self):
bot.start()
if __name__ == "__main__":
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
try:
job_queue.start()
BotThread().start()
app.run(host='0.0.0.0')
except KeyboardInterrupt:
sys.exit(0)