diff options
author | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-16 16:13:33 +0200 |
---|---|---|
committer | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-16 16:13:33 +0200 |
commit | 5b2a63f65edd13f182787ebb997ceaa493f61665 (patch) | |
tree | 8769496b970cdd374358e60cf91cb397874a6627 | |
parent | 948b1e9558479846c2ddd52be5483ab8b44be77c (diff) |
Add config read-api and use it to detect public access
Currently no way to modify the settings except by manual SQL, but it's
coming.
This allows us to have event-specific configuration in the database. This
commit adds js that uses this to detect if the frontend is a public vhost
or not.
There are currently only three columns provided in addition to ID.
publicvhost: The domain name of the publicly accessible version of Gondul.
shortname: code name for the vent. e.g dx16, tg17, etc.
data: jsonb to contain most other settings as we see fit.
Most settings will be stored in 'data' if it's only or mainly used by the
frontend. This will allow frontend developers to supply arbitrary
configuration options without the need for API or database changes in the
future.
Fixes #51
References #54
Still need the GUI for it.
-rw-r--r-- | build/schema.sql | 53 | ||||
-rwxr-xr-x | web/api/public/config | 28 | ||||
-rw-r--r-- | web/js/nms.js | 46 |
3 files changed, 121 insertions, 6 deletions
diff --git a/build/schema.sql b/build/schema.sql index 4742575..4012eb3 100644 --- a/build/schema.sql +++ b/build/schema.sql @@ -4,7 +4,7 @@ SET statement_timeout = 0; SET lock_timeout = 0; -SET client_encoding = 'UTF8'; +SET client_encoding = 'SQL_ASCII'; SET standard_conforming_strings = on; SET check_function_bodies = false; SET client_min_messages = warning; @@ -44,6 +44,41 @@ SET default_tablespace = ''; SET default_with_oids = false; -- +-- Name: config; Type: TABLE; Schema: public; Owner: nms; Tablespace: +-- + +CREATE TABLE config ( + id integer NOT NULL, + publicvhost character varying, + shortname character varying, + data jsonb +); + + +ALTER TABLE config OWNER TO nms; + +-- +-- Name: config_id_seq; Type: SEQUENCE; Schema: public; Owner: nms +-- + +CREATE SEQUENCE config_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE config_id_seq OWNER TO nms; + +-- +-- Name: config_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: nms +-- + +ALTER SEQUENCE config_id_seq OWNED BY config.id; + + +-- -- Name: dhcp; Type: TABLE; Schema: public; Owner: nms; Tablespace: -- @@ -306,6 +341,13 @@ CREATE TABLE test_table ( ALTER TABLE test_table OWNER TO nms; -- +-- Name: id; Type: DEFAULT; Schema: public; Owner: nms +-- + +ALTER TABLE ONLY config ALTER COLUMN id SET DEFAULT nextval('config_id_seq'::regclass); + + +-- -- Name: linknet; Type: DEFAULT; Schema: public; Owner: nms -- @@ -558,6 +600,15 @@ GRANT ALL ON SCHEMA public TO PUBLIC; -- +-- Name: config; Type: ACL; Schema: public; Owner: nms +-- + +REVOKE ALL ON TABLE config FROM PUBLIC; +REVOKE ALL ON TABLE config FROM nms; +GRANT ALL ON TABLE config TO nms; + + +-- -- Name: dhcp; Type: ACL; Schema: public; Owner: nms -- diff --git a/web/api/public/config b/web/api/public/config new file mode 100755 index 0000000..dd8ccc1 --- /dev/null +++ b/web/api/public/config @@ -0,0 +1,28 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use DBI; +use lib '/opt/gondul/include'; +use nms; +use nms::web; +use strict; +use warnings; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "3600"; + +my $hostname = $ENV{'HTTP_HOST'} || ""; +my $q2 = $nms::web::dbh->prepare('select id, publicvhost, shortname, data from config order by id desc limit 1;'); + +$q2->execute(); +while (my $ref = $q2->fetchrow_hashref()) { + $nms::web::json{'config'} = $ref; + $nms::web::json{'config'}{'data'} = JSON::XS::decode_json($ref->{'data'}); + if ($ref->{'publicvhost'} eq $hostname) { + $nms::web::json{'config'}{'public'} = "true"; + } else { + $nms::web::json{'config'}{'public'} = "false"; + } +} + +finalize_output(); diff --git a/web/js/nms.js b/web/js/nms.js index 1f7398b..5df9431 100644 --- a/web/js/nms.js +++ b/web/js/nms.js @@ -548,6 +548,35 @@ function setNightMode(toggle) { } /* + * Only used to fetch the initial config for anything that needs to be + * handled prior to regular "boot up". + * + * For the moment, that only means detecting if we're being run on a public + * vhost or not. This has to be done in synch because it affects what + * sources we register for nmsData[]. If we wait for nmsData['config'], + * it's too late because all other things have been initialized already. + * + * If you add a configuration setting, use nmsData['config'] as much as + * possible. Avoid adding to this function. + */ +function getInitialConfig() { + $.ajax({ + type: "GET", + url: "/api/public/config", + async: false, + dataType: "json", + success: function (data, textStatus, jqXHR) { + if (data["config"]["public"] == "true") { + nms._public = true; + } else { + console.log("Private"); + nms._public = false; + } + } + }); +} + +/* * Boot up "fully fledged" NMS. * * This can be re-written to provide different looks and feels but using @@ -558,12 +587,17 @@ function initNMS() { nms.timers.playback = new nmsTimer(nms.playback.tick, 1000, "Playback ticker", "Handler used to advance time"); // Public + nmsData.registerSource("config","/api/public/config"); nmsData.registerSource("ping", "/api/public/ping"); nmsData.registerSource("switches","/api/public/switches"); nmsData.registerSource("switchstate","/api/public/switch-state"); nmsData.registerSource("dhcpsummary","/api/public/dhcp-summary"); nmsData.registerSource("dhcp","/api/public/dhcp"); - + + // Fetch initial config. Basically just populates nms._public. + // All other settings are kept in nmsData['config']. + getInitialConfig(); + // This is a magic dummy-source, it's purpose is to give a unified // way to get ticks every second. It is mainly meant to allow map // handlers to register for ticks so they will execute without data @@ -571,10 +605,12 @@ function initNMS() { // despite no pings) nmsData.registerSource("ticker","bananabananbanana"); - // Private - nmsData.registerSource("snmp","/api/read/snmp"); - nmsData.registerSource("comments", "/api/read/comments"); - nmsData.registerSource("smanagement","/api/read/switches-management"); + if (!nms._public) { + // Private + nmsData.registerSource("snmp","/api/read/snmp"); + nmsData.registerSource("comments", "/api/read/comments"); + nmsData.registerSource("smanagement","/api/read/switches-management"); + } restoreSettings(); nmsMap.init(); |