/* * 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 2016 Joyent, Inc. * * `triton instance enable-firewall ...` */ var assert = require('assert-plus'); var format = require('util').format; var vasync = require('vasync'); var common = require('../common'); var errors = require('../errors'); function do_enable_firewall(subcmd, opts, args, cb) { assert.func(cb, 'cb'); if (opts.help) { this.do_help('help', {}, [subcmd], cb); return; } if (args.length === 0) { cb(new errors.UsageError('Missing argument(s)')); return; } var cli = this.top; var insts = args; function wait(instId, startTime, next) { var cloudapi = cli.tritonapi.cloudapi; var waiter = cloudapi.waitForMachineFirewallState.bind(cloudapi); waiter({ id: instId, state: true }, function (err, inst) { if (err) { return next(err); } if (inst.firewall_enabled === true) { var duration = Date.now() - startTime; var durStr = common.humanDurationFromMs(duration); console.log('Enabled firewall for instance "%s" in %s', instId, durStr); next(); } else { // shouldn't get here, but... var msg = 'Failed to enable firewall for instance "%s"'; next(new Error(format(msg, instId))); } }); } vasync.pipeline({funcs: [ function confirm(_, next) { if (opts.force) { return next(); } var msg; if (insts.length === 1) { msg = 'Enable firewall for instance "' + insts[0] + '"? [y/n] '; } else { msg = format('Enable firewalls for %d instances (%s)? [y/n] ', insts.length, insts.join(', ')); } common.promptYesNo({msg: msg}, function (answer) { if (answer !== 'y') { console.error('Aborting'); next(true); // early abort signal } else { next(); } }); }, function enableThem(_, next) { var startTime = Date.now(); vasync.forEachParallel({ inputs: insts, func: function enableOne(instId, nextId) { cli.tritonapi.enableInstanceFirewall({ id: instId }, function (err, __, res) { if (err) { nextId(err); return; } var msg = 'Enabling firewall for instance "%s"'; console.log(msg, res.instId); if (opts.wait) { wait(res.instId, startTime, nextId); } else { nextId(); } }); } }, next); } ]}, function (err) { if (err === true) { err = null; } cb(err); }); } do_enable_firewall.options = [ { names: ['help', 'h'], type: 'bool', help: 'Show this help.' }, { names: ['force', 'f'], type: 'bool', help: 'Skip confirmation to enable.' }, { names: ['wait', 'w'], type: 'bool', help: 'Wait for the firewall to be enabled.' } ]; do_enable_firewall.help = [ 'Enable the firewall of one or more instances.', '', 'Usage:', ' {{name}} enable-firewall [] [...]', '', '{{options}}' ].join('\n'); module.exports = do_enable_firewall;