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.
This commit is contained in:
Markus Mäkelä
2017-07-13 19:54:19 +03:00
parent 67b2654f50
commit 816e317485
18 changed files with 271 additions and 211 deletions

View File

@ -18,8 +18,11 @@ var Table = require('cli-table');
module.exports = function() { module.exports = function() {
this._ = require('lodash-getpath') 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 // Request a resource collection and format it as a table
this.getCollection = function (resource, fields) { this.getCollection = function (resource, fields) {
@ -114,11 +117,10 @@ module.exports = function() {
} }
// Helper for converting endpoints to acutal URLs // Helper for converting endpoints to acutal URLs
this.getUri = function(host, endpoint) { this.getUri = function(host, secure, endpoint) {
var base = 'http://' var base = 'http://'
var argv = this.program.argv
if (argv.secure) { if (secure) {
base = 'https://' base = 'https://'
} }
@ -127,12 +129,12 @@ module.exports = function() {
// Helper for executing requests and handling their responses // Helper for executing requests and handling their responses
this.doRequest = function(resource, cb, obj) { this.doRequest = function(resource, cb, obj) {
pingCluster() pingCluster(this.argv.hosts)
.then(function() { .then(function() {
var argv = this.program.argv var argv = this.argv
argv.hosts.forEach(function(host) { argv.hosts.forEach(function(host) {
args = obj || {} args = obj || {}
args.uri = getUri(host, resource) args.uri = getUri(host, argv.secure, resource)
args.json = true args.json = true
args.timeout = argv.timeout args.timeout = argv.timeout
@ -206,10 +208,10 @@ function pingMaxScale(host) {
}) })
} }
function pingCluster() { function pingCluster(hosts) {
var promises = [] var promises = []
this.program.argv.hosts.forEach(function(i) { hosts.forEach(function(i) {
promises.push(pingMaxScale(i)) promises.push(pingMaxScale(i))
}) })

View File

@ -10,24 +10,16 @@
* of this software will be governed by version 2 or later of the General * of this software will be governed by version 2 or later of the General
* Public License. * Public License.
*/ */
require('./common.js')()
var fs = require('fs') var fs = require('fs')
var program = require('yargs');
const maxctrl_version = '1.0.0'; const maxctrl_version = '1.0.0';
require('./common.js')()
module.exports = function(argv) { 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) { return new Promise(function(resolve, reject) {
program program
.version(maxctrl_version) .version(maxctrl_version)

View File

@ -18,19 +18,24 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('server <server> <key> <value>', 'Alter server parameters', {}, function(argv) { .command('server <server> <key> <value>', '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 <monitor> <key> <value>', 'Alter monitor parameters', {}, function(argv) { .command('monitor <monitor> <key> <value>', '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 <service> <key> <value>', 'Alter service parameters', {}, function(argv) { .command('service <service> <key> <value>', '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 <key> <value>', 'Alter logging parameters', {}, function(argv) { .command('logging <key> <value>', '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 <key> <value>', 'Alter MaxScale parameters', {}, function(argv) { .command('maxscale <key> <value>', '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 <command>') .usage('Usage: alter <command>')
.help() .help()

View File

@ -20,22 +20,24 @@ exports.builder = function(yargs) {
.command('command <module> <command> [parameters...]', 'Call a module command', {}, function(argv) { .command('command <module> <command> [parameters...]', 'Call a module command', {}, function(argv) {
// First we have to find the correct method to use // 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 // A GET request will return the correct error if the command is not found
var verb = 'GET' var verb = 'GET'
resp.data.attributes.commands.forEach(function(i) { resp.data.attributes.commands.forEach(function(i) {
if (i.id == argv.command) { if (i.id == argv.command) {
verb = i.attributes.method; 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 <command>') .usage('Usage: call <command>')
.help() .help()

View File

@ -19,7 +19,8 @@ exports.builder = function(yargs) {
yargs yargs
.command('server <server> <state>', 'Clear server state', {}, function(argv) { .command('server <server> <state>', 'Clear server state', {}, function(argv) {
var target = 'servers/' + argv.server + '/clear?state=' + argv.state var target = 'servers/' + argv.server + '/clear?state=' + argv.state
doRequest(target, null, {method: 'PUT'}) maxctrl(argv)
.doRequest(target, null, {method: 'PUT'})
}) })
.usage('Usage: clear <command>') .usage('Usage: clear <command>')
.help() .help()

View File

@ -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 // 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 // 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 <name> <password>', 'Create a new network user', {}, function(argv) { .command('user <name> <password>', '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 <command>') .usage('Usage: create <command>')

View File

@ -18,16 +18,20 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('server <name>', 'Destroy an unused server', {}, function(argv) { .command('server <name>', 'Destroy an unused server', {}, function(argv) {
doRequest('servers/' + argv.name, null, {method: 'DELETE'}) maxctrl(argv)
.doRequest('servers/' + argv.name, null, {method: 'DELETE'})
}) })
.command('monitor <name>', 'Destroy an unused monitor', {}, function(argv) { .command('monitor <name>', 'Destroy an unused monitor', {}, function(argv) {
doRequest('monitors/' + argv.name, null, {method: 'DELETE'}) maxctrl(argv)
.doRequest('monitors/' + argv.name, null, {method: 'DELETE'})
}) })
.command('listener <service> <name>', 'Destroy an unused listener', {}, function(argv) { .command('listener <service> <name>', '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 <name>', 'Remove a network user', {}, function(argv) { .command('user <name>', '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 <command>') .usage('Usage: destroy <command>')
.help() .help()

View File

@ -26,19 +26,24 @@ exports.builder = function(yargs) {
yargs yargs
.command('log-priority <log>', 'Disable log priority [warning|notice|info|debug]', {}, function(argv) { .command('log-priority <log>', 'Disable log priority [warning|notice|info|debug]', {}, function(argv) {
if (log_levels.indexOf(argv.log) != -1) { 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 { } else {
logError('Invalid log priority: ' + argv.log); maxctrl(argv)
.logError('Invalid log priority: ' + argv.log);
} }
}) })
.command('maxlog', 'Disable MaxScale logging', {}, function(argv) { .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) { .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 <name>', 'Disable a Linux user account from administrative use', {}, function(argv) { .command('account <name>', '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 <command>') .usage('Usage: disable <command>')
.help() .help()

View File

@ -26,16 +26,20 @@ exports.builder = function(yargs) {
yargs yargs
.command('log-priority <log>', 'Enable log priority [warning|notice|info|debug]', {}, function(argv) { .command('log-priority <log>', 'Enable log priority [warning|notice|info|debug]', {}, function(argv) {
if (log_levels.indexOf(argv.log) != -1) { 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 { } else {
logError('Invalid log priority: ' + argv.log); maxctrl(argv)
.logError('Invalid log priority: ' + argv.log);
} }
}) })
.command('maxlog', 'Enable MaxScale logging', {}, function(argv) { .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) { .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 <name>', 'Activate a Linux user account for administrative use', {}, function(argv) { .command('account <name>', 'Activate a Linux user account for administrative use', {}, function(argv) {
var req_body = { var req_body = {
@ -44,7 +48,8 @@ exports.builder = function(yargs) {
type: 'unix' 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 <command>') .usage('Usage: enable <command>')
.help() .help()

View File

@ -12,20 +12,22 @@
*/ */
require('../common.js')() require('../common.js')()
function addServer(path, targets) { function addServer(argv, path, targets) {
doRequest(path, function(res) { maxctrl(argv)
var servers =_.get(res, 'data.relationships.servers.data', []) .doRequest(path, function(res) {
var servers =_.get(res, 'data.relationships.servers.data', [])
targets.forEach(function(i){ targets.forEach(function(i){
servers.push({id: i, type: 'servers'}) 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 <command>' exports.command = 'link <command>'
@ -34,10 +36,10 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('service <name> <server...>', 'Link servers to a service', {}, function(argv) { .command('service <name> <server...>', 'Link servers to a service', {}, function(argv) {
addServer('services/' + argv.name, argv.server) addServer(argv, 'services/' + argv.name, argv.server)
}) })
.command('monitor <name> <server...>', 'Link servers to a monitor', {}, function(argv) { .command('monitor <name> <server...>', 'Link servers to a monitor', {}, function(argv) {
addServer('monitors/' + argv.name, argv.server) addServer(argv, 'monitors/' + argv.name, argv.server)
}) })
.usage('Usage: link <command>') .usage('Usage: link <command>')
.help() .help()

View File

@ -18,63 +18,71 @@ exports.desc = 'List objects'
exports.handler = function() {} exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('servers', 'List servers', {}, function() { .command('servers', 'List servers', {}, function(argv) {
getCollection('servers', [ maxctrl(argv)
{'Server': 'id'}, .getCollection('servers', [
{'Address': 'attributes.parameters.address'}, {'Server': 'id'},
{'Port': 'attributes.parameters.port'}, {'Address': 'attributes.parameters.address'},
{'Connections': 'attributes.statistics.connections'}, {'Port': 'attributes.parameters.port'},
{'State': 'attributes.state'} {'Connections': 'attributes.statistics.connections'},
]) {'State': 'attributes.state'}
])
}) })
.command('services', 'List services', {}, function() { .command('services', 'List services', {}, function(argv) {
getCollection('services',[ maxctrl(argv)
{'Service': 'id'}, .getCollection('services',[
{'Router': 'attributes.router'}, {'Service': 'id'},
{'Connections': 'attributes.connections'}, {'Router': 'attributes.router'},
{'Total Connections': 'attributes.total_connections'}, {'Connections': 'attributes.connections'},
{'Servers': 'relationships.servers.data[].id'} {'Total Connections': 'attributes.total_connections'},
]) {'Servers': 'relationships.servers.data[].id'}
])
}) })
.command('monitors', 'List monitors', {}, function() { .command('monitors', 'List monitors', {}, function(argv) {
getCollection('monitors', [ maxctrl(argv)
{'Monitor': 'id'}, .getCollection('monitors', [
{'State': 'attributes.state'}, {'Monitor': 'id'},
{'Servers': 'relationships.servers.data[].id'} {'State': 'attributes.state'},
]) {'Servers': 'relationships.servers.data[].id'}
])
}) })
.command('sessions', 'List sessions', {}, function() { .command('sessions', 'List sessions', {}, function(argv) {
getCollection('sessions',[ maxctrl(argv)
{'Id': 'id'}, .getCollection('sessions',[
{'Service': 'relationships.services.data[].id'}, {'Id': 'id'},
{'User': 'attributes.user'}, {'Service': 'relationships.services.data[].id'},
{'Host': 'attributes.remote'} {'User': 'attributes.user'},
]) {'Host': 'attributes.remote'}
])
}) })
.command('filters', 'List filters', {}, function() { .command('filters', 'List filters', {}, function(argv) {
getCollection('filters', [ maxctrl(argv)
{'Filter': 'id'}, .getCollection('filters', [
{'Service': 'relationships.services.data[].id'}, {'Filter': 'id'},
{'Module': 'attributes.module'} {'Service': 'relationships.services.data[].id'},
]) {'Module': 'attributes.module'}
])
}) })
.command('modules', 'List loaded modules', {}, function() { .command('modules', 'List loaded modules', {}, function(argv) {
getCollection('maxscale/modules',[ maxctrl(argv)
{'Module':'id'}, .getCollection('maxscale/modules',[
{'Type':'attributes.module_type'}, {'Module':'id'},
{'Version': 'attributes.version'} {'Type':'attributes.module_type'},
]) {'Version': 'attributes.version'}
])
}) })
.command('users', 'List created network users', {}, function() { .command('users', 'List created network users', {}, function(argv) {
getCollection('users/inet',[ maxctrl(argv)
{'Name':'id'} .getCollection('users/inet',[
]) {'Name':'id'}
])
}) })
.command('commands', 'List module commands', {}, function() { .command('commands', 'List module commands', {}, function(argv) {
getCollection('maxscale/modules',[ maxctrl(argv)
{'Module':'id'}, .getCollection('maxscale/modules',[
{'Commands': 'attributes.commands[].id'} {'Module':'id'},
]) {'Commands': 'attributes.commands[].id'}
])
}) })
.usage('Usage: list <command>') .usage('Usage: list <command>')
.help() .help()

View File

@ -18,7 +18,8 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('logs', 'Rotate log files by closing and reopening the files', {}, function(argv) { .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 <command>') .usage('Usage: rotate <command>')
.help() .help()

View File

@ -19,7 +19,8 @@ exports.builder = function(yargs) {
yargs yargs
.command('server <server> <state>', 'Set server state', {}, function(argv) { .command('server <server> <state>', 'Set server state', {}, function(argv) {
var target = 'servers/' + argv.server + '/set?state=' + argv.state var target = 'servers/' + argv.server + '/set?state=' + argv.state
doRequest(target, null, {method: 'PUT'}) maxctrl(argv)
.doRequest(target, null, {method: 'PUT'})
}) })
.usage('Usage: set <command>') .usage('Usage: set <command>')
.help() .help()

View File

@ -19,86 +19,94 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('server <server>', 'Show server', {}, function(argv) { .command('server <server>', 'Show server', {}, function(argv) {
getResource('servers/' + argv.server, [ maxctrl(argv)
{'Server': 'id'}, .getResource('servers/' + argv.server, [
{'Address': 'attributes.parameters.address'}, {'Server': 'id'},
{'Port': 'attributes.parameters.port'}, {'Address': 'attributes.parameters.address'},
{'State': 'attributes.state'}, {'Port': 'attributes.parameters.port'},
{'Services': 'relationships.services.data[].id'}, {'State': 'attributes.state'},
{'Monitors': 'relationships.monitors.data[].id'}, {'Services': 'relationships.services.data[].id'},
{'Master ID': 'attributes.master_id'}, {'Monitors': 'relationships.monitors.data[].id'},
{'Node ID': 'attributes.node_id'}, {'Master ID': 'attributes.master_id'},
{'Slave Server IDs': 'attributes.slaves'}, {'Node ID': 'attributes.node_id'},
{'Statistics': 'attributes.statistics'}, {'Slave Server IDs': 'attributes.slaves'},
{'Parameters': 'attributes.parameters'} {'Statistics': 'attributes.statistics'},
]) {'Parameters': 'attributes.parameters'}
])
}) })
.command('service <service>', 'Show service', {}, function(argv) { .command('service <service>', 'Show service', {}, function(argv) {
getResource('services/' + argv.service, [ maxctrl(argv)
{'Service': 'id'}, .getResource('services/' + argv.service, [
{'Router': 'attributes.router'}, {'Service': 'id'},
{'State': 'attributes.state'}, {'Router': 'attributes.router'},
{'Started At': 'attributes.started'}, {'State': 'attributes.state'},
{'Current Connections': 'attributes.connections'}, {'Started At': 'attributes.started'},
{'Total Connections': 'attributes.total_connections'}, {'Current Connections': 'attributes.connections'},
{'Servers': 'relationships.servers.data[].id'}, {'Total Connections': 'attributes.total_connections'},
{'Parameters': 'attributes.parameters'}, {'Servers': 'relationships.servers.data[].id'},
{'Router Diagnostics': 'attributes.router_diagnostics'} {'Parameters': 'attributes.parameters'},
]) {'Router Diagnostics': 'attributes.router_diagnostics'}
])
}) })
.command('monitor <monitor>', 'Show monitor', {}, function(argv) { .command('monitor <monitor>', 'Show monitor', {}, function(argv) {
getResource('monitors/' + argv.monitor, [ maxctrl(argv)
{'Monitor': 'id'}, .getResource('monitors/' + argv.monitor, [
{'State': 'attributes.state'}, {'Monitor': 'id'},
{'Servers': 'relationships.servers.data[].id'}, {'State': 'attributes.state'},
{'Parameters': 'attributes.parameters'}, {'Servers': 'relationships.servers.data[].id'},
{'Monitor Diagnostics': 'attributes.monitor_diagnostics'} {'Parameters': 'attributes.parameters'},
]) {'Monitor Diagnostics': 'attributes.monitor_diagnostics'}
])
}) })
.command('session <session>', 'Show session', {}, function(argv) { .command('session <session>', 'Show session', {}, function(argv) {
getResource('sessions/' + argv.session, [ maxctrl(argv)
{'Id': 'id'}, .getResource('sessions/' + argv.session, [
{'Service': 'relationships.services.data[].id'}, {'Id': 'id'},
{'State': 'attributes.state'}, {'Service': 'relationships.services.data[].id'},
{'User': 'attributes.user'}, {'State': 'attributes.state'},
{'Host': 'attributes.remote'}, {'User': 'attributes.user'},
{'Connected': 'attributes.connected'}, {'Host': 'attributes.remote'},
{'Idle': 'attributes.idle'} {'Connected': 'attributes.connected'},
]) {'Idle': 'attributes.idle'}
])
}) })
.command('filter <filter>', 'Show filter', {}, function(argv) { .command('filter <filter>', 'Show filter', {}, function(argv) {
getResource('filters/' + argv.filter, [ maxctrl(argv)
{'Filter': 'id'}, .getResource('filters/' + argv.filter, [
{'Module': 'attributes.module'}, {'Filter': 'id'},
{'Services': 'relationships.services.data[].id'}, {'Module': 'attributes.module'},
{'Parameters': 'attributes.parameters'} {'Services': 'relationships.services.data[].id'},
]) {'Parameters': 'attributes.parameters'}
])
}) })
.command('module <module>', 'Show loaded module', {}, function(argv) { .command('module <module>', 'Show loaded module', {}, function(argv) {
getResource('maxscale/modules/' + argv.module, [ maxctrl(argv)
{'Module': 'id'}, .getResource('maxscale/modules/' + argv.module, [
{'Type': 'attributes.module_type'}, {'Module': 'id'},
{'Version': 'attributes.version'}, {'Type': 'attributes.module_type'},
{'Maturity': 'attributes.maturity'}, {'Version': 'attributes.version'},
{'Description': 'attributes.description'}, {'Maturity': 'attributes.maturity'},
{'Parameters': 'attributes.parameters'}, {'Description': 'attributes.description'},
{'Commands': 'attributes.commands'} {'Parameters': 'attributes.parameters'},
]) {'Commands': 'attributes.commands'}
])
}) })
.command('maxscale', 'Show MaxScale information', {}, function(argv) { .command('maxscale', 'Show MaxScale information', {}, function(argv) {
getResource('maxscale', [ maxctrl(argv)
{'Version': 'attributes.version'}, .getResource('maxscale', [
{'Commit': 'attributes.commit'}, {'Version': 'attributes.version'},
{'Started At': 'attributes.started_at'}, {'Commit': 'attributes.commit'},
{'Uptime': 'attributes.uptime'} {'Started At': 'attributes.started_at'},
]) {'Uptime': 'attributes.uptime'}
])
}) })
.command('commands <module>', 'Show module commands of a module', {}, function(argv) { .command('commands <module>', 'Show module commands of a module', {}, function(argv) {
getSubCollection('maxscale/modules/' + argv.module, 'attributes.commands', [ maxctrl(argv)
{'Command': 'id'}, .getSubCollection('maxscale/modules/' + argv.module, 'attributes.commands', [
{'Parameters': 'attributes.parameters[].type'}, {'Command': 'id'},
{'Descriptions': 'attributes.parameters[].description'} {'Parameters': 'attributes.parameters[].type'},
]) {'Descriptions': 'attributes.parameters[].description'}
])
}) })
.usage('Usage: show <command>') .usage('Usage: show <command>')
.help() .help()

View File

@ -18,10 +18,12 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('service <name>', 'Start a service', {}, function(argv) { .command('service <name>', '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 <name>', 'Start a monitor', {}, function(argv) { .command('monitor <name>', '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 <command>') .usage('Usage: start <command>')
.help() .help()

View File

@ -18,10 +18,12 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('service <name>', 'Stop a service', {}, function(argv) { .command('service <name>', '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 <name>', 'Stop a monitor', {}, function(argv) { .command('monitor <name>', '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 <command>') .usage('Usage: stop <command>')
.help() .help()

View File

@ -12,20 +12,22 @@
*/ */
require('../common.js')() require('../common.js')()
function removeServer(path, targets) { function removeServer(argv, path, targets) {
doRequest(path, function(res) { maxctrl(argv)
var servers =_.get(res, 'data.relationships.servers.data', []) .doRequest(path, function(res) {
var servers =_.get(res, 'data.relationships.servers.data', [])
_.remove(servers, function(i) { _.remove(servers, function(i) {
return targets.indexOf(i.id) != -1 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 <command>' exports.command = 'unlink <command>'
@ -34,10 +36,10 @@ exports.handler = function() {}
exports.builder = function(yargs) { exports.builder = function(yargs) {
yargs yargs
.command('service <name> <server...>', 'Unlink servers from a service', {}, function(argv) { .command('service <name> <server...>', 'Unlink servers from a service', {}, function(argv) {
removeServer('services/' + argv.name, argv.server) removeServer(argv, 'services/' + argv.name, argv.server)
}) })
.command('monitor <name> <server...>', 'Unlink servers from a monitor', {}, function(argv) { .command('monitor <name> <server...>', 'Unlink servers from a monitor', {}, function(argv) {
removeServer('monitors/' + argv.name, argv.server) removeServer(argv, 'monitors/' + argv.name, argv.server)
}) })
.usage('Usage: unlink <command>') .usage('Usage: unlink <command>')
.help() .help()

View File

@ -13,4 +13,18 @@
'use strict'; '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)