forked from cms-sw/topic-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildrequests
executable file
·248 lines (236 loc) · 9.08 KB
/
buildrequests
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
#!/usr/bin/env python26
from secrets import cern_secrets as config
from urllib import urlencode
from urlparse import parse_qs
if config.get("debug", False):
import cgitb
cgitb.enable()
import cgi, os, re, sys
from hashlib import sha1
from os.path import join
from json import dumps, loads
from common import debug, call, jsonReply, badRequest, doQuery as _doQuery
from commands import getstatusoutput
from sys import exit
from time import strftime
buildrequestsMapping = [("id", 0),
("pid", 1),
("machine", 2),
("lastModified", 3),
("author", 4),
("payload", 5),
("state", 6),
("url", 7),
("hostnameFilter", 8),
("architecture", 9),
("release", 10),
]
buildrequestMapping = [
("id", 0),
("pid", 1),
("machine", 2),
("lastModified", 3),
("author", 4),
("payload", 5),
("state", 6),
("url", 7),
("hostnameFilter", 8),
("architecture", 9),
("release", 10),
]
def columns(mapping):
return ",".join([k for (k,v) in mapping])
def doQuery(q, mapping = None):
error, output = _doQuery(q, join(config["tccache"], "buildrequests.sql3"))
if not output:
return []
table = [x.split("@@@") for x in output.split("\n")]
if not mapping:
return table
result = []
for r in table:
result += [dict([(k, r[v]) for (k,v) in mapping])]
return result
def getOne(q, mapping = None):
r = doQuery(q, mapping)
if r == []:
badRequest()
return r.pop()
def sanitize(s, ok="a-zA-Z0-9:_./-"):
return re.sub("[.]/", "", re.sub("[^%s]" % ok, "", s))
ALLOWED_USERS=["cmsbuild", "davidlt", "eulisse", "muzaffar"]
BUILD_REQUEST_ADMINS=["cmsbuild", "davidlt", "eulisse", "muzaffar"]
BUILD_REQUEST_BUILDERS=["cmsbuild"]
VALID_UPDATES=["state", "url", "machine", "pid", "lastModified"]
VALID_STATES=["Running", "Pending", "Cancelled", "Stopped", "Failed", "Completed"]
def forbidden():
print 'Status: 403 Forbidden\r\n\r\n\r\n';
exit(0)
if __name__ == "__main__":
user = os.environ.get("ADFS_LOGIN")
if not os.environ.get("ADFS_LOGIN"):
forbidden()
if not user in ALLOWED_USERS:
forbidden()
etag = os.environ.get("HTTP_IF_NONE_MATCH", "")
if not os.path.exists("buildrequests.sql3"):
doQuery("create table if not exists BuildRequests ("
"id integer primary key,"
"release text,"
"architecture text,"
"author text,"
"state text,"
"url text,"
"machine text,"
"pid integer,"
"creation text,"
"lastModified text,"
"payload text,"
"hostnameFilter text,"
"uuid text"
");")
pathInfo = os.environ.get("PATH_INFO", "").strip("/")
requestMethod = os.environ.get("REQUEST_METHOD")
if requestMethod == "GET":
q = parse_qs(os.environ.get("QUERY_STRING", ""))
if not pathInfo:
states = []
if "state" in q:
badStates = [s for s in q["state"][0].split(",") if not s in VALID_STATES]
if badStates:
badRequest()
states = ["BuildRequests.state = \"%s\"" % s for s in q["state"][0].split(",")]
stateFilter = ""
if states:
stateFilter = "WHERE " + " AND ".join(states)
query = "SELECT %s FROM BuildRequests %s;" % (columns(buildrequestsMapping), stateFilter)
results = doQuery("SELECT %s FROM BuildRequests %s;" % (columns(buildrequestsMapping), stateFilter), buildrequestsMapping)
architectureFilter = "architecture_match" in q and q["architecture_match"][0] or ".*"
releaseFilter = "release_match" in q and q["release_match"][0] or ".*"
for r in results:
payload = r.get("payload", "")
r["payload"] = (loads(parse_qs(payload).get("payload", ["{}"])[0]))
results = [r for r in results if re.match(releaseFilter, r.get("release", "UNKNOWN"))]
results = [r for r in results if re.match(architectureFilter, r.get("architecture", "UNKNOWN"))]
jsonReply(results)
elif pathInfo.isdigit():
r = getOne("select %s from BuildRequests where id = %s;" % (columns(buildrequestMapping), pathInfo), buildrequestMapping)
payload = r.get("payload", "")
r["payload"] = (loads(parse_qs(payload).get("payload", ["{}"])[0]))
jsonReply(r)
elif requestMethod == "DELETE":
parts = pathInfo.split(",")
deletable = []
for part in parts:
if part.isdigit():
deletable += [part]
result = []
for d in deletable:
# Only owner or admins can delete a given request
request_id = str(d)
ownerQuery = "SELECT author, state FROM BuildRequests WHERE id = %s;" % request_id
a = doQuery(ownerQuery, [("author", 0), ("state", 1)])
if not len(a) == 1:
result += [{"id": request_id, "deleted": False}]
continue
author = a[0]["author"]
state = a[0]["state"]
# If the state is running, we cancel it.
# If the state is cancelled, we need to wait for being either Failed or Completed.
# Otherwise we can delete if we are either admins or we own the request.
if state == "Running":
result += [{"id": request_id, "deleted": False}]
cancelQuery = "UPDATE BuildRequest state = \"Cancelled\" WHERE id = %s" % request_id
continue
if state == "Cancelled":
result += [{"id": request_id, "deleted": False}]
continue
if (not author in BUILD_REQUEST_ADMINS) and (not author == user):
forbidden()
doQuery("DELETE from BuildRequests WHERE id = %s;" % request_id)
result += [{"id": request_id, "deleted": True}]
jsonReply(result)
elif requestMethod == "PATCH":
if pathInfo.isdigit():
# Only builders or authors can update the requests.
request_id = str(pathInfo)
ownerQuery = "SELECT author FROM BuildRequests WHERE id = %s;" % request_id
a = getOne(ownerQuery)
if not len(a) == 1:
badRequest()
if (not user in BUILD_REQUEST_BUILDERS) and (not user in BUILD_REQUEST_ADMINS) and (not a[0] == user):
forbidden()
try:
data = sys.stdin.read()
data = loads(data)
except:
badRequest()
for (k,v) in data.iteritems():
if not k in VALID_UPDATES:
badRequest()
if k == "state":
if v not in VALID_STATES:
badRequest()
updates = ["%s = \"%s\"" % (k, v) for (k, v) in data.iteritems()]
updateQuery = "UPDATE BuildRequests SET %s WHERE id = %s;" % (", ".join(updates), request_id)
doQuery(updateQuery)
r = getOne("select %s from BuildRequests where id = %s;" % (columns(buildrequestMapping), pathInfo), buildrequestMapping)
payload = r.get("payload", "")
r["payload"] = (loads(parse_qs(payload).get("payload", ["{}"])[0]))
jsonReply(r)
elif requestMethod == "POST":
# We assume additional parameters are passed as JSON.
try:
data = sys.stdin.read()
data = loads(data)
arch = data.get("architecture")
release = data.get("release")
except:
badRequest()
release_series = re.sub("(CMSSW_[0-9]+_[0-9]+).*", "\\1_X", release)
payload = {
"architecture": sanitize(data.get("architecture", "")),
"release": sanitize(data.get("release", "")),
"repository": sanitize(data.get("repository", "cms")),
"PKGTOOLS": sanitize(data.get("PKGTOOLS", "IB/%s/%s" % (release_series, arch)), ok="a-zA-Z0-9./_:-"),
"CMSDIST": sanitize(data.get("CMSDIST", "IB/%s/%s" % (release_series, arch)), ok="a-zA-Z0-9./_:-"),
"ignoreErrors": data.get("ignoreErrors", "") and True or False,
"package": sanitize(data.get("package", "cmssw-tool-conf")),
"continuations": sanitize(data.get("continuations", ""), ok="a-zA-Z0-9_.,:;-"),
"syncBack": data.get("syncBack", "") and True or False,
"debug": data.get("debug", "") and True or False,
"tmpRepository": sanitize(data.get("tmpRepository", os.environ.get("ADFS_LOGIN")), "a-zA-Z0-9.")
}
currentTime = strftime("%Y-%m-%dT%H:%M:%S")
# We calculate a unique ID to avoid inserting twice the same request.
uuid = sha1(payload["release"])
uuid.update(payload["architecture"])
uuid.update(payload["repository"])
uuid.update(payload["PKGTOOLS"])
uuid.update(payload["CMSDIST"])
uuid.update(payload["tmpRepository"])
request = [
("release", payload["release"]),
("architecture", payload["architecture"]),
("author", os.environ.get("ADFS_LOGIN")),
("state", "Pending"),
("url", ""),
("machine", ""),
("pid", ""),
("creation", currentTime),
("lastModified", currentTime),
("hostnameFilter", sanitize(data.get("hostnameFilter", ""), ok="a-zA-Z0-9_.*+-")),
("payload", urlencode({"payload": dumps(payload)})),
("uuid", uuid.hexdigest())
]
query = "INSERT INTO BuildRequests (%s) VALUES (\"%s\");" % (columns(request), "\", \"".join([v for (k,v) in request]))
doQuery(query)
getQuery = "SELECT %s FROM BuildRequests WHERE uuid = \"%s\";" % (columns(buildrequestMapping), uuid.hexdigest())
r = getOne(getQuery, buildrequestMapping)
payload = r.get("payload", "")
r["payload"] = (loads(parse_qs(payload).get("payload", ["{}"])[0]))
jsonReply(r)
else:
print "Status: 501 Not Implemented"
print
exit(1)