diff --git a/app.js b/app.js index 164923b..cf9156f 100755 --- a/app.js +++ b/app.js @@ -28,8 +28,9 @@ app.get(routes.root, function(req, res) { //crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *"); crontab.crontabs( function(docs){ res.render('index', { - routes : routes, - crontabs : JSON.stringify(docs) + routes : JSON.stringify(routes), + crontabs : JSON.stringify(docs), + backups : crontab.get_backup_names() }); }); }) @@ -46,6 +47,29 @@ app.post(routes.save, function(req, res) { res.end(); }) +app.post(routes.stop, function(req, res) { + crontab.status(req.body._id, true); + res.end(); +}) + +app.post(routes.start, function(req, res) { + crontab.status(req.body._id, false); + res.end(); +}) + +app.post(routes.remove, function(req, res) { + crontab.remove(req.body._id); + res.end(); +}) +app.get(routes.crontab, function(req, res) { + crontab.set_crontab(); + res.end(); +}) + +app.get(routes.backup, function(req, res) { + crontab.backup(); + res.end(); +}) app.listen(app.get('port'), function() { console.log("Crontab UI is running at localhost:" + app.get('port')) }) diff --git a/crontab.js b/crontab.js index 5e0a694..7a07edf 100644 --- a/crontab.js +++ b/crontab.js @@ -24,6 +24,14 @@ exports.create_new = function(name, command, schedule){ exports.update = function(data){ db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null)); } + +exports.status = function(_id, stopped){ + db.update({_id: _id},{$set: {stopped: stopped}}); +} + +exports.remove = function(_id){ + db.remove({_id: _id}, {}); +} exports.crontabs = function(callback){ db.find({}, function(err, docs){ callback(docs); @@ -43,3 +51,20 @@ exports.set_crontab = function(){ }); } + +exports.get_backup_names = function(){ + var backups = [] + fs.readdirSync(__dirname + '/crontabs').forEach(function(file){ + // file name begins with backup + if(file.indexOf("backup") == 0){ + backups.push(file); + } + }); + + return backups; +} + +exports.backup = function(){ + //TODO check if it failed + fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString() + '.db')); +} diff --git a/public/js/script.js b/public/js/script.js index d620786..a6aaf0f 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -1,10 +1,9 @@ /*********** MessageBox ****************/ // simply show info. Only close button function infoMessageBox(message, title){ - $("#modal-body").html(message); - $("#modal-title").html(title); - $("#modal-button").hide(); - $("#popup").modal('show'); + $("#info-body").html(message); + $("#info-title").html(title); + $("#info-popup").modal('show'); } // modal with full control function messageBox(body, title, ok_text, close_text, callback){ @@ -23,23 +22,39 @@ function messageBox(body, title, ok_text, close_text, callback){ function deleteJob(_id){ // TODO fix this. pass callback properly messageBox("
Do you want to delete this Job?
", "Confirm delete", null, null, function(){ - console.log("delete job"); + $.post(routes.remove, {_id: _id}, function(){ + location.reload(); + }); }); } function stopJob(_id){ messageBox("Do you want to stop this Job?
", "Confirm stop job", null, null, function(){ - console.log("stop job"); + $.post(routes.stop, {_id: _id}, function(){ + location.reload(); + }); }); } function startJob(_id){ messageBox("Do you want to start this Job?
", "Confirm start job", null, null, function(){ - console.log("start job"); + $.post(routes.start, {_id: _id}, function(){ + location.reload(); + }); + }); +} + +function setCrontab(){ + messageBox("Do you want to set the crontab file?
", "Confirm crontab setup", null, null, function(){ + $.get(routes.crontab, {}, function(){ + // TODO show only if success + infoMessageBox("Successfuly set crontab file!","Information"); + }); }); } function editJob(_id){ + console.log(_id); var job = null; crontabs.forEach(function(crontab){ if(crontab._id == _id) @@ -48,7 +63,7 @@ function editJob(_id){ if(job){ $("#job").modal("show"); $("#job-name").val(job.name); - $("#job-command").val(job.command); + $("#job-command").val(job.command.replace("\"","\\\"")); // if macro not used if(job.schedule.indexOf("@") != 0){ var components = job.schedule.split(" "); @@ -65,7 +80,7 @@ function editJob(_id){ $("#job-save").click(function(){ // TODO good old boring validations - $.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){ + $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){ location.reload(); }) }); @@ -86,12 +101,19 @@ function newJob(){ job_string(); $("#job-save").click(function(){ // TODO good old boring validations - $.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){ + $.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){ location.reload(); }) }); } +function doBackup(){ + messageBox("Do you want to take backup?
", "Confirm backup", null, null, function(){ + $.get(routes.backup, {}, function(){ + location.reload(); + }); + }); +} // script corresponding to job popup management var schedule = ""; @@ -105,4 +127,5 @@ function set_schedule(){ schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val(); job_string(); } +// popup management ends diff --git a/routes.js b/routes.js index 05b19b0..0cdb9bf 100644 --- a/routes.js +++ b/routes.js @@ -2,5 +2,9 @@ exports.routes = { "root" : "/", "downloads" : "/downloads", "save" : "/save", - "crontab" : "/crontab" + "crontab" : "/crontab", + "stop" : "/stop", + "start" : "/start", + "remove": "/remove", + "backup": "/backup", } diff --git a/views/index.ejs b/views/index.ejs index c07e02d..62b7faa 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -7,10 +7,13 @@ @@ -61,10 +64,10 @@ New - Backup + Backup Export Import from crontab - Save to crontab + Save to crontab