mecano
Version:
Common functions for system deployment.
133 lines (130 loc) • 4.22 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var regexp;
module.exports = function(options, callback) {
var write;
options.log({
message: "Entering system_limits",
level: 'DEBUG',
module: 'mecano/lib/system_limits'
});
if (options.system && options.user) {
return callback(Error("Incoherent options: both options system and user defined, " + (JSON.stringify({
system: options.system,
user: options.user
}))));
}
if (options.system) {
options.user = '*';
}
if (!options.user) {
return callback(Error("Missing required option 'user'"));
}
if (options.target == null) {
options.target = "/etc/security/" + (options.user === '*' ? "limits.conf" : "limits.d/" + options.user + ".conf");
}
write = [];
return this.execute({
cmd: "cat /proc/sys/fs/file-max",
shy: true,
"if": options.nofile != null
}, function(err, status, stdout) {
var _, kern_limit, ref, results, v;
if (err) {
throw err;
}
if (!status) {
return;
}
kern_limit = parseInt(stdout.trim());
if (options.nofile === true) {
return options.nofile = Math.round(kern_limit * 0.75);
} else if (typeof options.nofile === 'number') {
if (options.nofile >= kern_limit) {
throw Error("Invalid nofile options. Please set int value lesser than kernel limit: " + kern_limit);
}
} else if (typeof options.nofile === 'object') {
ref = options.nofile;
results = [];
for (_ in ref) {
v = ref[_];
if (v >= kern_limit) {
throw Error("Invalid nofile options. Please set int value lesser than kernel limit: " + kern_limit);
} else {
results.push(void 0);
}
}
return results;
}
}).execute({
cmd: "cat /proc/sys/kernel/pid_max",
shy: true,
"if": options.nproc != null
}, function(err, status, stdout) {
var _, kern_limit, ref, results, v;
if (err) {
throw err;
}
if (!status) {
return;
}
kern_limit = parseInt(stdout.trim());
if (options.nproc === true) {
return options.nproc = Math.round(kern_limit * 0.75);
} else if (typeof options.nproc === 'number') {
if (options.nproc >= kern_limit) {
throw Error("Invalid nproc options. Please set int value lesser than kernel limit: " + kern_limit);
}
} else if (typeof options.nproc === 'object') {
ref = options.nproc;
results = [];
for (_ in ref) {
v = ref[_];
if (v >= kern_limit) {
throw Error("Invalid nproc options. Please set int value lesser than kernel limit: " + kern_limit);
} else {
results.push(void 0);
}
}
return results;
}
}).call(function() {
var i, j, k, len, len1, opt, ref, ref1;
ref = ['as', 'core', 'cpu', 'data', 'fsize', 'locks', 'maxlogins', 'maxsyslogins', 'memlock', 'msgqueue', 'nice', 'nofile', 'nproc', 'priority', 'rss', 'sigpending', 'stack', 'rtprio'];
for (i = 0, len = ref.length; i < len; i++) {
opt = ref[i];
if (options[opt] != null) {
if (typeof options[opt] !== 'object') {
options[opt] = {
'-': options[opt]
};
}
ref1 = Object.keys(options[opt]);
for (j = 0, len1 = ref1.length; j < len1; j++) {
k = ref1[j];
if (k !== 'soft' && k !== 'hard' && k !== '-') {
throw Error("Invalid option: " + (JSON.stringify(options[opt])));
}
if (!((typeof options[opt][k] === 'number') || options[opt][k] === 'unlimited')) {
throw Error("Invalid option: " + options[opt][k] + " not a number");
}
write.push({
match: RegExp("^" + (regexp.escape(options.user)) + " +" + (regexp.escape(k)) + " +" + opt + ".+$", 'm'),
replace: options.user + " " + k + " " + opt + " " + options[opt][k],
append: true
});
}
}
}
return false;
}).file({
target: options.target,
write: write,
eof: true,
uid: options.uid,
gid: options.gid,
"if": function() {
return write.length;
}
}).then(callback);
};
regexp = require('../misc').regexp;