UNPKG

mecano

Version:

Common functions for system deployment.

246 lines (242 loc) 7.2 kB
// Generated by CoffeeScript 1.9.1 var service_startup, string, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; module.exports = function(options, callback) { var chkname, do_action, do_finish, do_install, do_installed, do_started, do_startuped, do_updates, installed, modified, pkgname, srvname, updates; installed = updates = null; pkgname = options.yum_name || options.name; chkname = options.chk_name || options.srv_name || options.name; srvname = options.srv_name || options.chk_name || options.name; modified = false; if (options.cache) { if (options.db == null) { options.db = {}; } installed = options.db['mecano:execute:installed']; updates = options.db['mecano:execute:updates']; } if (typeof options.action === 'string') { options.action = options.action.split(','); } do_installed = (function(_this) { return function() { var cache, decide; if (!pkgname) { return do_startuped(); } cache = function() { var c; if (typeof options.log === "function") { options.log("Mecano `service: list installed [DEBUG]"); } c = options.cache ? '-C' : ''; return _this.execute({ ssh: options.ssh, cmd: "yum " + c + " list installed", code_skipped: 1, stdout: null, stderr: null }, function(err, executed, stdout) { var i, len, pkg, start; if (err) { return callback(err); } stdout = string.lines(stdout); start = false; installed = []; for (i = 0, len = stdout.length; i < len; i++) { pkg = stdout[i]; if (pkg.trim() === 'Installed Packages') { start = true; } if (!start) { continue; } if (pkg = /^([^\. ]+?)\./.exec(pkg)) { installed.push(pkg[1]); } } return decide(); }); }; decide = function() { if (installed.indexOf(pkgname) !== -1) { return do_updates(); } else { return do_install(); } }; if (installed) { return decide(); } else { return cache(); } }; })(this); do_updates = (function(_this) { return function() { var cache, decide; cache = function() { var c; if (typeof options.log === "function") { options.log("Mecano `service`: list available updates [DEBUG]"); } c = options.cache ? '-C' : ''; return _this.execute({ cmd: "yum " + c + " list updates", code_skipped: 1 }, function(err, executed, stdout) { var i, len, pkg, start; if (err) { return callback(err); } stdout = string.lines(stdout); start = false; updates = []; for (i = 0, len = stdout.length; i < len; i++) { pkg = stdout[i]; if (pkg.trim() === 'Updated Packages') { start = true; } if (!start) { continue; } if (pkg = /^([^\. ]+?)\./.exec(pkg)) { updates.push(pkg[1]); } } return decide(); }); }; decide = function() { if (updates.indexOf(pkgname) !== -1) { return do_install(); } else { if (typeof options.log === "function") { options.log("Mecano `service`: No available update for '" + pkgname + "' [INFO]"); } return do_startuped(); } }; if (updates) { return decide(); } else { return cache(); } }; })(this); do_install = (function(_this) { return function() { if (typeof options.log === "function") { options.log("Mecano `service`: install '" + pkgname + "' [INFO]"); } return _this.execute({ ssh: options.ssh, cmd: "yum install -y " + pkgname }, function(err, succeed) { var installedIndex, updatesIndex; if (err) { return callback(err); } installedIndex = installed.indexOf(pkgname); if (installedIndex === -1) { installed.push(pkgname); } if (updates) { updatesIndex = updates.indexOf(pkgname); if (updatesIndex !== -1) { updates.splice(updatesIndex, 1); } } if (!succeed) { if (typeof options.log === "function") { options.log("Mecano `service`: No package available for '" + pkgname + "' [ERROR]"); } return callback(new Error("No package available for '" + pkgname + "'.")); } if (installedIndex !== -1) { modified = true; } return do_startuped(); }); }; })(this); do_startuped = (function(_this) { return function() { if (options.startup == null) { return do_started(); } return _this.service_startup({ name: chkname, startup: options.startup, "if": options.startup != null }, function(err, startuped) { if (err) { return callback(err); } modified = startuped; return do_started(); }); }; })(this); do_started = (function(_this) { return function() { if (!options.action) { return do_finish(); } if (typeof options.log === "function") { options.log("Mecano `service`: check if started"); } return _this.execute({ cmd: "service " + srvname + " status", code_skipped: [3, 1] }, function(err, started) { if (err) { return callback(err); } if (started) { if (indexOf.call(options.action, 'stop') >= 0) { return do_action('stop'); } if (indexOf.call(options.action, 'restart') >= 0) { return do_action('restart'); } } else { if (indexOf.call(options.action, 'start') >= 0) { return do_action('start'); } } return do_finish(); }); }; })(this); do_action = (function(_this) { return function(action) { if (!options.action) { return do_finish(); } if (typeof options.log === "function") { options.log("Mecano `service`: " + action + " service"); } return _this.execute({ cmd: "service " + srvname + " " + action }, function(err, executed) { if (err) { return callback(err); } modified = true; return do_finish(); }); }; })(this); do_finish = function() { if (options.cache) { options.db['mecano:execute:installed'] = installed; options.db['mecano:execute:updates'] = updates; } return callback(null, modified); }; return do_installed(); }; service_startup = require('./service_startup'); string = require('./misc/string');