mecano
Version:
Common functions for system deployment.
221 lines (217 loc) • 5.93 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var discover, string;
module.exports = function(options) {
var cacheonly, installed, updates;
options.log({
message: "Entering service.install",
level: 'DEBUG',
module: 'mecano/lib/service/install'
});
if (typeof options.argument === 'string') {
if (options.name == null) {
options.name = options.argument;
}
}
options.log({
message: "Install service " + options.name,
level: 'INFO',
module: 'mecano/lib/service/install'
});
installed = updates = null;
if (options.cache) {
installed = options.store['mecano:execute:installed'];
updates = options.store['mecano:execute:updates'];
}
if (options.manager == null) {
options.manager = options.store['mecano:service:manager'];
}
if (!options.name) {
throw Error("Invalid Name: " + (JSON.stringify(options.name)));
}
cacheonly = options.cacheonly ? '-C' : '';
this.call(discover.system);
this.execute({
cmd: "if which yum >/dev/null; then exit 1; fi\nif which apt-get >/dev/null; then exit 2; fi\nexit 3",
code: [1, 2],
unless: options.manager,
shy: true
}, function(err, status, stdout, stderr, signal) {
if ((err != null ? err.code : void 0) === 3) {
throw Error("Undetected Package Manager");
}
if (err) {
throw err;
}
if (!status) {
return;
}
options.manager = (function() {
switch (signal) {
case 1:
return 'yum';
case 2:
return 'apt';
}
})();
if (options.cache) {
return options.store['mecano:service:manager'] = options.manager;
}
});
this.execute({
cmd: function() {
switch (options.manager) {
case 'yum':
return 'rpm -qa --qf "%{NAME}\n"';
case 'apt':
return 'dpkg -l | grep \'^ii\' | awk \'{print $2}\'';
default:
throw Error("Invalid Manager: " + options.manager);
}
},
code_skipped: 1,
stdout_log: false,
shy: true,
unless: installed != null
}, function(err, executed, stdout) {
var pkg;
if (err) {
throw err;
}
if (!executed) {
return;
}
options.log({
message: "Installed packages retrieved",
level: 'INFO',
module: 'mecano/service/install'
});
return installed = (function() {
var i, len, ref, results;
ref = string.lines(stdout);
results = [];
for (i = 0, len = ref.length; i < len; i++) {
pkg = ref[i];
results.push(pkg);
}
return results;
})();
});
this.execute({
cmd: function() {
switch (options.manager) {
case 'yum':
return "yum " + cacheonly + " list updates";
case 'apt':
case 'apt-get':
return "apt-get -u upgrade --assume-no | grep '^\\s' | sed 's/\\s/\\n/g'";
default:
throw Error("Invalid Manager: " + options.manager);
}
},
code_skipped: 1,
stdout_log: false,
shy: true,
unless: updates != null,
"if": function() {
return installed.indexOf(options.name) === -1;
}
}, function(err, executed, stdout) {
var pkg, start;
if (err) {
throw err;
}
if (!executed) {
return updates = [];
}
options.log({
message: "Available updates retrieved",
level: 'INFO',
module: 'mecano/service/install'
});
start = false;
return updates = (function() {
var i, len, ref, results;
switch (options.manager) {
case 'yum':
ref = string.lines(stdout);
results = [];
for (i = 0, len = ref.length; i < len; i++) {
pkg = ref[i];
if (pkg.trim() === 'Updated Packages') {
start = true;
}
if (!start) {
continue;
}
if (!(pkg = /^([^\. ]+?)\./.exec(pkg))) {
continue;
}
results.push(pkg[1]);
}
return results;
break;
case 'apt':
return string.lines(stdout.trim());
}
})();
});
this.execute({
cmd: function() {
switch (options.manager) {
case 'yum':
return "yum install -y " + cacheonly + " " + options.name;
case 'apt':
return "apt-get install -y " + options.name;
default:
throw Error("Invalid Manager: " + options.manager);
}
},
code_skipped: options.code_skipped,
"if": function() {
return installed.indexOf(options.name) === -1 || updates.indexOf(options.name) !== -1;
}
}, function(err, succeed) {
var installedIndex, updatesIndex;
if (err) {
throw err;
}
options.log(succeed ? {
message: "Package \"" + options.name + "\" is installed",
level: 'WARN',
module: 'mecano/service/install'
} : {
message: "Package \"" + options.name + "\" is already installed",
level: 'WARN',
module: 'mecano/service/install'
});
installedIndex = installed.indexOf(options.name);
if (installedIndex === -1) {
installed.push(options.name);
}
if (updates) {
updatesIndex = updates.indexOf(options.name);
if (updatesIndex !== -1) {
return updates.splice(updatesIndex, 1);
}
}
});
return this.call({
"if": options.cache,
handler: function() {
options.log({
message: "Caching installed on \"mecano:execute:installed\"",
level: 'INFO',
module: 'mecano/service/install'
});
options.store['mecano:execute:installed'] = installed;
options.log({
message: "Caching updates on \"mecano:execute:updates\"",
level: 'INFO',
module: 'mecano/service/install'
});
return options.store['mecano:execute:updates'] = updates;
}
});
};
string = require('../misc/string');
discover = require('../misc/discover');