king
Version:
A powerful server infrastructure management platform - "The King of your Nodes"
118 lines (100 loc) • 3.45 kB
JavaScript
// Generated by CoffeeScript 1.6.3
var Base, List, ServantServer, capabilities, fork, helper, spawn, upnode, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
_ref = require("child_process"), spawn = _ref.spawn, fork = _ref.fork;
upnode = require("upnode");
Base = require("../common/base");
List = require("../common/list");
helper = require("../common/helper");
capabilities = require("./capabilities");
ServantServer = (function(_super) {
__extends(ServantServer, _super);
ServantServer.prototype.name = "ServantServer";
function ServantServer(kingHost) {
this.kingAddr = helper.host.parse(kingHost);
this.id = helper.guid();
this.procs = new List;
this.status = 'idle';
helper.bindAll(this);
capabilities.calculate(this.gotCapabilties);
}
ServantServer.prototype.gotCapabilties = function(capabilities) {
var _this = this;
this.capabilities = capabilities;
this.api = this.makeApi();
this.comms = upnode(this.api);
this.log("connecting to: " + this.kingAddr.host + ":" + this.kingAddr.port + "...");
this.d = this.comms.connect(this.kingAddr.port, this.kingAddr.host);
this.d.on('remote', this.onRemote);
this.d.on('error', this.onError);
this.d.on('up', function() {
return _this.setStatus('up');
});
this.d.on('down', function() {
return _this.setStatus('down');
});
return this.d.on('reconnect', function() {
return _this.setStatus('connecting');
});
};
ServantServer.prototype.onRemote = function(remote) {
this.log("connected to server");
return this.remote = remote;
};
ServantServer.prototype.onError = function(e) {
return this.log("connection error!", e);
};
ServantServer.prototype.setStatus = function(s) {
if (s !== this.status) {
this.log("status " + this.status + " -> " + s);
}
return this.status = s;
};
ServantServer.prototype.makeApi = function() {
var _this = this;
return {
id: this.id,
capabilities: this.capabilities,
exec: function(cmd, callback) {
var args, proc, program;
_this.log("executing: '" + cmd + "'");
args = cmd.split(/\s+/);
program = args.shift();
proc = program === 'node' ? fork(args) : spawn(program, args);
_this.procs.add(proc);
proc.stdout.on('data', function(buff) {
return callback({
type: 'stdout',
msg: buff.toString()
});
});
proc.stderr.on('data', function(buff) {
return callback({
type: 'stderr',
msg: buff.toString()
});
});
proc.on('close', function(code) {
callback({
type: 'close',
code: code
});
return _this.procs.remove(proc);
});
proc.on('error', function(err) {
callback({
type: 'error',
err: err.toString()
});
return _this.procs.remove(proc);
});
return null;
}
};
};
return ServantServer;
})(Base);
exports.start = function(kingHost) {
return new ServantServer(kingHost);
};