/
cli.js
192 lines (175 loc) · 4.97 KB
/
cli.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
var _ = require('underscore'),
bag = require('bagofholding'),
Jenkins = require('./jenkins');
/**
* Execute nestor using JENKINS_URL environment variable.
* Nestor uses http_proxy environment variable when it's available.
*/
function exec() {
var url = process.env.JENKINS_URL,
proxy = process.env.http_proxy;
function _build() {
return function (jobName, params, args) {
if (!args) {
args = params || {};
}
var message = 'Job %s was started successfully',
cb;
if (args.console) {
cb = function (err, result) {
if (err) {
bag.cli.exit(err, result);
} else {
console.log(message, jobName);
// wait for pending period
setTimeout(function () {
_console()(jobName);
}, 5000);
}
};
} else {
cb = bag.cli.exitCb(null, function (result) {
console.log(message, jobName);
});
}
new Jenkins(url, proxy).build(
jobName,
(_.isString(params)) ? params : undefined,
cb
);
};
}
function _console() {
return function (jobName) {
new Jenkins(url, proxy).console(
jobName,
bag.cli.exit
);
};
}
function _stop() {
return function (jobName, params) {
new Jenkins(url, proxy).stop(
jobName,
bag.cli.exitCb(null, function (result) {
console.log('Job %s was stopped successfully', jobName);
})
);
};
}
function _dashboard() {
return function () {
new Jenkins(url, proxy).dashboard(bag.cli.exitCb(null, function (result) {
if (result.length === 0) {
console.log('Jobless Jenkins');
} else {
result.forEach(function (job) {
console.log('%s - %s', job.status, job.name);
});
}
}));
};
}
function _discover() {
return function (host) {
new Jenkins(url, proxy).discover(host, bag.cli.exitCb(null, function (result) {
console.log('Jenkins ver. %s is running on %s',
result.hudson.version[0],
(result.hudson.url && result.hudson.url[0]) ? result.hudson.url[0] : host);
}));
};
}
function _executor() {
return function () {
new Jenkins(url, proxy).executor(bag.cli.exitCb(null, function (result) {
if (!_.isEmpty(_.keys(result))) {
_.keys(result).forEach(function (computer) {
console.log('+ ' + computer);
result[computer].forEach(function (executor) {
if (executor.idle) {
console.log(' - idle');
} else {
console.log(' - %s | %s%%s', executor.name, executor.progress, (executor.stuck) ? ' stuck!' : '');
}
});
});
} else {
console.log('No executor found');
}
}));
};
}
function _job() {
return function (name) {
new Jenkins(url, proxy).job(name, bag.cli.exitCb(null, function (result) {
console.log('%s | %s', name, result.status);
result.reports.forEach(function (report) {
console.log(' - %s', report);
});
}));
};
}
function _queue() {
return function () {
new Jenkins(url, proxy).queue(bag.cli.exitCb(null, function (result) {
if (result.length === 0) {
console.log('Queue is empty');
} else {
result.forEach(function (job) {
console.log('- %s', job);
});
}
}));
};
}
function _version() {
return function () {
new Jenkins(url, proxy).version(bag.cli.exitCb(null, function (result) {
console.log('Jenkins ver. %s', result);
}));
};
}
var commands = {
build: {
desc: 'Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1¶m2=value2"]',
options: [
{ arg: '-c, --console', desc: 'Display console ouptput of the triggered build progress' }
],
action: _build()
},
console: {
desc: 'Display latest build console output\n\tnestor console <jobname>',
action: _console()
},
stop: {
desc: 'Stop the currently running build\n\tnestor stop <jobname>',
action: _stop()
},
dashboard: {
desc: 'View status of all jobs\n\tnestor dashboard',
action: _dashboard()
},
discover: {
desc: 'Discover Jenkins instance running on a specified host\n\tnestor discover <hostname>',
action: _discover()
},
executor: {
desc: 'View executors\' status (running builds)\n\tnestor executor',
action: _executor()
},
job: {
desc: 'View job status reports\n\tnestor job <jobname>',
action: _job()
},
queue: {
desc: 'View queued jobs\n\tnestor queue',
action: _queue()
},
ver: {
desc: 'View Jenkins version number\n\tnestor ver',
action: _version()
}
};
bag.cli.parse(commands, __dirname);
}
exports.exec = exec;