Add service action support stop/start/restart for Redhat distros https://github.com/Akkadius/glass-isc-dhcp/issues/6

This commit is contained in:
Akkadius 2018-08-14 01:27:05 -05:00
parent e558f1b97e
commit 3d30b00e96
1 changed files with 35 additions and 19 deletions

View File

@ -1,10 +1,9 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js'); var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js'); var authorize = require('../lib/authorize.js');
router.get('/', authorize.auth, function(req, res, next) { router.get('/', authorize.auth, function (req, res, next) {
var content = ""; var content = "";
@ -13,11 +12,11 @@ router.get('/', authorize.auth, function(req, res, next) {
var exec = require('child_process').exec; var exec = require('child_process').exec;
exec(' ps aux | grep dhcpd | grep -v "grep"', function(err, stdout, stderr) { exec('ps aux | grep dhcpd | grep -v "grep"', function (err, stdout, stderr) {
is_running = 0; is_running = 0;
var result = stdout.split("\n"); var result = stdout.split("\n");
for (var i = 0; i < result.length; i++) { for (var i = 0; i < result.length; i++) {
if(/dhcpd/i.test(result[i])){ if (/dhcpd/i.test(result[i])) {
is_running = 1; is_running = 1;
} }
// console.log('line ' + result[i]); // console.log('line ' + result[i]);
@ -25,7 +24,7 @@ router.get('/', authorize.auth, function(req, res, next) {
var return_content = ""; var return_content = "";
if(is_running){ if (is_running) {
return_content = return_content + 'DHCP Server is online!<br><br>'; return_content = return_content + 'DHCP Server is online!<br><br>';
return_content = return_content + '<button type="button" onclick="process_action(\'stop\')" class="btn btn-default waves-effect">Stop Server</button> '; return_content = return_content + '<button type="button" onclick="process_action(\'stop\')" class="btn btn-default waves-effect">Stop Server</button> ';
} }
@ -35,37 +34,54 @@ router.get('/', authorize.auth, function(req, res, next) {
} }
return_content = return_content + '<button type="button" onclick="process_action(\'restart\')" class="btn btn-default waves-effect">Restart Server</button> '; return_content = return_content + '<button type="button" onclick="process_action(\'restart\')" class="btn btn-default waves-effect">Restart Server</button> ';
content = template_render.set_template_variable(content, "c_content", return_content);
content = template_render.set_template_variable(content, "c_content", return_content);
res.send(template_render.get_index_template(content, req.url)); res.send(template_render.get_index_template(content, req.url));
}); });
}); });
router.post('/', authorize.auth, function(req, res, next) { router.post('/', authorize.auth, function (req, res, next) {
var request = req.body; var request = req.body;
const execSync = require('child_process').execSync; const execSync = require('child_process').execSync;
/**
* Get distribution
*/
const distro = execSync('cat /etc/issue | head -n 1').toString();
/**
* Base service name - Known to Ubuntu/Debian
*
* @type {string}
*/
let service_name = "isc-dhcp-server";
/**
* Redhat derivatives use a different service name
*/
if (/Redhat|CentOS|Fedora/i.test(distro)) {
service_name = "dhcpd";
}
/**
* Execute service action and return javascript in request
*/
switch (request.action) { switch (request.action) {
case "stop": case "stop":
dhcp_exec = execSync('/usr/sbin/service isc-dhcp-server stop && /bin/sleep 1'); dhcp_exec = execSync('/usr/sbin/service ' + service_name + ' stop && /bin/sleep 1');
res.send("<script type='text/javascript'>notification('DHCP Server Stopped');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>"); res.send("<script type='text/javascript'>notification('DHCP Server Stopped');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>");
break; break;
case "start": case "start":
dhcp_exec = execSync('/usr/sbin/service isc-dhcp-server start'); dhcp_exec = execSync('/usr/sbin/service ' + service_name + ' start');
res.send("<script type='text/javascript'>notification('DHCP Server Started');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');</script>"); res.send("<script type='text/javascript'>notification('DHCP Server Started');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');</script>");
break; break;
case "restart": case "restart":
dhcp_exec = execSync('/usr/sbin/service isc-dhcp-server restart && /bin/sleep 1'); dhcp_exec = execSync('/usr/sbin/service ' + service_name + ' restart && /bin/sleep 1');
res.send("<script type='text/javascript'>notification('DHCP Server Restarted " + dhcp_exec + "');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>"); res.send("<script type='text/javascript'>notification('DHCP Server Restarted " + dhcp_exec + "');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>");
break; break;
default: default:
break; break;
} }
console.log(request);
}); });
module.exports = router; module.exports = router;