UNPKG

mecano

Version:

Common functions for system deployment.

66 lines (61 loc) 1.83 kB
// Generated by CoffeeScript 1.9.1 var escape, execute, wrap; escape = function(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }; module.exports = function(options, callback) { var crontab, jobs, ref, status; if (!(((ref = options.cmd) != null ? ref.length : void 0) > 0)) { return callback(new Error('valid cmd is required')); } if (options.user != null) { if (typeof options.log === "function") { options.log("Using user " + options.user + " [INFO]"); } crontab = "crontab -u " + options.user; } else { if (typeof options.log === "function") { options.log('Using default user [INFO]'); } crontab = "crontab"; } status = false; jobs = []; return this.execute({ cmd: crontab + " -l", shy: true }, function(err, _, stdout, stderr) { var i, j, job, len, myjob, regex; if (err) { throw err; } if (/^no crontab for/.test(stderr)) { throw Error('User crontab not found'); } myjob = options.when ? escape(options.when) : '.*'; myjob += escape(" " + options.cmd); regex = new RegExp(myjob); jobs = stdout.trim().split('\n'); for (i = j = 0, len = jobs.length; j < len; i = ++j) { job = jobs[i]; if (!regex.test(job)) { continue; } if (typeof options.log === "function") { options.log("Job '" + job + "' matches. Removing from list [WARN]"); } status = true; jobs.splice(i, 1); } if (!status) { return typeof options.log === "function" ? options.log('No Job matches. Skipping [INFO]') : void 0; } }).execute({ cmd: crontab + " - <<EOF\n" + (jobs.join('\n')) + "\nEOF", "if": function() { return status; } }).then(callback); }; wrap = require('./misc/wrap'); execute = require('./execute');