-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkinsLamp.js
346 lines (300 loc) · 9.51 KB
/
jenkinsLamp.js
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var config = require('./config.json');
var async = require("async");
var clear = require('clear');
const util = require('util');
const http = require('http');
const https = require('https');
const jp = require('jsonpath');
const tfunk = require("tfunk");
const pad = require("pad");
const JenkinsState = require('./model.js').JenkinsState;
const LampState = require('./model.js').LampState;
const JobState = require('./model.js').JobState;
// config data and lamp status
const LampData = require('./model.js').LampData;
const Job = require('./model.js').Job;
clear();
// config
var delay = getConfigValue(config.jenkins.delay, 60000)
var useLibrary = getConfigValue(config.jenkins.useLibrary, "pcLamp.js")
console.log('=> delay is ' + delay + 'ms');
console.log('=> use the output library: ' + useLibrary);
// select library
var Lamp = require("./" + useLibrary);
function getConfigValue(value, defaultValue) {
if (value === undefined) {
return defaultValue;
};
return value;
}
function JenkinsLamp() {
var jenkinsData,
lampState,
firstLoop,
lampList; // list of lamps, one lamp is a group of green, orange and red
// initialize data
this.jenkinsData = {};
this.lampData = new LampData();
this.firstLoop = true;
this.lampList = new Array();
for (lamp of config.lamps) {
this.lampList.push(this.initLamp(lamp));
}
// initialize Lamp
this.Lamp = new Lamp();
this.Lamp.startup();
this.initShutdown();
};
JenkinsLamp.prototype.test = function(value) {
console.log('from test function: ' + value);
};
JenkinsLamp.prototype.initLamp = function(lampConfig) {
let lampData = new LampData();
lampData.id = lampConfig.id;
lampData.name = lampConfig.name;
lampData.enabled = lampConfig.enabled;
lampData.job = new Job();
lampData.job.type = lampConfig.job.type;
lampData.job.path = lampConfig.job.path;
lampData.job.ignore = lampConfig.job.ignore;
lampData.job.colorJsonPath = lampConfig.job.colorJsonPath;
console.log('Initialize the lamp: ' + lampConfig.name);
return lampData;
};
JenkinsLamp.prototype.initShutdown = function() {
// initialize the shutdown process
let Lamp = this.Lamp;
process.on('SIGINT', function() {
console.log('');
Lamp.shutdown();
console.log('Bye, bye!');
process.exit();
});
};
JenkinsLamp.prototype.workNotParallel = function() {
for (lampData of this.lampList) {
if (lampData.enabled) {
this.callJenkins(lampData, function(err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
self.displayLamps(self.lampList);
self.displayOutput(self.lampList);
}
});
}
}
// loop
setTimeout(jenkinsLamp.work, this.delay);
};
JenkinsLamp.prototype.work = function() {
let self = this;
async.forever(
function(next) {
try {
if (self.firstLoop) {
self.firstLoop = false;
} else {
clear();
}
async.each(self.lampList.filter(function(lamp){
return lamp.enabled;
}), self.callJenkins, function(err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
self.displayLamps(self.lampList);
self.displayOutput(self.lampList);
}
});
setTimeout(function() {
next();
}, delay);
} catch (err) {
console.error('Error in the "next" from async.forever:');
console.error(err);
}
},
function(err) {
console.error('Error in error function from async.forever:');
console.error(err);
}
);
};
JenkinsLamp.prototype.displayLamps = function(lampList) {
for (lamp of lampList) {
if (lamp.id === 'dev. job') {
lamp.red === LampState.ON ? this.Lamp.enableRed() : this.Lamp.disableRed();
lamp.orange === LampState.ON ? this.Lamp.enableOrange() : this.Lamp.disableOrange();
lamp.green === LampState.ON ? this.Lamp.enableGreen() : this.Lamp.disableGreen();
}
}
}
JenkinsLamp.prototype.displayOutput = function(lampList) {
var lines = [];
lines[0] = '';
lines[1] = '';
lines[2] = '';
lines[3] = '';
lines[4] = '';
lines[5] = '';
lines[6] = '';
lines[7] = '';
console.log('' + lines[0]);
for (lamp of lampList) {
let jobId = pad(lamp.id, 11, {"strip":true}) + ' ';
if (lamp.red === LampState.ON) {
lines[0] += '{bgRed:' + jobId + '}';
} else if (lamp.red === LampState.ON) {
lines[0] += '{bgOrange:' + jobId + '}';
} else {
lines[0] += jobId;
}
lines[1] += ' ˏ__ˎ ';
if (lamp.red === LampState.OFF) {
lines[2] += ' |○ | ';
} else {
lines[2] += ' {red:|◉ |} ';
}
if (lamp.orange === LampState.OFF) {
lines[3] += ' |○ | ';
} else {
lines[3] += ' {yellow:|◉ |} ';
}
if (lamp.green === LampState.OFF) {
lines[4] += ' |○ | ';
} else {
lines[4] += ' {green:|◉ |} ';
}
lines[5] += ' \\ˉˉ/ ';
lines[6] += ' ˏ⎞⎛ˎ ';
}
console.log(tfunk(lines[0]));
console.log(tfunk(lines[1]));
console.log(tfunk(lines[2]));
console.log(tfunk(lines[3]));
console.log(tfunk(lines[4]));
console.log(tfunk(lines[5]));
console.log(tfunk(lines[6]));
}
JenkinsLamp.prototype.callJenkins = function(lamp, callback) {
console.log('call: ' + config.jenkins.host + ':' + config.jenkins.port + lamp.job.path);
let options = {
host: config.jenkins.host,
port: config.jenkins.port,
path: lamp.job.path,
headers: {
'Authorization': 'Basic ' + new Buffer(config.jenkins.user + ':' + config.jenkins.password).toString('base64')
}
};
http.get(options, (res) => {
res.on('data', (d) => {
try {
//console.log(d.toString());
let jsonData = JSON.parse(d);
console.log('----------------------------');
console.log('call ' + lamp.id + ' : "' + lamp.name + '"');
let colorResults = jp.query(jsonData, lamp.job.colorJsonPath);
colorResults = jenkinsLamp.filterJenkinsJobs(colorResults, lamp.job.ignore);
for (colorResult of colorResults) {
if (colorResult && colorResult.name) {
console.log(tfunk(colorResult.name + ' : {' + colorResult.color + ':' + colorResult.color + '}'));
} else {
console.log(tfunk('{green:' + colorResults + '}'));
}
}
switch (lamp.job.type) {
case JobState.CONDITION:
jenkinsLamp.updateJenkinsStateFromCondition(lamp, colorResults);
break;
default:
jenkinsLamp.updateJenkinsStateFromColor(lamp, colorResults);
}
callback();
} catch (err) {
console.error('Error in the "callJenkins.on" method:');
console.error(err);
}
});
}).on('error', (e) => {
console.error('Error in the "callJenkins" method:');
console.error(e);
// throw e;
});
};
JenkinsLamp.prototype.updateJenkinsStateFromCondition = function(lamp, colorResults) {
// console.log('updateJenkinsStateFromCondition:');
// console.log(colorResults);
// disable all lamps
lamp.red = LampState.OFF;
lamp.orange = LampState.OFF;
lamp.green = LampState.OFF;
if (colorResults[0] && colorResults[0].indexOf('failed') >= 0) {
lamp.red = LampState.ON;
} else {
lamp.green = LampState.ON;
}
};
JenkinsLamp.prototype.updateJenkinsStateFromColor = function(lamp, colorResults) {
//console.log('updateJenkinsStateFromColor');
//console.log(colorResults);
// disable all lamps
lamp.red = LampState.OFF;
lamp.orange = LampState.OFF;
lamp.green = LampState.OFF;
// console.log('************************');
// console.log(colorResults);
// red lamp is on, if one of the jobs has a red or red_anime state
for (let colorResult of colorResults) {
if (this.getColor(colorResult).startsWith('red')
|| this.getColor(colorResult).startsWith('aborted')) {
lamp.red = LampState.ON;
break;
}
}
// orange lamp is on, if one jobs has yellow state and nobody has red state
if (lamp.red === LampState.OFF) {
for (let colorResult of colorResults) {
if (this.getColor(colorResult).startsWith('yellow')) {
lamp.orange = LampState.ON;
break;
}
}
}
// green lamp is on, if the other lamps are off
if (lamp.red === LampState.OFF && lamp.orange === LampState.OFF) {
lamp.green = LampState.ON;
}
// orange lamp is also on, if one of the jobs color ends with '_anime'
for (colorResult of colorResults) {
if (this.getColor(colorResult).endsWith('_anime')) {
lamp.orange = LampState.ON;
break;
}
}
}
JenkinsLamp.prototype.getColor = function(colorResult) {
if (typeof colorResult === 'string') {
return colorResult;
}
return colorResult.color;
}
JenkinsLamp.prototype.filterJenkinsJobs = function(colorResults, ignores) {
return colorResults.filter(function(colorResult){
if(colorResult) {
return !ignores.includes(colorResult.name);
}
return true;
});
}
// start jenkins Lamp
var jenkinsLamp = new JenkinsLamp();
jenkinsLamp.work();