From 816e31748505ff600959bf4c1f6fc41503527167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 13 Jul 2017 19:54:19 +0300 Subject: [PATCH] MXS-1300: Make MaxCtrl more like a library All invocations into the common helper functions are now done through the `maxctrl` function. This allows the program arguments to be passed to the core as a parameter instead of reading them from global variables. This helps with the implicit initialization that was done when the yargs library was required which caused duplicated output. Refactored the core functions so that they only process the argument vector. The parsing of the argument vector is done in maxctrl.js where it is more appropriate. --- maxctrl/common.js | 22 ++++--- maxctrl/core.js | 18 ++---- maxctrl/lib/alter.js | 15 +++-- maxctrl/lib/call.js | 28 +++++---- maxctrl/lib/clear.js | 3 +- maxctrl/lib/create.js | 12 ++-- maxctrl/lib/destroy.js | 12 ++-- maxctrl/lib/disable.js | 15 +++-- maxctrl/lib/enable.js | 15 +++-- maxctrl/lib/link.js | 30 ++++----- maxctrl/lib/list.js | 108 ++++++++++++++++--------------- maxctrl/lib/rotate.js | 3 +- maxctrl/lib/set.js | 3 +- maxctrl/lib/show.js | 140 ++++++++++++++++++++++------------------- maxctrl/lib/start.js | 6 +- maxctrl/lib/stop.js | 6 +- maxctrl/lib/unlink.js | 30 ++++----- maxctrl/maxctrl.js | 16 ++++- 18 files changed, 271 insertions(+), 211 deletions(-) diff --git a/maxctrl/common.js b/maxctrl/common.js index 968414438..eb1bbe2ee 100644 --- a/maxctrl/common.js +++ b/maxctrl/common.js @@ -18,8 +18,11 @@ var Table = require('cli-table'); module.exports = function() { this._ = require('lodash-getpath') - // Common options for all commands - this.program = require('yargs'); + + this.maxctrl = function(argv) { + this.argv = argv + return this + } // Request a resource collection and format it as a table this.getCollection = function (resource, fields) { @@ -114,11 +117,10 @@ module.exports = function() { } // Helper for converting endpoints to acutal URLs - this.getUri = function(host, endpoint) { + this.getUri = function(host, secure, endpoint) { var base = 'http://' - var argv = this.program.argv - if (argv.secure) { + if (secure) { base = 'https://' } @@ -127,12 +129,12 @@ module.exports = function() { // Helper for executing requests and handling their responses this.doRequest = function(resource, cb, obj) { - pingCluster() + pingCluster(this.argv.hosts) .then(function() { - var argv = this.program.argv + var argv = this.argv argv.hosts.forEach(function(host) { args = obj || {} - args.uri = getUri(host, resource) + args.uri = getUri(host, argv.secure, resource) args.json = true args.timeout = argv.timeout @@ -206,10 +208,10 @@ function pingMaxScale(host) { }) } -function pingCluster() { +function pingCluster(hosts) { var promises = [] - this.program.argv.hosts.forEach(function(i) { + hosts.forEach(function(i) { promises.push(pingMaxScale(i)) }) diff --git a/maxctrl/core.js b/maxctrl/core.js index dd4114afb..6e03bc098 100644 --- a/maxctrl/core.js +++ b/maxctrl/core.js @@ -10,24 +10,16 @@ * of this software will be governed by version 2 or later of the General * Public License. */ -require('./common.js')() + var fs = require('fs') +var program = require('yargs'); + const maxctrl_version = '1.0.0'; +require('./common.js')() + module.exports = function(argv) { - // Mangle the arguments if we are being called from the command line - if (argv[0] == process.execPath) { - argv.shift() - } - - try { - while (argv.length > 0) { - fs.accessSync(argv[0]) - argv.shift() - } - } catch (err) { } - return new Promise(function(resolve, reject) { program .version(maxctrl_version) diff --git a/maxctrl/lib/alter.js b/maxctrl/lib/alter.js index dc25e6026..3355886ae 100644 --- a/maxctrl/lib/alter.js +++ b/maxctrl/lib/alter.js @@ -18,19 +18,24 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('server ', 'Alter server parameters', {}, function(argv) { - updateValue('servers/' + argv.server, 'data.attributes.parameters.' + argv.key, argv.value) + maxctrl(argv) + .updateValue('servers/' + argv.server, 'data.attributes.parameters.' + argv.key, argv.value) }) .command('monitor ', 'Alter monitor parameters', {}, function(argv) { - updateValue('monitors/' + argv.monitor, 'data.attributes.parameters.' + argv.key, argv.value) + maxctrl(argv) + .updateValue('monitors/' + argv.monitor, 'data.attributes.parameters.' + argv.key, argv.value) }) .command('service ', 'Alter service parameters', {}, function(argv) { - updateValue('services/' + argv.service, 'data.attributes.parameters.' + argv.key, argv.value) + maxctrl(argv) + .updateValue('services/' + argv.service, 'data.attributes.parameters.' + argv.key, argv.value) }) .command('logging ', 'Alter logging parameters', {}, function(argv) { - updateValue('maxscale/logs', 'attributes.parameters.' + argv.key, argv.value) + maxctrl(argv) + .updateValue('maxscale/logs', 'attributes.parameters.' + argv.key, argv.value) }) .command('maxscale ', 'Alter MaxScale parameters', {}, function(argv) { - updateValue('maxscale', 'attributes.parameters.' + argv.key, argv.value) + maxctrl(argv) + .updateValue('maxscale', 'attributes.parameters.' + argv.key, argv.value) }) .usage('Usage: alter ') .help() diff --git a/maxctrl/lib/call.js b/maxctrl/lib/call.js index ba6ad61e1..80fd6538c 100644 --- a/maxctrl/lib/call.js +++ b/maxctrl/lib/call.js @@ -20,22 +20,24 @@ exports.builder = function(yargs) { .command('command [parameters...]', 'Call a module command', {}, function(argv) { // First we have to find the correct method to use - doRequest('maxscale/modules/' + argv.module + '/', function(resp) { + maxctrl(argv) + .doRequest('maxscale/modules/' + argv.module + '/', function(resp) { - // A GET request will return the correct error if the command is not found - var verb = 'GET' + // A GET request will return the correct error if the command is not found + var verb = 'GET' - resp.data.attributes.commands.forEach(function(i) { - if (i.id == argv.command) { - verb = i.attributes.method; - } + resp.data.attributes.commands.forEach(function(i) { + if (i.id == argv.command) { + verb = i.attributes.method; + } + }) + + maxctrl + .doRequest('maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'), + function(resp) { + console.log(JSON.stringify(resp, null, 4)) + }, { method: verb }) }) - - doRequest('maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'), - function(resp) { - console.log(JSON.stringify(resp, null, 4)) - }, { method: verb }) - }) }) .usage('Usage: call ') .help() diff --git a/maxctrl/lib/clear.js b/maxctrl/lib/clear.js index 265261e11..dc9adc216 100644 --- a/maxctrl/lib/clear.js +++ b/maxctrl/lib/clear.js @@ -19,7 +19,8 @@ exports.builder = function(yargs) { yargs .command('server ', 'Clear server state', {}, function(argv) { var target = 'servers/' + argv.server + '/clear?state=' + argv.state - doRequest(target, null, {method: 'PUT'}) + maxctrl(argv) + .doRequest(target, null, {method: 'PUT'}) }) .usage('Usage: clear ') .help() diff --git a/maxctrl/lib/create.js b/maxctrl/lib/create.js index aa7162f97..329e418b8 100644 --- a/maxctrl/lib/create.js +++ b/maxctrl/lib/create.js @@ -71,7 +71,8 @@ exports.builder = function(yargs) { } } - doRequest('servers', null, {method: 'POST', body: server}) + maxctrl(argv) + .doRequest('servers', null, {method: 'POST', body: server}) }) // Create monitor @@ -97,7 +98,8 @@ exports.builder = function(yargs) { } } - doRequest('monitors', null, {method: 'POST', body: monitor}) + maxctrl(argv) + .doRequest('monitors', null, {method: 'POST', body: monitor}) }) // Create listener @@ -157,7 +159,8 @@ exports.builder = function(yargs) { } } - doRequest('services/' + argv.service + '/listeners', null, {method: 'POST', body: listener}) + maxctrl(argv) + .doRequest('services/' + argv.service + '/listeners', null, {method: 'POST', body: listener}) }) .command('user ', 'Create a new network user', {}, function(argv) { @@ -171,7 +174,8 @@ exports.builder = function(yargs) { } } - doRequest('users/inet', null, {method: 'POST', body: user}) + maxctrl(argv) + .doRequest('users/inet', null, {method: 'POST', body: user}) }) .usage('Usage: create ') diff --git a/maxctrl/lib/destroy.js b/maxctrl/lib/destroy.js index 94c91549d..8561747f0 100644 --- a/maxctrl/lib/destroy.js +++ b/maxctrl/lib/destroy.js @@ -18,16 +18,20 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('server ', 'Destroy an unused server', {}, function(argv) { - doRequest('servers/' + argv.name, null, {method: 'DELETE'}) + maxctrl(argv) + .doRequest('servers/' + argv.name, null, {method: 'DELETE'}) }) .command('monitor ', 'Destroy an unused monitor', {}, function(argv) { - doRequest('monitors/' + argv.name, null, {method: 'DELETE'}) + maxctrl(argv) + .doRequest('monitors/' + argv.name, null, {method: 'DELETE'}) }) .command('listener ', 'Destroy an unused listener', {}, function(argv) { - doRequest('services/' + argv.service + '/listeners/' + argv.name, null, {method: 'DELETE'}) + maxctrl(argv) + .doRequest('services/' + argv.service + '/listeners/' + argv.name, null, {method: 'DELETE'}) }) .command('user ', 'Remove a network user', {}, function(argv) { - doRequest('users/inet/' + argv.name, null, {method: 'DELETE'}) + maxctrl(argv) + .doRequest('users/inet/' + argv.name, null, {method: 'DELETE'}) }) .usage('Usage: destroy ') .help() diff --git a/maxctrl/lib/disable.js b/maxctrl/lib/disable.js index 79a4657a9..5c53014e9 100644 --- a/maxctrl/lib/disable.js +++ b/maxctrl/lib/disable.js @@ -26,19 +26,24 @@ exports.builder = function(yargs) { yargs .command('log-priority ', 'Disable log priority [warning|notice|info|debug]', {}, function(argv) { if (log_levels.indexOf(argv.log) != -1) { - updateValue('maxscale/logs', 'data.attributes.parameters.log_' + argv.log, false) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.log_' + argv.log, false) } else { - logError('Invalid log priority: ' + argv.log); + maxctrl(argv) + .logError('Invalid log priority: ' + argv.log); } }) .command('maxlog', 'Disable MaxScale logging', {}, function(argv) { - updateValue('maxscale/logs', 'data.attributes.parameters.maxlog', false) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.maxlog', false) }) .command('syslog', 'Disable syslog logging', {}, function(argv) { - updateValue('maxscale/logs', 'data.attributes.parameters.syslog', false) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.syslog', false) }) .command('account ', 'Disable a Linux user account from administrative use', {}, function(argv) { - doRequest('users/unix/' + argv.name, null, { method: 'DELETE'}) + maxctrl(argv) + .doRequest('users/unix/' + argv.name, null, { method: 'DELETE'}) }) .usage('Usage: disable ') .help() diff --git a/maxctrl/lib/enable.js b/maxctrl/lib/enable.js index c6bcf8a92..85a757e38 100644 --- a/maxctrl/lib/enable.js +++ b/maxctrl/lib/enable.js @@ -26,16 +26,20 @@ exports.builder = function(yargs) { yargs .command('log-priority ', 'Enable log priority [warning|notice|info|debug]', {}, function(argv) { if (log_levels.indexOf(argv.log) != -1) { - updateValue('maxscale/logs', 'data.attributes.parameters.log_' + argv.log, true) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.log_' + argv.log, true) } else { - logError('Invalid log priority: ' + argv.log); + maxctrl(argv) + .logError('Invalid log priority: ' + argv.log); } }) .command('maxlog', 'Enable MaxScale logging', {}, function(argv) { - updateValue('maxscale/logs', 'data.attributes.parameters.maxlog', true) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.maxlog', true) }) .command('syslog', 'Enable syslog logging', {}, function(argv) { - updateValue('maxscale/logs', 'data.attributes.parameters.syslog', true) + maxctrl(argv) + .updateValue('maxscale/logs', 'data.attributes.parameters.syslog', true) }) .command('account ', 'Activate a Linux user account for administrative use', {}, function(argv) { var req_body = { @@ -44,7 +48,8 @@ exports.builder = function(yargs) { type: 'unix' } } - doRequest('users/unix', null, { method: 'POST', body: req_body}) + maxctrl(argv) + .doRequest('users/unix', null, { method: 'POST', body: req_body}) }) .usage('Usage: enable ') .help() diff --git a/maxctrl/lib/link.js b/maxctrl/lib/link.js index f5f3d4b8f..2955f4677 100644 --- a/maxctrl/lib/link.js +++ b/maxctrl/lib/link.js @@ -12,20 +12,22 @@ */ require('../common.js')() -function addServer(path, targets) { - doRequest(path, function(res) { - var servers =_.get(res, 'data.relationships.servers.data', []) +function addServer(argv, path, targets) { + maxctrl(argv) + .doRequest(path, function(res) { + var servers =_.get(res, 'data.relationships.servers.data', []) - targets.forEach(function(i){ - servers.push({id: i, type: 'servers'}) + targets.forEach(function(i){ + servers.push({id: i, type: 'servers'}) + }) + + // Update relationships and remove unnecessary parts + _.set(res, 'data.relationships.servers.data', servers) + delete res.data.attributes + + maxctrl(argv) + .doRequest(path, null, {method: 'PATCH', body: res}) }) - - // Update relationships and remove unnecessary parts - _.set(res, 'data.relationships.servers.data', servers) - delete res.data.attributes - - doRequest(path, null, {method: 'PATCH', body: res}) - }) } exports.command = 'link ' @@ -34,10 +36,10 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('service ', 'Link servers to a service', {}, function(argv) { - addServer('services/' + argv.name, argv.server) + addServer(argv, 'services/' + argv.name, argv.server) }) .command('monitor ', 'Link servers to a monitor', {}, function(argv) { - addServer('monitors/' + argv.name, argv.server) + addServer(argv, 'monitors/' + argv.name, argv.server) }) .usage('Usage: link ') .help() diff --git a/maxctrl/lib/list.js b/maxctrl/lib/list.js index 1fe890e71..59fd36b8d 100644 --- a/maxctrl/lib/list.js +++ b/maxctrl/lib/list.js @@ -18,63 +18,71 @@ exports.desc = 'List objects' exports.handler = function() {} exports.builder = function(yargs) { yargs - .command('servers', 'List servers', {}, function() { - getCollection('servers', [ - {'Server': 'id'}, - {'Address': 'attributes.parameters.address'}, - {'Port': 'attributes.parameters.port'}, - {'Connections': 'attributes.statistics.connections'}, - {'State': 'attributes.state'} - ]) + .command('servers', 'List servers', {}, function(argv) { + maxctrl(argv) + .getCollection('servers', [ + {'Server': 'id'}, + {'Address': 'attributes.parameters.address'}, + {'Port': 'attributes.parameters.port'}, + {'Connections': 'attributes.statistics.connections'}, + {'State': 'attributes.state'} + ]) }) - .command('services', 'List services', {}, function() { - getCollection('services',[ - {'Service': 'id'}, - {'Router': 'attributes.router'}, - {'Connections': 'attributes.connections'}, - {'Total Connections': 'attributes.total_connections'}, - {'Servers': 'relationships.servers.data[].id'} - ]) + .command('services', 'List services', {}, function(argv) { + maxctrl(argv) + .getCollection('services',[ + {'Service': 'id'}, + {'Router': 'attributes.router'}, + {'Connections': 'attributes.connections'}, + {'Total Connections': 'attributes.total_connections'}, + {'Servers': 'relationships.servers.data[].id'} + ]) }) - .command('monitors', 'List monitors', {}, function() { - getCollection('monitors', [ - {'Monitor': 'id'}, - {'State': 'attributes.state'}, - {'Servers': 'relationships.servers.data[].id'} - ]) + .command('monitors', 'List monitors', {}, function(argv) { + maxctrl(argv) + .getCollection('monitors', [ + {'Monitor': 'id'}, + {'State': 'attributes.state'}, + {'Servers': 'relationships.servers.data[].id'} + ]) }) - .command('sessions', 'List sessions', {}, function() { - getCollection('sessions',[ - {'Id': 'id'}, - {'Service': 'relationships.services.data[].id'}, - {'User': 'attributes.user'}, - {'Host': 'attributes.remote'} - ]) + .command('sessions', 'List sessions', {}, function(argv) { + maxctrl(argv) + .getCollection('sessions',[ + {'Id': 'id'}, + {'Service': 'relationships.services.data[].id'}, + {'User': 'attributes.user'}, + {'Host': 'attributes.remote'} + ]) }) - .command('filters', 'List filters', {}, function() { - getCollection('filters', [ - {'Filter': 'id'}, - {'Service': 'relationships.services.data[].id'}, - {'Module': 'attributes.module'} - ]) + .command('filters', 'List filters', {}, function(argv) { + maxctrl(argv) + .getCollection('filters', [ + {'Filter': 'id'}, + {'Service': 'relationships.services.data[].id'}, + {'Module': 'attributes.module'} + ]) }) - .command('modules', 'List loaded modules', {}, function() { - getCollection('maxscale/modules',[ - {'Module':'id'}, - {'Type':'attributes.module_type'}, - {'Version': 'attributes.version'} - ]) + .command('modules', 'List loaded modules', {}, function(argv) { + maxctrl(argv) + .getCollection('maxscale/modules',[ + {'Module':'id'}, + {'Type':'attributes.module_type'}, + {'Version': 'attributes.version'} + ]) }) - .command('users', 'List created network users', {}, function() { - getCollection('users/inet',[ - {'Name':'id'} - ]) + .command('users', 'List created network users', {}, function(argv) { + maxctrl(argv) + .getCollection('users/inet',[ + {'Name':'id'} + ]) }) - .command('commands', 'List module commands', {}, function() { - getCollection('maxscale/modules',[ - {'Module':'id'}, - {'Commands': 'attributes.commands[].id'} - ]) + .command('commands', 'List module commands', {}, function(argv) { + maxctrl(argv) + .getCollection('maxscale/modules',[ + {'Module':'id'}, + {'Commands': 'attributes.commands[].id'} + ]) }) .usage('Usage: list ') .help() diff --git a/maxctrl/lib/rotate.js b/maxctrl/lib/rotate.js index 6e37b39b4..5b13e5a3f 100644 --- a/maxctrl/lib/rotate.js +++ b/maxctrl/lib/rotate.js @@ -18,7 +18,8 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('logs', 'Rotate log files by closing and reopening the files', {}, function(argv) { - doRequest('maxscale/logs/flush/', null, {method: 'POST'}) + maxctrl(argv) + .doRequest('maxscale/logs/flush/', null, {method: 'POST'}) }) .usage('Usage: rotate ') .help() diff --git a/maxctrl/lib/set.js b/maxctrl/lib/set.js index 11aa3b1e0..f3e870c5e 100644 --- a/maxctrl/lib/set.js +++ b/maxctrl/lib/set.js @@ -19,7 +19,8 @@ exports.builder = function(yargs) { yargs .command('server ', 'Set server state', {}, function(argv) { var target = 'servers/' + argv.server + '/set?state=' + argv.state - doRequest(target, null, {method: 'PUT'}) + maxctrl(argv) + .doRequest(target, null, {method: 'PUT'}) }) .usage('Usage: set ') .help() diff --git a/maxctrl/lib/show.js b/maxctrl/lib/show.js index 712c64006..b2776d5db 100644 --- a/maxctrl/lib/show.js +++ b/maxctrl/lib/show.js @@ -19,86 +19,94 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('server ', 'Show server', {}, function(argv) { - getResource('servers/' + argv.server, [ - {'Server': 'id'}, - {'Address': 'attributes.parameters.address'}, - {'Port': 'attributes.parameters.port'}, - {'State': 'attributes.state'}, - {'Services': 'relationships.services.data[].id'}, - {'Monitors': 'relationships.monitors.data[].id'}, - {'Master ID': 'attributes.master_id'}, - {'Node ID': 'attributes.node_id'}, - {'Slave Server IDs': 'attributes.slaves'}, - {'Statistics': 'attributes.statistics'}, - {'Parameters': 'attributes.parameters'} - ]) + maxctrl(argv) + .getResource('servers/' + argv.server, [ + {'Server': 'id'}, + {'Address': 'attributes.parameters.address'}, + {'Port': 'attributes.parameters.port'}, + {'State': 'attributes.state'}, + {'Services': 'relationships.services.data[].id'}, + {'Monitors': 'relationships.monitors.data[].id'}, + {'Master ID': 'attributes.master_id'}, + {'Node ID': 'attributes.node_id'}, + {'Slave Server IDs': 'attributes.slaves'}, + {'Statistics': 'attributes.statistics'}, + {'Parameters': 'attributes.parameters'} + ]) }) .command('service ', 'Show service', {}, function(argv) { - getResource('services/' + argv.service, [ - {'Service': 'id'}, - {'Router': 'attributes.router'}, - {'State': 'attributes.state'}, - {'Started At': 'attributes.started'}, - {'Current Connections': 'attributes.connections'}, - {'Total Connections': 'attributes.total_connections'}, - {'Servers': 'relationships.servers.data[].id'}, - {'Parameters': 'attributes.parameters'}, - {'Router Diagnostics': 'attributes.router_diagnostics'} - ]) + maxctrl(argv) + .getResource('services/' + argv.service, [ + {'Service': 'id'}, + {'Router': 'attributes.router'}, + {'State': 'attributes.state'}, + {'Started At': 'attributes.started'}, + {'Current Connections': 'attributes.connections'}, + {'Total Connections': 'attributes.total_connections'}, + {'Servers': 'relationships.servers.data[].id'}, + {'Parameters': 'attributes.parameters'}, + {'Router Diagnostics': 'attributes.router_diagnostics'} + ]) }) .command('monitor ', 'Show monitor', {}, function(argv) { - getResource('monitors/' + argv.monitor, [ - {'Monitor': 'id'}, - {'State': 'attributes.state'}, - {'Servers': 'relationships.servers.data[].id'}, - {'Parameters': 'attributes.parameters'}, - {'Monitor Diagnostics': 'attributes.monitor_diagnostics'} - ]) + maxctrl(argv) + .getResource('monitors/' + argv.monitor, [ + {'Monitor': 'id'}, + {'State': 'attributes.state'}, + {'Servers': 'relationships.servers.data[].id'}, + {'Parameters': 'attributes.parameters'}, + {'Monitor Diagnostics': 'attributes.monitor_diagnostics'} + ]) }) .command('session ', 'Show session', {}, function(argv) { - getResource('sessions/' + argv.session, [ - {'Id': 'id'}, - {'Service': 'relationships.services.data[].id'}, - {'State': 'attributes.state'}, - {'User': 'attributes.user'}, - {'Host': 'attributes.remote'}, - {'Connected': 'attributes.connected'}, - {'Idle': 'attributes.idle'} - ]) + maxctrl(argv) + .getResource('sessions/' + argv.session, [ + {'Id': 'id'}, + {'Service': 'relationships.services.data[].id'}, + {'State': 'attributes.state'}, + {'User': 'attributes.user'}, + {'Host': 'attributes.remote'}, + {'Connected': 'attributes.connected'}, + {'Idle': 'attributes.idle'} + ]) }) .command('filter ', 'Show filter', {}, function(argv) { - getResource('filters/' + argv.filter, [ - {'Filter': 'id'}, - {'Module': 'attributes.module'}, - {'Services': 'relationships.services.data[].id'}, - {'Parameters': 'attributes.parameters'} - ]) + maxctrl(argv) + .getResource('filters/' + argv.filter, [ + {'Filter': 'id'}, + {'Module': 'attributes.module'}, + {'Services': 'relationships.services.data[].id'}, + {'Parameters': 'attributes.parameters'} + ]) }) .command('module ', 'Show loaded module', {}, function(argv) { - getResource('maxscale/modules/' + argv.module, [ - {'Module': 'id'}, - {'Type': 'attributes.module_type'}, - {'Version': 'attributes.version'}, - {'Maturity': 'attributes.maturity'}, - {'Description': 'attributes.description'}, - {'Parameters': 'attributes.parameters'}, - {'Commands': 'attributes.commands'} - ]) + maxctrl(argv) + .getResource('maxscale/modules/' + argv.module, [ + {'Module': 'id'}, + {'Type': 'attributes.module_type'}, + {'Version': 'attributes.version'}, + {'Maturity': 'attributes.maturity'}, + {'Description': 'attributes.description'}, + {'Parameters': 'attributes.parameters'}, + {'Commands': 'attributes.commands'} + ]) }) .command('maxscale', 'Show MaxScale information', {}, function(argv) { - getResource('maxscale', [ - {'Version': 'attributes.version'}, - {'Commit': 'attributes.commit'}, - {'Started At': 'attributes.started_at'}, - {'Uptime': 'attributes.uptime'} - ]) + maxctrl(argv) + .getResource('maxscale', [ + {'Version': 'attributes.version'}, + {'Commit': 'attributes.commit'}, + {'Started At': 'attributes.started_at'}, + {'Uptime': 'attributes.uptime'} + ]) }) .command('commands ', 'Show module commands of a module', {}, function(argv) { - getSubCollection('maxscale/modules/' + argv.module, 'attributes.commands', [ - {'Command': 'id'}, - {'Parameters': 'attributes.parameters[].type'}, - {'Descriptions': 'attributes.parameters[].description'} - ]) + maxctrl(argv) + .getSubCollection('maxscale/modules/' + argv.module, 'attributes.commands', [ + {'Command': 'id'}, + {'Parameters': 'attributes.parameters[].type'}, + {'Descriptions': 'attributes.parameters[].description'} + ]) }) .usage('Usage: show ') .help() diff --git a/maxctrl/lib/start.js b/maxctrl/lib/start.js index db44cbd09..ed24b8e89 100644 --- a/maxctrl/lib/start.js +++ b/maxctrl/lib/start.js @@ -18,10 +18,12 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('service ', 'Start a service', {}, function(argv) { - doRequest('services/' + argv.name + '/start', null, {method: 'PUT'}) + maxctrl(argv) + .doRequest('services/' + argv.name + '/start', null, {method: 'PUT'}) }) .command('monitor ', 'Start a monitor', {}, function(argv) { - doRequest('monitors/' + argv.name + '/start', null, {method: 'PUT'}) + maxctrl(argv) + .doRequest('monitors/' + argv.name + '/start', null, {method: 'PUT'}) }) .usage('Usage: start ') .help() diff --git a/maxctrl/lib/stop.js b/maxctrl/lib/stop.js index 477818fcb..fffd82f51 100644 --- a/maxctrl/lib/stop.js +++ b/maxctrl/lib/stop.js @@ -18,10 +18,12 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('service ', 'Stop a service', {}, function(argv) { - doRequest('services/' + argv.name + '/stop', null, {method: 'PUT'}) + maxctrl(argv) + .doRequest('services/' + argv.name + '/stop', null, {method: 'PUT'}) }) .command('monitor ', 'Stop a monitor', {}, function(argv) { - doRequest('monitors/' + argv.name + '/stop', null, {method: 'PUT'}) + maxctrl(argv) + .doRequest('monitors/' + argv.name + '/stop', null, {method: 'PUT'}) }) .usage('Usage: stop ') .help() diff --git a/maxctrl/lib/unlink.js b/maxctrl/lib/unlink.js index 0bd50abdc..e8f38e5f4 100644 --- a/maxctrl/lib/unlink.js +++ b/maxctrl/lib/unlink.js @@ -12,20 +12,22 @@ */ require('../common.js')() -function removeServer(path, targets) { - doRequest(path, function(res) { - var servers =_.get(res, 'data.relationships.servers.data', []) +function removeServer(argv, path, targets) { + maxctrl(argv) + .doRequest(path, function(res) { + var servers =_.get(res, 'data.relationships.servers.data', []) - _.remove(servers, function(i) { - return targets.indexOf(i.id) != -1 + _.remove(servers, function(i) { + return targets.indexOf(i.id) != -1 + }) + + // Update relationships and remove unnecessary parts + _.set(res, 'data.relationships.servers.data', servers) + delete res.data.attributes + + maxctrl(argv) + .doRequest(path, null, {method: 'PATCH', body: res}) }) - - // Update relationships and remove unnecessary parts - _.set(res, 'data.relationships.servers.data', servers) - delete res.data.attributes - - doRequest(path, null, {method: 'PATCH', body: res}) - }) } exports.command = 'unlink ' @@ -34,10 +36,10 @@ exports.handler = function() {} exports.builder = function(yargs) { yargs .command('service ', 'Unlink servers from a service', {}, function(argv) { - removeServer('services/' + argv.name, argv.server) + removeServer(argv, 'services/' + argv.name, argv.server) }) .command('monitor ', 'Unlink servers from a monitor', {}, function(argv) { - removeServer('monitors/' + argv.name, argv.server) + removeServer(argv, 'monitors/' + argv.name, argv.server) }) .usage('Usage: unlink ') .help() diff --git a/maxctrl/maxctrl.js b/maxctrl/maxctrl.js index cc3fb7b1e..0e576355e 100644 --- a/maxctrl/maxctrl.js +++ b/maxctrl/maxctrl.js @@ -13,4 +13,18 @@ 'use strict'; -require('./core.js')(process.argv) +var argv = process.argv + +// Mangle the arguments if we are being called from the command line +if (argv[0] == process.execPath) { + argv.shift() + // The first argument is always the script + argv.shift() +} + +require('./core.js')(argv) + .then(function(output){ + if (output.length > 0) { + console.log(output) + } + }, console.log)