|
|
|
@ -1,21 +1,29 @@
|
|
|
|
|
//load database
|
|
|
|
|
|
|
|
|
|
exports.crontab_db_file = __dirname + '/crontabs/crontab.db';
|
|
|
|
|
exports.templates_db_file = __dirname + '/crontabs/templates.db';
|
|
|
|
|
|
|
|
|
|
var Datastore = require('nedb');
|
|
|
|
|
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
|
|
|
|
|
db.loadDatabase(function (err) {
|
|
|
|
|
});
|
|
|
|
|
var db = {
|
|
|
|
|
crontabs: new Datastore({ filename: exports.crontab_db_file, autoload: true }),
|
|
|
|
|
templates: new Datastore({ filename: exports.templates_db_file, autoload: true }),
|
|
|
|
|
};
|
|
|
|
|
var exec = require('child_process').exec;
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
var cron_parser = require("cron-parser")
|
|
|
|
|
var os = require("os")
|
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
|
|
|
|
|
exports.log_folder = __dirname + '/crontabs/logs';
|
|
|
|
|
exports.env_file = __dirname + '/crontabs/env.db';
|
|
|
|
|
|
|
|
|
|
crontab = function(name, command, schedule, stopped, logging){
|
|
|
|
|
crontab = function(name, command, command_template, vars, schedule, stopped, logging){
|
|
|
|
|
var data = {};
|
|
|
|
|
data.name = name;
|
|
|
|
|
data.command = command;
|
|
|
|
|
data.command_template = command_template;
|
|
|
|
|
data.schedule = schedule;
|
|
|
|
|
data.vars = vars || {};
|
|
|
|
|
if(stopped != null) {
|
|
|
|
|
data.stopped = stopped;
|
|
|
|
|
}
|
|
|
|
@ -24,34 +32,88 @@ crontab = function(name, command, schedule, stopped, logging){
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.create_new = function(name, command, schedule, logging){
|
|
|
|
|
var tab = crontab(name, command, schedule, false, logging);
|
|
|
|
|
crontab_template = function(name, command, schedule){
|
|
|
|
|
var data = {};
|
|
|
|
|
data.name = name;
|
|
|
|
|
data.command = command;
|
|
|
|
|
data.schedule = schedule;
|
|
|
|
|
data.timestamp = (new Date()).toString();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.create_new = function(name, command, command_template, vars, schedule, logging){
|
|
|
|
|
var tab = crontab(name, command, command_template, vars, schedule, false, logging);
|
|
|
|
|
tab.created = new Date().valueOf();
|
|
|
|
|
db.insert(tab);
|
|
|
|
|
db.crontabs.insert(tab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.update = function(data){
|
|
|
|
|
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null, data.logging));
|
|
|
|
|
db.crontabs.update({_id: data._id}, crontab(data.name, data.command, data.command_template, data.vars, data.schedule, null, data.logging));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.status = function(_id, stopped){
|
|
|
|
|
db.update({_id: _id},{$set: {stopped: stopped}});
|
|
|
|
|
db.crontabs.update({_id: _id},{$set: {stopped: stopped}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.remove = function(_id){
|
|
|
|
|
db.remove({_id: _id}, {});
|
|
|
|
|
}
|
|
|
|
|
exports.crontabs = function(callback){
|
|
|
|
|
db.find({}).sort({ created: -1 }).exec(function(err, docs){
|
|
|
|
|
for(var i=0; i<docs.length; i++){
|
|
|
|
|
if(docs[i].schedule == "@reboot")
|
|
|
|
|
docs[i].next = "Next Reboot"
|
|
|
|
|
else
|
|
|
|
|
docs[i].next = cron_parser.parseExpression(docs[i].schedule).next().toString();
|
|
|
|
|
}
|
|
|
|
|
callback(docs);
|
|
|
|
|
db.crontabs.remove({_id: _id}, {});
|
|
|
|
|
}
|
|
|
|
|
exports.crontabs = function(callback) {
|
|
|
|
|
exports.templates(function(templates) {
|
|
|
|
|
db.crontabs.find({}).sort({ name: 1 }).exec(function(err, docs){
|
|
|
|
|
for(var i=0; i<docs.length; i++){
|
|
|
|
|
var doc = docs[i];
|
|
|
|
|
|
|
|
|
|
if(doc.command_template) {
|
|
|
|
|
var template = _.findWhere(templates, { _id: doc.command_template });
|
|
|
|
|
|
|
|
|
|
if(template) {
|
|
|
|
|
doc.command = exports.renderTemplateCommand(template, doc);
|
|
|
|
|
doc.schedule = template.schedule;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(doc.schedule == "@reboot")
|
|
|
|
|
doc.next = "Next Reboot"
|
|
|
|
|
else if(doc.schedule)
|
|
|
|
|
doc.next = cron_parser.parseExpression(doc.schedule).next().toString();
|
|
|
|
|
}
|
|
|
|
|
callback(docs);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.templates = function(callback){
|
|
|
|
|
db.templates.find({}).sort({ name: 1 }).exec(function(err, templates){
|
|
|
|
|
callback(templates);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.renderTemplateCommand = function(template, cronjob) {
|
|
|
|
|
var vars = cronjob.vars || {};
|
|
|
|
|
|
|
|
|
|
return template.command.replace(/{[a-zA-Z]+}/g, function(s) {
|
|
|
|
|
return vars[s.substring(1, s.length - 1)] || 'undefined';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.create_new_template = function(name, command, schedule){
|
|
|
|
|
var tab = crontab_template(name, command, schedule);
|
|
|
|
|
tab.created = new Date().valueOf();
|
|
|
|
|
db.templates.insert(tab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.update_template = function(data){
|
|
|
|
|
db.templates.update({_id: data._id}, crontab_template(data.name, data.command, data.schedule));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.remove_template = function(_id) {
|
|
|
|
|
db.templates.remove({_id: _id}, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.set_crontab = function(env_vars){
|
|
|
|
|
exports.crontabs( function(tabs){
|
|
|
|
|
var crontab_string = "";
|
|
|
|
@ -108,18 +170,39 @@ exports.get_backup_names = function(){
|
|
|
|
|
return backups;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.backup = function(){
|
|
|
|
|
exports.backup_data = function() {
|
|
|
|
|
//TODO check if it failed
|
|
|
|
|
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db'));
|
|
|
|
|
var crontabData = fs.readFileSync( exports.crontab_db_file ).toString('utf8')
|
|
|
|
|
var templateData = fs.readFileSync( exports.templates_db_file ).toString('utf8')
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
version: 1,
|
|
|
|
|
crontabs: crontabData,
|
|
|
|
|
templates: templateData,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.backup = function(){
|
|
|
|
|
var backupFile = __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db';
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(backupFile, JSON.stringify(exports.backup_data()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.restore = function(db_name){
|
|
|
|
|
fs.createReadStream( __dirname + '/crontabs/' + db_name).pipe(fs.createWriteStream( __dirname + '/crontabs/crontab.db'));
|
|
|
|
|
db.loadDatabase(); // reload the database
|
|
|
|
|
exports.restore_data(JSON.parse(fs.readFileSync( __dirname + '/crontabs/' + db_name )))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.restore_data = function(backupData) {
|
|
|
|
|
fs.writeFileSync(exports.crontab_db_file, backupData.crontabs);
|
|
|
|
|
fs.writeFileSync(exports.templates_db_file, backupData.templates);
|
|
|
|
|
|
|
|
|
|
db.crontabs.loadDatabase(); // reload the database
|
|
|
|
|
db.templates.loadDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.reload_db= function(){
|
|
|
|
|
db.loadDatabase();
|
|
|
|
|
exports.reload_db = function(){
|
|
|
|
|
db.crontabs.loadDatabase();
|
|
|
|
|
db.templates.loadDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.get_env = function(){
|
|
|
|
|