mecano
Version:
Common functions for system deployment.
111 lines (106 loc) • 2.93 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var connect, fs, misc;
module.exports = {
handler: function(options) {
var base, base1, k, ref, ref1, v;
options.log({
message: "Entering ssh.open",
level: 'DEBUG',
module: 'mecano/lib/ssh/open'
});
if (options.ssh == null) {
options.ssh = {};
}
ref = options.ssh;
for (k in ref) {
v = ref[k];
if (options[k] == null) {
options[k] = v;
}
}
if (options.username == null) {
options.username = 'root';
}
if (options.host == null) {
options.host = options.ip;
}
if (options.port == null) {
options.port = 22;
}
if (options.private_key == null) {
options.private_key = null;
}
if (options.private_key_path == null) {
options.private_key_path = '~/.ssh/id_rsa';
}
if (options.root == null) {
options.root = {};
}
if ((base = options.root).host == null) {
base.host = options.ip || options.host;
}
if ((base1 = options.root).port == null) {
base1.port = options.port;
}
if (((ref1 = this.options.ssh) != null ? ref1.config : void 0) && this.options.ssh.config.host === options.host && this.options.ssh.config.port === options.port && this.options.ssh.config.username === options.username) {
return;
}
this.call({
unless: options.private_key
}, function(_, callback) {
return misc.path.normalize(options.private_key_path, (function(_this) {
return function(location) {
return fs.readFile(location, 'ascii', function(err, content) {
if (err && err.code === 'ENOENT') {
return callback(Error("Private key doesnt exists: " + (JSON.stringify(location))));
}
if (err) {
return callback(err);
}
options.private_key = content;
return callback();
});
};
})(this));
});
this.call({
relax: true
}, function(_, callback) {
return connect(options, (function(_this) {
return function(err, ssh) {
if (!err) {
_this.options.ssh = ssh;
}
return callback(err, !!ssh);
};
})(this));
}, function(err, status) {
if (!err) {
return this.end();
}
});
this.call({
"if": options.root.username,
unless: options.ssh
}, function() {
return this.ssh.root({
public_key: options.public_key
}, options.root);
});
return this.call({
retry: 3
}, function(_, callback) {
return connect(options, (function(_this) {
return function(err, ssh) {
if (!err) {
_this.options.ssh = ssh;
}
return callback(err);
};
})(this));
});
}
};
fs = require('fs');
misc = require('../misc');
connect = require('ssh2-connect');