corrade-lsl-templates – Blame information for rev 42

Subversion Repositories:
Rev:
Rev Author Line No. Line
42 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that builds Jenkins jobs.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return "";
15 list a = llParseStringKeepNulls(data, ["&", "="], []);
16 integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
17 if(i != -1) return llList2String(a, 2*i+1);
18 return "";
19 }
20  
21 ///////////////////////////////////////////////////////////////////////////
22 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
23 ///////////////////////////////////////////////////////////////////////////
24 string wasKeyValueEncode(list data) {
25 list k = llList2ListStrided(data, 0, -1, 2);
26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
27 data = [];
28 do {
29 data += llList2String(k, 0) + "=" + llList2String(v, 0);
30 k = llDeleteSubList(k, 0, 0);
31 v = llDeleteSubList(v, 0, 0);
32 } while(llGetListLength(k) != 0);
33 return llDumpList2String(data, "&");
34 }
35  
36 ///////////////////////////////////////////////////////////////////////////
37 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
38 ///////////////////////////////////////////////////////////////////////////
39 // http://was.fm/secondlife/wanderer
40 vector wasCirclePoint(float radius) {
41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
42 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
43 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
44 return <x, y, 0>;
45 return wasCirclePoint(radius);
46 }
47  
48 ///////////////////////////////////////////////////////////////////////////
49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
50 ///////////////////////////////////////////////////////////////////////////
51 // escapes a string in conformance with RFC1738
52 string wasURLEscape(string i) {
53 string o = "";
54 do {
55 string c = llGetSubString(i, 0, 0);
56 i = llDeleteSubString(i, 0, 0);
57 if(c == "") jump continue;
58 if(c == " ") {
59 o += "+";
60 jump continue;
61 }
62 if(c == "\n") {
63 o += "%0D" + llEscapeURL(c);
64 jump continue;
65 }
66 o += llEscapeURL(c);
67 @continue;
68 } while(i != "");
69 return o;
70 }
71  
72 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
74 ///////////////////////////////////////////////////////////////////////////
75 list wasCSVToList(string csv) {
76 list l = [];
77 list s = [];
78 string m = "";
79 do {
80 string a = llGetSubString(csv, 0, 0);
81 csv = llDeleteSubString(csv, 0, 0);
82 if(a == ",") {
83 if(llList2String(s, -1) != "\"") {
84 l += m;
85 m = "";
86 jump continue;
87 }
88 m += a;
89 jump continue;
90 }
91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
92 m += a;
93 csv = llDeleteSubString(csv, 0, 0);
94 jump continue;
95 }
96 if(a == "\"") {
97 if(llList2String(s, -1) != a) {
98 s += a;
99 jump continue;
100 }
101 s = llDeleteSubList(s, -1, -1);
102 jump continue;
103 }
104 m += a;
105 @continue;
106 } while(csv != "");
107 // postcondition: length(s) = 0
108 return l + m;
109 }
110  
111 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) {
115 list v = [];
116 do {
117 string a = llDumpList2String(
118 llParseStringKeepNulls(
119 llList2String(
120 l,
121  
122 ),
123 ["\""],
124 []
125 ),
126 "\"\""
127 );
128 if(llParseStringKeepNulls(
129 a,
130 [" ", ",", "\n", "\""], []
131 ) !=
132 (list) a
133 ) a = "\"" + a + "\"";
134 v += a;
135 l = llDeleteSubList(l, 0, 0);
136 } while(l != []);
137 return llDumpList2String(v, ",");
138 }
139  
140 ///////////////////////////////////////////////////////////////////////////
141 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
142 ///////////////////////////////////////////////////////////////////////////
143 // unescapes a string in conformance with RFC1738
144 string wasURLUnescape(string i) {
145 return llUnescapeURL(
146 llDumpList2String(
147 llParseString2List(
148 llDumpList2String(
149 llParseString2List(
150 i,
151 ["+"],
152 []
153 ),
154 " "
155 ),
156 ["%0D%0A"],
157 []
158 ),
159 "\n"
160 )
161 );
162 }
163  
164 ///////////////////////////////////////////////////////////////////////////
165 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
166 ///////////////////////////////////////////////////////////////////////////
167 list wasSetIntersect(list a, list b) {
168 if(llGetListLength(a) == 0) return [];
169 string i = llList2String(a, 0);
170 a = llDeleteSubList(a, 0, 0);
171 if(llListFindList(b, (list)i) == -1)
172 return wasSetIntersect(a, b);
173 return i + wasSetIntersect(a, b);
174 }
175  
176 // configuration data
177 string configuration = "";
178 // store message over state.
179 string data = "";
180 // caller
181 string firstname = "";
182 string lastname = "";
183  
184 default {
185 state_entry() {
186 llOwnerSay("[Jenkins] Starting...");
187 llSetTimerEvent(10);
188 }
189 link_message(integer sender, integer num, string message, key id) {
190 if(id != "configuration") return;
191 llOwnerSay("[Jenkins] Got configuration...");
192 configuration = message;
193 state listen_group;
194 }
195 timer() {
196 llOwnerSay("[Jenkins] Requesting configuration...");
197 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
198 }
199 on_rez(integer num) {
200 llResetScript();
201 }
202 changed(integer change) {
203 if((change & CHANGED_INVENTORY) ||
204 (change & CHANGED_REGION_START) ||
205 (change & CHANGED_OWNER)) {
206 llResetScript();
207 }
208 }
209 state_exit() {
210 llSetTimerEvent(0);
211 }
212 }
213  
214 state listen_group {
215 state_entry() {
216 // DEBUG
217 llOwnerSay("[Jenkins] Waiting for group messages...");
218 }
219 link_message(integer sender, integer num, string message, key id) {
220 // We only care about notifications now.
221 if(id != "notification")
222 return;
223  
224 // This script only processes group notifications.
225 if(wasKeyValueGet("type", message) != "group" ||
226 (wasKeyValueGet("type", message) == "group" &&
227 wasURLUnescape(wasKeyValueGet("group", message)) !=
228 wasKeyValueGet("group", configuration)))
229 return;
230  
231 // Get the sent message.
232 data = wasURLUnescape(
233 wasKeyValueGet(
234 "message",
235 message
236 )
237 );
238  
239 // Check if this is an eggdrop command.
240 if(llGetSubString(data, 0, 0) !=
241 wasKeyValueGet("command", configuration))
242 return;
243  
244 // Check if the command matches the current module.
245 list command = llParseString2List(data, [" "], []);
246 if(llList2String(command, 0) !=
247 wasKeyValueGet("command", configuration) + "jenkins")
248 return;
249  
250 // Remove command.
251 command = llDeleteSubList(command, 0, 0);
252  
253 firstname = wasKeyValueGet("firstname", message);
254 lastname = wasKeyValueGet("lastname", message);
255  
256 if(firstname == "" || lastname == "") {
257 data = "And who would yarr be?";
258 state tell;
259 }
260  
261 // Dump the rest of the message.
262 data = wasURLUnescape(llDumpList2String(command, " "));
263 if(data == "") {
264 data = "Jenkins job name is required.";
265 state tell;
266 }
267  
268 // Get roles of caller.
269 state get_caller_roles;
270 }
271 on_rez(integer num) {
272 llResetScript();
273 }
274 changed(integer change) {
275 if((change & CHANGED_INVENTORY) ||
276 (change & CHANGED_REGION_START) ||
277 (change & CHANGED_OWNER)) {
278 llResetScript();
279 }
280 }
281 }
282  
283 state get_caller_roles {
284 state_entry() {
285 // DEBUG
286 llOwnerSay("[Jenkins] Searching for caller...");
287 llInstantMessage(
288 wasKeyValueGet(
289 "corrade",
290 configuration
291 ),
292 wasKeyValueEncode(
293 [
294 "command", "getmemberroles",
295 "group", wasURLEscape(
296 wasKeyValueGet(
297 "group",
298 configuration
299 )
300 ),
301 "password", wasURLEscape(
302 wasKeyValueGet(
303 "password",
304 configuration
305 )
306 ),
307 "firstname", firstname,
308 "lastname", lastname,
309 "callback", wasURLEscape(
310 wasKeyValueGet(
311 "URL",
312 configuration
313 )
314 )
315 ]
316 )
317 );
318  
319 llSetTimerEvent(60);
320 }
321 link_message(integer sender, integer num, string body, key id) {
322 // Only process callbacks for the database command.
323 if(id != "callback" || wasKeyValueGet("command", body) != "getmemberroles")
324 return;
325  
326 if(wasKeyValueGet("command", body) != "getmemberroles" ||
327 wasKeyValueGet("success", body) != "True") {
328 // DEBUG
329 llOwnerSay("[Jenkins] Unable to get member roles: " +
330 wasURLUnescape(
331 wasKeyValueGet("error", body)
332 )
333 );
334 state listen_group;
335 }
336  
337 // Dump the roles to a list.
338 list roles = wasCSVToList(
339 wasURLUnescape(
340 wasKeyValueGet("data", body)
341 )
342 );
343  
344 if(llGetListLength(
345 wasSetIntersect(roles,
346 wasCSVToList(
347 wasKeyValueGet(
348 "admin roles", configuration
349 )
350 )
351 )
352 ) == 0) {
353 data = "You ain't got the cojones!";
354 state tell;
355 }
356  
357 state jenkins;
358 }
359 timer() {
360 state listen_group;
361 }
362 on_rez(integer num) {
363 llResetScript();
364 }
365 changed(integer change) {
366 if((change & CHANGED_INVENTORY) ||
367 (change & CHANGED_REGION_START) ||
368 (change & CHANGED_OWNER)) {
369 llResetScript();
370 }
371 }
372 state_exit() {
373 llSetTimerEvent(0);
374 }
375 }
376  
377 state jenkins {
378 state_entry() {
379 string jenkins_url = "http://" + wasKeyValueGet(
380 "jenkins user",
381 configuration
382 ) + ":" + wasKeyValueGet(
383 "jenkins password",
384 configuration
385 ) + "@" + wasKeyValueGet(
386 "jenkins host",
387 configuration
388 ) + "/job/" + data + "/build";
389 // DEBUG
390 llOwnerSay("[Jenkins] Queuing Jenkins CI build to URL: " + jenkins_url);
391 llInstantMessage(
392 wasKeyValueGet(
393 "corrade",
394 configuration
395 ),
396 wasKeyValueEncode(
397 [
398 "command", "HTTP",
399 "group", wasURLEscape(
400 wasKeyValueGet(
401 "group",
402 configuration
403 )
404 ),
405 "password", wasURLEscape(
406 wasKeyValueGet(
407 "password",
408 configuration
409 )
410 ),
411 "action", "post",
412 "type", "text",
413 // "https://" + "user:pass" + "@" + jenkins CI host + "/job/" + job name + "/build"
414 "URL", wasURLEscape(jenkins_url),
415 "callback", wasURLEscape(
416 wasKeyValueGet(
417 "URL",
418 configuration
419 )
420 )
421 ]
422 )
423 );
424 llSetTimerEvent(60);
425 }
426 link_message(integer sender, integer num, string body, key id) {
427 // Only process callbacks for the database command.
428 if(id != "callback" || wasKeyValueGet("command", body) != "HTTP")
429 return;
430  
431 if(wasKeyValueGet("command", body) != "HTTP" ||
432 wasKeyValueGet("success", body) != "True") {
433 // DEBUG
434 llOwnerSay("[Jenkins] Unable to POST to Jenkins: " +
435 wasURLUnescape(
436 wasKeyValueGet("error", body)
437 )
438 );
439 state listen_group;
440 }
441  
442 data = "Job build scheduled!";
443  
444 state tell;
445 }
446 timer() {
447 state listen_group;
448 }
449 on_rez(integer num) {
450 llResetScript();
451 }
452 changed(integer change) {
453 if((change & CHANGED_INVENTORY) ||
454 (change & CHANGED_REGION_START) ||
455 (change & CHANGED_OWNER)) {
456 llResetScript();
457 }
458 }
459 state_exit() {
460 llSetTimerEvent(0);
461 }
462 }
463  
464 state tell {
465 state_entry() {
466 // DEBUG
467 llOwnerSay("[Jenkins] Sending to group.");
468 llInstantMessage(
469 wasKeyValueGet(
470 "corrade",
471 configuration
472 ),
473 wasKeyValueEncode(
474 [
475 "command", "tell",
476 "group", wasURLEscape(
477 wasKeyValueGet(
478 "group",
479 configuration
480 )
481 ),
482 "password", wasURLEscape(
483 wasKeyValueGet(
484 "password",
485 configuration
486 )
487 ),
488 "entity", "group",
489 "message", wasURLEscape(data)
490 ]
491 )
492 );
493  
494 state listen_group;
495 }
496 }