Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ribalba opened this issue Apr 15, 2015 · 7 comments
Open

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

ribalba opened this issue Apr 15, 2015 · 7 comments

Comments

@ribalba
Copy link

ribalba commented Apr 15, 2015

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]
@pstadler
Copy link
Owner

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.

@paul42
Copy link

paul42 commented May 15, 2015

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.

@StevenLangbroek
Copy link

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

@pstadler
Copy link
Owner

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

@paul42
Copy link

paul42 commented Jun 10, 2015

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.

@blub0hr
Copy link

blub0hr commented Jan 15, 2016

You can programmatically run Flightplan

I feel this should be mentioned in the docs.

@ralphholzmann
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants