mecano
Version:
Common functions for system deployment.
165 lines (158 loc) • 5.56 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var discover, merge, misc, path, 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) {
if (!((options.groups != null) || (options.mounts != null) || (options["default"] != null))) {
throw Error('Missing cgroups content');
}
if (options.mounts == null) {
options.mounts = [];
}
if (options.groups == null) {
options.groups = {};
}
if (options.merge == null) {
options.merge = true;
}
options.cgconfig = {};
options.cgconfig['mounts'] = options.mounts;
options.cgconfig['groups'] = options.groups;
if (options["default"] != null) {
options.cgconfig['groups'][''] = options["default"];
}
if (options.ignore == null) {
options.ignore = [];
}
if (!Array.isArray(options.ignore)) {
options.ignore = [options.ignore];
}
if (options.store == null) {
options.store = {};
}
this.call({
shy: true,
unless: function() {
return (options.store['mecano:system:type'] != null) && (options.store['mecano:system:release'] != null);
}
}, function() {
return this.execute({
cmd: 'cat /etc/system-release',
code_skipped: 1
}, function(err, status, stdout, stderr) {
var base, base1, base2, base3, index, line;
if (err) {
throw err;
}
line = string.lines(stdout)[0];
if (/CentOS/.test(line)) {
if ((base = options.store)['mecano:system:type'] == null) {
base['mecano:system:type'] = 'centos';
}
index = line.split(' ').indexOf('release');
if ((base1 = options.store)['mecano:system:release'] == null) {
base1['mecano:system:release'] = line.split(' ')[index + 1];
}
}
if (/Red\sHat/.test(line)) {
if ((base2 = options.store)['mecano:system:type'] == null) {
base2['mecano:system:type'] = 'redhat';
}
index = line.split(' ').indexOf('release');
if ((base3 = options.store)['mecano:system:release'] == null) {
base3['mecano:system:release'] = line.split(' ')[index + 1];
}
}
if (options.store['mecano:system:type'] == null) {
throw Error('Unsupported OS');
}
});
});
this.call({
shy: true,
"if": function() {
var ref;
return (ref = options.store['mecano:system:type']) === 'redhat' || ref === 'centos';
},
handler: function() {
return this.execute({
cmd: 'cgsnapshot -s 2>&1'
}, function(err, status, stdout, stderr) {
var base, base1, base2, base3, cgconfig, cpu_path, cpuaccts, cpus, group, groups, name, ref, ref1;
if (err) {
throw err;
}
cgconfig = misc.cgconfig.parse(stdout);
if (cgconfig.mounts == null) {
cgconfig.mounts = [];
}
cpus = cgconfig.mounts.filter(function(mount) {
if (mount.type === 'cpu') {
return mount;
}
});
cpuaccts = cgconfig.mounts.filter(function(mount) {
if (mount.type === 'cpuacct') {
return mount;
}
});
if (options.store['mecano:cgroups:cpu_path'] == null) {
if (cpus.length > 0) {
cpu_path = cpus[0]['path'].split(',')[0];
if ((base = options.store)['mecano:cgroups:cpu_path'] == null) {
base['mecano:cgroups:cpu_path'] = cpu_path;
}
} else {
switch (options.store['mecano:system:type']) {
case 'redhat':
if (options.store['mecano:system:release'][0] === '6') {
if ((base1 = options.store)['mecano:cgroups:cpu_path'] == null) {
base1['mecano:cgroups:cpu_path'] = '/cgroups/cpu';
}
}
if (options.store['mecano:system:release'][0] === '7') {
if ((base2 = options.store)['mecano:cgroups:cpu_path'] == null) {
base2['mecano:cgroups:cpu_path'] = '/sys/fs/cgroup/cpu';
}
}
break;
default:
throw Error("Mecano does not support cgroups on your OS " + options.store['mecano:system:type']);
}
}
}
if (options.store['mecano:cgroups:mount'] == null) {
if ((base3 = options.store)['mecano:cgroups:mount'] == null) {
base3['mecano:cgroups:mount'] = "" + (path.dirname(options.store['mecano:cgroups:cpu_path']));
}
}
if (options.merge) {
groups = {};
ref = cgconfig.groups;
for (name in ref) {
group = ref[name];
if (!((name.indexOf('docker/') !== -1) || (indexOf.call(options.ignore, name) >= 0))) {
groups[name] = group;
}
}
options.cgconfig.groups = merge(groups, options.groups);
return (ref1 = options.cgconfig.mounts).push.apply(ref1, cgconfig.mounts);
}
});
}
});
return this.call(function() {
if (options.store['mecano:system:type'] === 'redhat') {
if (options.target == null) {
options.target = '/etc/cgconfig.conf';
}
}
return this.file(options, {
content: misc.cgconfig.stringify(options.cgconfig)
});
});
};
misc = require('../misc');
string = require('../misc/string');
merge = misc.merge;
path = require('path');
discover = require('../misc/discover');