Repository navigation
Expand file tree
/
Copy pathBuilds.java
More file actions
276 lines (256 loc) · 12.6 KB
/
Copy pathBuilds.java
File metadata and controls
276 lines (256 loc) · 12.6 KB
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
package com.waytta;
import java.io.IOException;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import hudson.Launcher;
import hudson.model.TaskListener;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class Builds {
public static void addArgumentsToSaltFunction(String myarguments, JSONObject saltFunc) {
if (myarguments.length() > 0) {
// Pattern match to detect anything between single and double quotes and drop it
// If anything is left that matches an equals sign, save
// This means a non-null regex match for group 1 is an equals sign outside of quotes
// and thus a kwarg
String equalsPattern = "\\\\\"|\\\\'|\"(?:\\\\\"|[^\"])*\"|'(?:\\\\'|[^'])*'|(=)";
Pattern p = Pattern.compile(equalsPattern);
JSONObject fullKwJSON = new JSONObject();
// spit on space separated not inside of single and double quotes
String[] argItems = myarguments.split("\\s+(?=(?:[^'\"]|'[^']*'|\"[^\"]*\")*$)");
for (String arg : argItems) {
// remove spaces at beginning or end
arg = arg.replaceAll("^\\s+|\\s+$", "");
// Look for any = not inside quotes
Boolean isKW = false;
Matcher m = p.matcher(arg);
while (m.find()) {
if (StringUtils.isNotEmpty(m.group(1))) {
// Found non-null match on group 1, assuming kwarg
isKW = true;
}
}
// if string wrapped in quotes, remove them since adding to list
// re-quotes
arg = arg.replaceAll("(^')|(^\")|('$)|(\"$)", "");
if (isKW) {
String[] kwString = arg.split("=");
if (kwString.length > 2) {
// kwarg contained more than one =. Let's put the string
// back together
String kwFull = "";
for (String kwItem : kwString) {
// Ignore the first item as it will remain the key
if (kwItem.equals(kwString[0])) {
continue;
}
// add the second item
if (kwItem.equals(kwString[1])) {
// again remove any wrapping quotes
kwItem = kwItem.replaceAll("(^')|(^\")|('$)|(\"$)", "");
kwFull += kwItem;
continue;
}
// add all other items with an = to rejoin
kwFull += "=" + kwItem;
}
fullKwJSON.put(kwString[0], kwFull);
} else {
// again remove any wrapping quotes
kwString[1] = kwString[1].replaceAll("(^')|(^\")|('$)|(\"$)", "");
try {
// If value was already a jsonobject, treat it as such
JSON kwJSON = JSONSerializer.toJSON(kwString[1]);
// Rejoin key and json object
fullKwJSON.element(kwString[0], kwJSON);
} catch (JSONException e) {
// Otherwise it must have been a string
if (isInteger(kwString[1])) {
fullKwJSON.put(kwString[0], Integer.parseInt(kwString[1]));
} else {
fullKwJSON.put(kwString[0], kwString[1]);
}
}
}
} else {
// Add any args to json message
if (isInteger(arg)) {
// if arg is int, add as int
saltFunc.accumulate("arg", Integer.parseInt(arg));
} else {
// otherwise assume string
saltFunc.accumulate("arg", arg);
}
}
}
// Now that loops have completed, add kwarg object
if (!fullKwJSON.isEmpty()) {
saltFunc.element("kwarg", fullKwJSON);
}
}
}
private static boolean isInteger(String s) {
boolean isInteger = false;
try {
Integer.parseInt(s);
isInteger = true;
}
catch (NumberFormatException ex) {
// s is not an integer
}
return isInteger;
}
public static int returnedMinions(JSONArray saltReturn) {
int numMinionsDone = 0;
JSONObject resultObject = new JSONObject();
resultObject = saltReturn.getJSONObject(0).getJSONObject("Result");
// Check Result for number of responses
Iterator<?> rMinionKeys = resultObject.keys();
while( rMinionKeys.hasNext() ) {
String key = (String)rMinionKeys.next();
if ( resultObject.get(key) instanceof JSONObject ) {
numMinionsDone += 1;
}
}
return numMinionsDone;
}
public static JSONArray returnData(JSONObject saltReturn, String netapi) {
JSONArray returnArray = new JSONArray();
if (netapi.contains("TornadoServer")) {
// Tornado is keyed with return
returnArray = saltReturn.getJSONArray("return");
} else {
// cherrypy is keyed with info
returnArray = saltReturn.getJSONArray("info");
}
return returnArray;
}
public static String getBlockingBuildJid(Launcher launcher, String myservername,
String token, JSONObject saltFunc, TaskListener listener)
throws IOException, InterruptedException, SaltException {
String jid = null;
// Send request to /minion url. This will give back a jid which we
// will need to poll and lookup for completion
JSONObject httpResponse = (JSONObject) JSONSerializer.toJSON(launcher.getChannel().call(new HttpCallable(myservername + "/minions", saltFunc, token)));
JSONArray returnArray = httpResponse.getJSONArray("return");
for (Object o : returnArray) {
JSONObject line = (JSONObject) o;
if (line.isEmpty() ) {
throw new SaltException("Missing JID: No minions matched target");
}
jid = line.getString("jid");
}
// Print out success
listener.getLogger().println("Running jid: " + jid);
return jid;
}
public static JSONArray checkBlockingBuild(Launcher launcher, String myservername, String token,
JSONObject saltFunc, TaskListener listener, int pollTime, int minionTimeout, String netapi, String jid)
throws IOException, InterruptedException, SaltException {
// Request successfully sent. Now use jid to check if job complete
int numMinions = 0;
int numMinionsDone = 0;
JSONArray minionsArray = new JSONArray();
JSONArray httpArray = new JSONArray();
JSONArray returnArray = new JSONArray();
JSONObject httpResponse = new JSONObject();
httpResponse = (JSONObject) JSONSerializer.toJSON(launcher.getChannel().call(new HttpCallable(myservername + "/jobs/" + jid, null, token)));
httpArray = returnData(httpResponse, netapi);
for (Object o : httpArray) {
JSONObject line = (JSONObject) o;
minionsArray = line.getJSONArray("Minions");
}
// Check Minions array for number of targets minions
numMinions = minionsArray.size();
listener.getLogger().println("Waiting for " + numMinions + " minions");
numMinionsDone = returnedMinions(httpArray);
listener.getLogger().println(numMinionsDone + " minions are done");
// Now that we know how many minions have responded, and how many we
// are waiting on. Let's see more have finished
if (numMinionsDone < numMinions) {
// Don't print annoying messages unless we really are waiting for
// more minions to return
listener.getLogger().println(
"Will check status every " + pollTime + " seconds...");
}
while (numMinionsDone < numMinions) {
try {
Thread.sleep(pollTime * 1000L);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// Allow user to cancel job in jenkins interface
throw new InterruptedException();
}
httpResponse = (JSONObject) JSONSerializer.toJSON(launcher.getChannel().call(new HttpCallable(myservername + "/jobs/" + jid, null, token)));
httpArray = returnData(httpResponse, netapi);
numMinionsDone = returnedMinions(httpArray);
// if minionTimeout is negative, still walkthrough the timeout process, but do not fail build
boolean timeoutFail = true;
if ( minionTimeout < 0) {
timeoutFail = false;
minionTimeout *= -1;
}
if (numMinionsDone > 0 && numMinionsDone < numMinions) {
// Some minions returned, but not all
// Give them minionTimeout to all return or fail build
try {
listener.getLogger().println(
"Some minions returned. Waiting " + minionTimeout + " seconds");
int numberChecks = minionTimeout / pollTime;
int numberChecksRemain = minionTimeout % pollTime;
// Check every pollTime seconds until minionTimeout.
for (int i=0; i<numberChecks; i++) {
httpResponse = (JSONObject) JSONSerializer.toJSON(launcher.getChannel().call(new HttpCallable(myservername + "/jobs/" + jid, null, token)));
httpArray = returnData(httpResponse, netapi);
numMinionsDone = returnedMinions(httpArray);
if (numMinionsDone >= numMinions) {
returnArray.clear();
returnArray.add(httpArray);
return returnArray;
}
Thread.sleep(pollTime * 1000L);
}
if (numberChecksRemain > 0) {
// If on last iteration of loop, sleep remainder between
// pollTime and minionTimeout to fully reach minionTimeout
Thread.sleep(numberChecksRemain * 1000L);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
// Allow user to cancel job in jenkins interface
throw new InterruptedException();
}
httpResponse = (JSONObject) JSONSerializer.toJSON(launcher.getChannel().call(new HttpCallable(myservername + "/jobs/" + jid, null, token)));
httpArray = returnData(httpResponse, netapi);
numMinionsDone = returnedMinions(httpArray);
if (numMinionsDone < numMinions) {
JSONArray respondedMinions = new JSONArray();
Iterator<?> rMinionKeys = httpArray.getJSONObject(0).getJSONObject("Result").keys();
while( rMinionKeys.hasNext() ) {
String key = (String)rMinionKeys.next();
respondedMinions.add(key);
}
for (int i = 0; i < respondedMinions.size(); ++i) {
minionsArray.discard(respondedMinions.get(i));
}
listener.error(
"Minions timed out:\n" + minionsArray.toString() + "\n\n");
if (timeoutFail) {
throw new SaltException(httpArray.getJSONObject(0).getJSONObject("Result").toString());
}
returnArray.clear();
returnArray.add(httpArray.getJSONObject(0).getJSONObject("Result"));
return returnArray;
}
}
}
returnArray.clear();
returnArray.add(httpArray.getJSONObject(0).getJSONObject("Result"));
return returnArray;
}
}