-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsvmp-server-cli.js
More file actions
377 lines (334 loc) · 11.9 KB
/
svmp-server-cli.js
File metadata and controls
377 lines (334 loc) · 11.9 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env node
/*
* Copyright 2013-2014 The MITRE Corporation, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this work except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Dave Bryson
*
*/
/**
* Command line administration client for SVMP
*/
var
program = require('commander'),
colors = require('colors'),
fs = require('fs'),
overseerClient = require('svmp-overseer-client'),
config = require('nconf'),
yaml = require('js-yaml'),
Table = require('cli-table');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
/**
* Current version used. Read from package.json
* @type {String}
*/
program.version(require('./package.json').version);
program
.command('list')
.description('List proxy Users')
.action(function () {
console.log('');
console.log('Proxy users:'.bold.help);
svmp.listUsers(function (err, res) {
if (!badResponse(err, res)) {
var list = res.body.users;
var table = new Table({
head: ['User', 'VM_IP', 'VM_ID', 'VOL_ID', 'Device']
});
for (var i = 0; i < list.length; i++) {
var o = list[i];
var vm_ip = o.vm_ip || "";
var vm_id = o.vm_id || "";
var volume_id = o.volume_id || "";
var device = o.device_type || "";
table.push([
o.username,
vm_ip,
vm_id,
volume_id,
device
]);
}
console.log(table.toString());
console.log('');
}
});
});
program
.command('devices')
.description('List supported device types')
.action(function () {
console.log('');
console.log('Supported device types:'.bold.help);
svmp.listDevices(function (err, res) {
if (!badResponse(err, res)) {
var images = res.body;
var table = new Table({
head: ['Device Type']
});
for (var key in images) {
table.push([key]);
}
console.log(table.toString());
console.log('');
}
});
});
program
.command('clear-vm-info <username>')
.description('Clear the Users VM Information')
.action(function (un) {
console.log('');
console.log('Clearing user information...'.bold.help);
var update = {vm_ip: "", vm_id: ""};
svmp.updateUser(un, update, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! Cleared VM IP and VM ID for user:', un);
console.log('');
}
});
});
program
.command('show <username>')
.description('Show information about a user')
.action(function (un) {
console.log('');
console.log('Finding user information...'.bold.help);
svmp.getUser(un, function (err, res) {
if (!badResponse(err, res)) {
var user = res.body.user;
console.log(JSON.stringify(user, null, 4).data);
console.log('');
}
});
});
program
.command('add <username> <password> <email> <device_type>')
.description('Add a User to system. NOTE: this does NOT create a volume for the User! (Use this command if you aren\'t using a cloud platform)')
.action(function (un, pw, email, dev) {
console.log('');
console.log('Adding a new user...'.bold.help);
svmp.createUser(un, pw, email, dev, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! Created user:', un);
console.log(' NOTE: The user does NOT have a volume assigned; use the "volume-create" command to do so'.warn);
console.log('');
}
});
});
program
.command('add-user-with-volume <username> <password> <email> <device_type>')
.description('Add a new User to the system and create a volume for the User')
.action(function (un, pw, email, dev) {
console.log('');
console.log('Adding a new user...'.bold.help);
svmp.createUser(un, pw, email, dev, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Created user:', un);
console.log('');
// add the user's volume
svmp.createVolume(un, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Volume created for:', un);
console.log(' ... information saved to user\'s account ...');
console.log(' NOTE: The Volume is NOT attached to the user\'s VM.'.warn);
console.log('');
}
});
}
});
});
program
.command('vm <username>')
.description('Create and start a VM for a user in the system.')
.action(function (un, imageid, imageflvr) {
console.log('');
console.log('Creating a new VM, this may take a few seconds...'.bold.help);
svmp.setupVM(un, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! VM created and running, IP:', res.body.vm_ip);
console.log('');
}
});
});
program
.command('vm-add <username> <vm_ip_address>')
.description('Register an existing VM at a given IP address to the user. (For testing/dev ONLY.)')
.action(function (un, vmip) {
console.log('');
console.log('Updating user\'s VM IP address...'.bold.help);
var update = {vm_ip: vmip, vm_id: ""};
svmp.updateUser(un, update, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! Updated VM IP and cleared VM ID for user:', un);
console.log('');
}
});
});
program
.command('list-volumes')
.description('list available volumes')
.action(function () {
console.log('');
console.log('Available volumes:'.bold.help);
svmp.listVolumes(function (err, res) {
if (!badResponse(err, res)) {
var r = res.body.volumes;
var table = new Table({
head: ['Name', 'Status', 'ID']
});
for (var i = 0; i < r.length; i++) {
table.push(r[i]);
}
console.log(table.toString());
console.log('');
}
});
});
program
.command('volume-create <username>')
.description('Create and assign a Volume to a user based on the gold snapshot id in config-local')
.action(function (un) {
console.log('');
console.log('Creating data volume for user...'.bold.help);
// add the user's volume
svmp.createVolume(un, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! Volume created for user:', un);
console.log(' ... information saved to user\'s account ...');
console.log(' NOTE: The Volume is NOT attached to the user\'s VM'.warn);
console.log('');
}
});
});
program
.command('volume-assign <username> <volume_id>')
.description('Does not attach Volume to VM, simply associates an existing user data volume with the specified user.')
.action(function (un, volid) {
console.log('');
console.log('Associating volume with user...'.bold.help);
svmp.assignVolume(un, volid, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done! Updated volume ID for user:', un);
console.log('');
}
});
});
program
.command('delete <username>')
.description('Delete a User from the Proxy')
.action(function (un) {
console.log('');
console.log('Deleting user...'.bold.help);
svmp.deleteUser(un, function (err, res) {
if (!badResponse(err, res)) {
console.log(' Done!');
console.log('');
}
});
});
program
.command('images')
.description('List available images and flavors on your cloud platform; this information is needed when creating a VM')
.action(function () {
svmp.listImages(function (err, res) {
if (!badResponse(err, res)) {
console.log('');
console.log('Image flavors available:'.bold.help);
var flavorTable = new Table({
head: ['Name', 'ID']
});
for (i = 0; i < res.body.flavors.length; i++) {
var o = res.body.flavors[i];
flavorTable.push([o[1], o[0]]);
}
console.log(flavorTable.toString());
console.log('');
console.log('NOTE: Use the ID value when choosing a Flavor'.warn);
console.log('');
console.log('Images available:'.bold.help);
var imgTable = new Table({
head: ['Name', 'ID']
});
for (i = 0; i < res.body.images.length; i++) {
var o = res.body.images[i];
imgTable.push([o[1], o[0]]);
}
console.log(imgTable.toString());
console.log('');
}
});
});
if (process.argv.length <= 2) {
console.log('');
console.log(' Run "svmp-config -h" to see available commands'.error);
console.log('');
process.exit();
} else {
config.env(['overseer_url', 'auth_token', 'trust_all_certs']);
if (fs.existsSync(process.env.HOME + '/.svmprc')) {
config.file({
file: process.env.HOME + '/.svmprc',
format: {
parse: yaml.safeLoad,
stringify: yaml.safeDump
}
});
}
if (typeof config.get('overseer_url') === 'undefined') {
console.log('No overseer_url set.');
process.exit();
}
if (typeof config.get('auth_token') === 'undefined') {
console.log('No auth_token set.');
process.exit();
}
console.log('Using overseer URL: ' + config.get('overseer_url'));
if (config.get('trust_all_certs')) {
console.log('Trusting ALL server SSL/TLS certificates...'.warn);
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
// Overseer Client
var svmp = new overseerClient(config.get('overseer_url'), config.get('auth_token'));
program.parse(process.argv);
}
// internal function
// returns 'true' and logs if there is an error
// returns 'false' if there is no error
function badResponse(err, response) {
var errText;
if (err) {
errText = ' Error: ' + err.message;
}
else if (response.status !== 200) {
errText = ' Error code: ' + response.status + ', text: ' + response.text;
}
if (errText) {
console.log(errText.error);
console.log('');
return true;
}
return false;
}