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

Added support for windows control machine using cygwin. #34

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 68 additions & 13 deletions lib/ansible.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var utils = require('./utils');
var Q = require('q');
var EventEmitter = require('events').EventEmitter;
var slice = [].slice;
//Are we on a Windows control machine?
var isWin = /^win/.test(process.platform);

var AbstractAnsibleCommand = function() {
EventEmitter.call(this);
Expand Down Expand Up @@ -37,8 +39,8 @@ AbstractAnsibleCommand.prototype.exec = function(options) {
_.extend( processOptions.env, { PYTHONUNBUFFERED: unbuffered } );

var output = '';
var child = exec.spawn(this.commandName(), this.compileParams(), processOptions);

var child = exec.spawn(this.commandName(), this.compileParams(), processOptions);
child.stdout.on('data', function(data) {
output += data.toString();
self.emit('stdout', data);
Expand All @@ -63,6 +65,16 @@ AbstractAnsibleCommand.prototype.exec = function(options) {
return deferred.promise;
};

AbstractAnsibleCommand.prototype.cygwinCommand = function(cygwinCommand) {
this.config.cygwinCommand = cygwinCommand;
return this;
};

AbstractAnsibleCommand.prototype.ansiblePath = function(ansiblePath) {
this.config.ansiblePath = ansiblePath;
return this;
};

AbstractAnsibleCommand.prototype.forks = function(forks) {
this.config.forks = forks;
return this;
Expand Down Expand Up @@ -153,12 +165,26 @@ var AdHoc = function() {

AbstractAnsibleCommand.call(this);

this.commandName = function() {
return 'ansible';
}

this.config = {};

this.ansibleCommand = function() {
var command = '';
if(!_.isUndefined(this.config.ansiblePath)){
command = this.config.ansiblePath + 'ansible';
}else{
command = 'ansible';
}
return command;
}

this.commandName = function() {
if(!isWin){
return this.ansibleCommand();
}else{
return this.config.cygwinCommand;
}
}

this.module = function (module) {
this.config.module = module;
return this;
Expand Down Expand Up @@ -203,6 +229,10 @@ var AdHoc = function() {
'-m',
this.config.module
];

if(isWin){
result.unshift(this.ansibleCommand());
}

if (this.config.args || this.config.freeform) {
var args = utils.formatArgs(this.config.args, this.config.freeform);
Expand All @@ -211,9 +241,12 @@ var AdHoc = function() {

result = this.commonCompileParams(result);

return result;
if(isWin){
return ['-lc', result.join(' ')];
}else{
return result;
}
}

}

inherits(AdHoc, AbstractAnsibleCommand);
Expand All @@ -222,12 +255,26 @@ var Playbook = function () {

AbstractAnsibleCommand.call(this);

this.config = {};

this.ansibleCommand = function() {
var command = '';
if(!_.isUndefined(this.config.ansiblePath)){
command = this.config.ansiblePath + 'ansible-playbook';
}else{
command = 'ansible-playbook';
}
return command;
}

this.commandName = function() {
return 'ansible-playbook';
if(!isWin){
return this.ansibleCommand();
}else{
return this.config.cygwinCommand;
}
}

this.config = {};

this.askPass = function() {
this.config.askPass = true;
return this;
Expand Down Expand Up @@ -288,9 +335,13 @@ var Playbook = function () {
this.config.playbook + ".yml"
];

if(isWin){
result.unshift(this.ansibleCommand());
}

if (this.config.variables) {
var args = JSON.stringify(this.config.variables);
result = result.concat('-e', args);
var args = JSON.stringify(this.config.variables, null, ' ');
result = result.concat('-e', '"' + args.replace(/(\r\n|\r|\n)/gm,"").replace(/(})/gm, " }") + '"');
}

if (this.config.askPass) {
Expand All @@ -313,7 +364,11 @@ var Playbook = function () {

result = this.commonCompileParams(result);

return result;
if(isWin){
return ['-lc', result.join(' ')];
}else{
return result;
}
}
}

Expand Down