2017-02-22 01:42:07 +02:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2017 Joyent, Inc.
|
|
|
|
*
|
2017-03-21 03:55:59 +02:00
|
|
|
* `triton volume delete ...`
|
2017-02-22 01:42:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var distractions = require('../distractions');
|
|
|
|
var errors = require('../errors');
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
|
|
|
|
function deleteVolume(volumeName, opts, cb) {
|
2017-02-22 01:42:07 +02:00
|
|
|
assert.string(volumeName, 'volumeName');
|
2017-03-21 03:55:59 +02:00
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.object(opts.tritonapi, 'opts.tritonapi');
|
|
|
|
assert.func(cb, 'cb');
|
2017-02-22 01:42:07 +02:00
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
var tritonapi = opts.tritonapi;
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
vasync.pipeline({funcs: [
|
|
|
|
function getVolume(ctx, next) {
|
|
|
|
tritonapi.getVolume(volumeName,
|
|
|
|
function onGetVolume(getVolErr, volume) {
|
|
|
|
if (!getVolErr) {
|
|
|
|
ctx.volume = volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
next(getVolErr);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function doDeleteVolume(ctx, next) {
|
|
|
|
assert.object(ctx.volume, 'ctx.volume');
|
|
|
|
|
|
|
|
tritonapi.cloudapi.deleteVolume(ctx.volume.id,
|
|
|
|
next);
|
|
|
|
},
|
|
|
|
function waitForVolumeStates(ctx, next) {
|
|
|
|
assert.object(ctx.volume, 'ctx.volume');
|
|
|
|
|
|
|
|
var distraction;
|
|
|
|
var volumeId = ctx.volume.id;
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
if (!opts.wait) {
|
2017-02-22 01:42:07 +02:00
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
distraction = distractions.createDistraction(opts.wait.length);
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
tritonapi.cloudapi.waitForVolumeStates({
|
|
|
|
id: volumeId,
|
|
|
|
states: ['deleted', 'failed']
|
|
|
|
}, function onWaitDone(waitErr, volume) {
|
|
|
|
distraction.destroy();
|
|
|
|
next(waitErr);
|
|
|
|
});
|
|
|
|
}
|
2017-03-21 03:55:59 +02:00
|
|
|
], arg: {}}, cb);
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
function do_delete(subcmd, opts, args, cb) {
|
2017-02-22 01:42:07 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (opts.help) {
|
2017-03-21 03:55:59 +02:00
|
|
|
self.do_help('help', {}, [subcmd], cb);
|
2017-02-22 01:42:07 +02:00
|
|
|
return;
|
|
|
|
} else if (args.length < 1) {
|
2017-03-21 03:55:59 +02:00
|
|
|
cb(new errors.UsageError('missing VOLUME arg(s)'));
|
2017-02-22 01:42:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var context = {
|
2017-03-23 01:54:36 +02:00
|
|
|
volumeIds: args,
|
|
|
|
cli: this.top
|
2017-02-22 01:42:07 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
vasync.pipeline({arg: context, funcs: [
|
2017-03-23 01:54:36 +02:00
|
|
|
common.cliSetupTritonApi,
|
|
|
|
function confirm(ctx, next) {
|
|
|
|
var promptMsg;
|
|
|
|
|
|
|
|
if (opts.yes) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.volumeIds.length === 1) {
|
|
|
|
promptMsg = format('Delete volume %s? [y/n] ',
|
|
|
|
ctx.volumeIds[0]);
|
|
|
|
} else {
|
|
|
|
promptMsg = format('Delete %d volumes? [y/n] ',
|
|
|
|
ctx.volumeIds.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
common.promptYesNo({msg: promptMsg},
|
|
|
|
function onPromptAnswered(answer) {
|
|
|
|
if (answer !== 'y') {
|
|
|
|
console.error('Aborting');
|
|
|
|
/*
|
|
|
|
* Early abort signal.
|
|
|
|
*/
|
|
|
|
next(true);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
2017-02-22 01:42:07 +02:00
|
|
|
},
|
|
|
|
function deleteVolumes(ctx, next) {
|
|
|
|
vasync.forEachParallel({
|
|
|
|
func: function doDeleteVolume(volumeId, done) {
|
2017-03-23 01:54:36 +02:00
|
|
|
self.top.tritonapi.deleteVolume({
|
|
|
|
id: volumeId,
|
|
|
|
wait: opts.wait && opts.wait.length > 0,
|
|
|
|
waitTimeout: opts.wait_timeout * 1000,
|
2017-02-22 01:42:07 +02:00
|
|
|
tritonapi: self.top.tritonapi
|
2017-03-23 01:54:36 +02:00
|
|
|
}, function onVolDeleted(volDelErr) {
|
|
|
|
if (volDelErr) {
|
|
|
|
console.error('Error when deleting volume %s, '
|
|
|
|
+ 'reason: %s', volumeId, volDelErr);
|
|
|
|
} else {
|
|
|
|
console.log('Deleted volume %s', volumeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-22 01:42:07 +02:00
|
|
|
},
|
|
|
|
inputs: ctx.volumeIds
|
|
|
|
}, next);
|
|
|
|
}
|
2017-03-21 03:55:59 +02:00
|
|
|
]}, function onDone(err) {
|
2017-03-23 01:54:36 +02:00
|
|
|
if (err === true) {
|
|
|
|
/*
|
|
|
|
* Answered 'no' to confirmation to delete.
|
|
|
|
*/
|
|
|
|
err = null;
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:42:07 +02:00
|
|
|
if (err) {
|
2017-03-21 03:55:59 +02:00
|
|
|
cb(err);
|
2017-02-22 01:42:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
cb();
|
2017-02-22 01:42:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_delete.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Other options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait', 'w'],
|
|
|
|
type: 'arrayOfBool',
|
|
|
|
help: 'Wait for the deletion to complete. Use multiple times for a ' +
|
|
|
|
'spinner.'
|
2017-03-23 01:54:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait-timeout'],
|
|
|
|
type: 'positiveInteger',
|
|
|
|
default: 120,
|
|
|
|
help: 'The number of seconds to wait before timing out with an error. '
|
|
|
|
+ 'The default is 120 seconds.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['yes', 'y'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Answer yes to confirmation to delete.'
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
do_delete.synopses = ['{{name}} {{cmd}} [OPTIONS] VOLUME [VOLUME ...]'];
|
|
|
|
|
|
|
|
do_delete.help = [
|
|
|
|
'Deletes a volume.',
|
|
|
|
'',
|
|
|
|
'{{usage}}',
|
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
'',
|
|
|
|
'Where VOLUME is a volume id (full UUID), exact name, or short id.'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_delete.completionArgtypes = ['tritonvolume', 'none'];
|
|
|
|
do_delete.aliases = ['rm'];
|
|
|
|
|
|
|
|
module.exports = do_delete;
|