flightplan icon indicating copy to clipboard operation
flightplan copied to clipboard

Would be nice to be able to execute code in target plans.

Open ribalba opened this issue 9 years ago • 7 comments

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]

ribalba avatar Apr 15 '15 15:04 ribalba

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.

pstadler avatar Apr 16 '15 14:04 pstadler

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.

paul42 avatar May 15 '15 21:05 paul42

+1 on control tower. would love that and integrate it with Slack here.

StevenLangbroek avatar Jun 10 '15 16:06 StevenLangbroek

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

pstadler avatar Jun 10 '15 17:06 pstadler

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.

paul42 avatar Jun 10 '15 20:06 paul42

You can programmatically run Flightplan

I feel this should be mentioned in the docs.

blub0hr avatar Jan 15 '16 13:01 blub0hr

Just wanted to +1 the API change to task(name[, deps], function(local, remote) {}). This is exactly what I'm looking for right now.

ralphholzmann avatar Mar 01 '16 12:03 ralphholzmann