flightplan
flightplan copied to clipboard
Would be nice to be able to execute code in target plans.
It would be useful if it would be possible to execute local code in target plans as you might want to run some shell commands to figure out what hosts you have to connect to. For example to connect to a vagrant machine you need to write code like
plan.target('vagrant', function(done) {
var hosts = [];
var sys = require('sys')
var exec = require('child_process').spawnSync;
//Run vagrant in the shell to get the ssh config info
var vagrantOutputString = exec('vagrant', ['ssh-config']).stdout.toString();
var re = /IdentityFile (.*)/gm;
//Match the IdentityFile
var keyFile = re.exec(vagrantOutputString)[1];
hosts.push({
host: '127.0.0.1',
port:2222,
username: 'vagrant',
privateKey: keyFile,
agent: process.env.SSH_AUTH_SOCK
});
done(hosts);
});
which is not the nicest way. In fabric it can look like :) Just a feature request.
@task
def vagrant():
env.user = 'vagrant'
env.hosts = ['127.0.0.1:2222']
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1]
I'm planning to change the API in upcoming versions. local()
and remote()
are probably gonna be replaced with something like task(name[, deps], function(local, remote) {})
. This would solve your problem.
would we be able to export tasks? I'd love to make a command and control application and use it like rundeck, where when X is detected, fire flight plan 1, when rest call Y is received, fire flight plan 2. you could even name it something amusing like control tower. or just make a rest api that fires flight plans and call it control tower.
+1 on control tower. would love that and integrate it with Slack here.
You can programmatically run Flightplan:
// flightplan.js
var plan = module.exports = require('flightplan'); // export instance
...
// api.js
var plan = require('./flightplan'); // load instance from local flightplan.js
plan.run(task, target, options);
Is this enough? Of course you can split up your tasks and targets into multiple files. This should get you an idea:
// flightplan.js
var plan = require('flightplan'); // export with `module.exports` if wanted, see above
require('./tasks')(plan);
require('./targets')(plan);
// tasks.js and targets.js
module.exports = function(plan) {
/* setup tasks or targets here */
};
Edit: I think it's not even necessary to pass the plan there because I'm exporting always the same instance in ./lib/index.js#L409
ah, I didn't realize you could do that, for some reason I always just assumed you had to use the CLI, thanks for taking the time to clarify this for me, I appreciate it.
Just wanted to +1 the API change to task(name[, deps], function(local, remote) {})
. This is exactly what I'm looking for right now.