DHCP Config saving/syntax validation

This commit is contained in:
Akkadius 2017-09-02 05:58:59 -05:00
parent 7b8650a4fa
commit 8266e5f0c5
10 changed files with 129 additions and 12 deletions

3
app.js
View File

@ -26,6 +26,8 @@ app.use('/get_dashboard', require('./routes/dashboard'));
app.use('/get_stats', require('./routes/get_stats'));
app.use('/dhcp_leases', require('./routes/dhcp_leases'));
app.use('/dhcp_log', require('./routes/dhcp_log'));
app.use('/dhcp_config', require('./routes/dhcp_config'));
app.use('/dhcp_config_save', require('./routes/dhcp_config_save'));
app.use('/api_examples', require('./routes/api_examples'));
app.use('/glass_settings', require('./routes/glass_settings'));
app.use('/glass_settings_save', require('./routes/glass_settings_save'));
@ -176,7 +178,6 @@ var tail_dhcp_log = new tail_module(
tail_dhcp_log.on("line", function(data) {
if(listening_to_log_file) {
// console.log(data);
wss.broadcast_event(data, 'dhcp_log_subscription');
}
});

View File

@ -2,5 +2,6 @@
"admin_user": "glassadmin",
"admin_password": "glassadmin",
"leases_file": "/var/lib/dhcp/dhcpd.leases",
"log_file": "/var/lib/dhcp.log",
"config_file": "/etc/dhcp/dhcpd.conf"
}

View File

@ -0,0 +1,12 @@
var dhcp_config = ace.edit("dhcp_config");
dhcp_config.setTheme("ace/theme/terminal");
dhcp_config.$blockScrolling = Infinity;
function save_dhcp_config () {
dhcp_config_form_data = get_form_query_string("dhcp_config_form");
dhcp_config_form_data = dhcp_config_form_data + "&dhcp_config_data=" + encodeURIComponent(dhcp_config.getValue());
$.post( "/dhcp_config_save", dhcp_config_form_data, function( data ) {
$( "#dhcp_config_result" ).html( data );
});
}

View File

@ -0,0 +1,35 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
[title]
</h2>
</div>
<div class="body">
[c_content]
<form id="dhcp_config_form">
<label>DHCP Config</label>
<div class="form-group">
<div class="form-line">
<input disabled type="text" id="dhcp_config_location" class="form-control" value="[dhcp_config_location]">
</div>
</div>
<div id="dhcp_config" style="width:100%; height:800px; color: #95cd24">[dhcp_config_content]</div>
<br>
<button type="button" class="btn btn-info waves-effect" onclick="save_dhcp_config()"><i class="material-icons">system_update_alt</i> <span>Save Config</span></button>
</form>
<div id="dhcp_config_result"></div>
</div>
</div>
</div>
</div>
<script src="assets/js/ace_editor/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/ace_editor/theme-terminal.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/glass-pages/page-dhcp-config.js" type="text/javascript" charset="utf-8"></script>

View File

@ -27,8 +27,6 @@
<div id="dhcp_log" style="width:100%; height:800px; color: #95cd24"></div>
</form>
</div>
</div>
</div>
@ -36,4 +34,4 @@
<script src="assets/js/ace_editor/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/ace_editor/theme-terminal.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/page-dhcp-logs.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/glass-pages/page-dhcp-logs.js" type="text/javascript" charset="utf-8"></script>

View File

@ -91,6 +91,14 @@
</a>
</li>
<li class="header">Admin</li>
<li>
<a href="/dhcp_config" pjax="1">
<i class="material-icons">mode_edit</i>
<span>DHCP Config</span>
</a>
</li>
<li class="header">Settings</li>
<li>
<a href="/glass_settings" pjax="1">
@ -99,13 +107,7 @@
</a>
</li>
<li class="header">Admin</li>
<li>
<a href="/dhcp_config" pjax="1">
<i class="material-icons">mode_edit</i>
<span>DHCP Config</span>
</a>
</li>
<li class="header">Glass API</li>
<li>
@ -180,6 +182,7 @@
<script src="assets/plugins/jquery-datatable/skin/bootstrap/js/dataTables.bootstrap.js"></script>
<script src="assets/plugins/jquery-datatable/extensions/export/dataTables.buttons.min.js"></script>
<script type="text/javascript">
function get_dashboard() {

26
routes/dhcp_config.js Normal file
View File

@ -0,0 +1,26 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
router.get('/', function(req, res, next) {
var content = "";
content = template_render.get_template("dhcp_config");
/* Read Config */
var json_file = require('jsonfile');
var glass_config = json_file.readFileSync('config/glass_config.json');
content = template_render.set_template_variable(content, "title", "DHCP Config");
content = template_render.set_template_variable(content, "c_content", "");
content = template_render.set_template_variable(content, "dhcp_config_location", glass_config.config_file);
var dhcp_config = fs.readFileSync(glass_config.config_file, 'utf8');
content = template_render.set_template_variable(content, "dhcp_config_content", dhcp_config);
res.send(template_render.get_index_template(content, req.url));
});
module.exports = router;

View File

@ -0,0 +1,41 @@
/**
* Created by cmiles on 8/9/2017.
*/
var express = require('express');
var router = express.Router();
router.post('/', function(req, res, next) {
var request = req.body;
fs.writeFileSync("./syntax_verify_config", request.dhcp_config_data, 'utf8');
var exec = require('child_process').exec;
exec('dhcpd -t -cf ./syntax_verify_config > verify_output 2> verify_output', function(err, stdout, stderr)
{
var output = fs.readFileSync('./verify_output', "utf8");
if (err) {
output = output.replace("\n", "<br>");
res.send('<script type="text/javascript">notification(\'There are errors!<br>See below for details...\');</script><br></b><b style="color:red">' + output + '</b>');
return;
}
else {
res.send('<script type="text/javascript">notification(' + JSON.stringify("<b>Syntax OK</b> <br><br>" + output) + ')</script>');
/* Read Config */
var json_file = require('jsonfile');
var glass_config = json_file.readFileSync('config/glass_config.json');
fs.writeFileSync(glass_config.config_file, request.dhcp_config_data, 'utf8');
fs.unlinkSync("./verify_output");
fs.unlinkSync("./syntax_verify_config");
}
});
});
module.exports = router;

View File

@ -10,7 +10,7 @@ router.post('/', function(req, res, next) {
var json_file = require('jsonfile');
json_file.writeFile('./config/glass_config.json', request, {spaces: 2}, function(err) {
console.error(err)
console.error(err);
});
res.send('<script type="text/javascript">notification(\'Saved Config!\')</script>');