manman-sync init

This commit is contained in:
Maciej Krüger 2021-12-15 06:36:39 +01:00 committed by Alexander List
parent 74b1044e4d
commit 703a51b3b9
11 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,13 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-config-mode-manman-sync
PKG_VERSION:=2
include ../gluon.mk
define Package/gluon-config-mode-manman-sync
TITLE:=Sync location data from manman
DEPENDS:=+gluon-config-mode-core +luci-lib-ip +gluon-manman-sync
endef
$(eval $(call BuildPackageGluon,gluon-config-mode-manman-sync))

View File

@ -0,0 +1,25 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: 2021-12-15 06:29+0100\n"
"Last-Translator: Cyrus Fox <cyrus@lambdacore.de>\n"
"Language-Team: German\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.0\n"
msgid "Enable ManMan sync"
msgstr "ManMan synchronisierung aktivieren"
msgid "ManMan location ID"
msgstr "ManMan Knoten ID"
msgid ""
"Sync data from ManMan by entering ManMan location id here.\n"
"This will automatically keep name, location and ips in sync with the values "
"specified in ManMan."
msgstr ""

View File

@ -0,0 +1,11 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2015-08-12 23:30+0100\n"
"Last-Translator:Tobias Bernot <tqbs@airmail.cc>\n"
"Language-Team: French\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

View File

@ -0,0 +1,15 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "Enable ManMan sync"
msgstr ""
msgid "ManMan location ID"
msgstr ""
msgid ""
"Sync data from ManMan "
"by entering ManMan location id here.\n"
"This will automatically keep name, location and ips "
"in sync with the values specified in ManMan."
msgstr ""

View File

@ -0,0 +1,17 @@
local site_i18n = i18n 'gluon-site'
local uci = require("simple-uci").cursor()
local msg
if uci:get_bool("gluon", "manman_sync", "enabled") then
msg = site_i18n._translate('gluon-config-mode:manman')
else
msg = site_i18n._translate('gluon-config-mode:no-manman')
end
if not msg then return end
renderer.render_string(msg, {
node_id = uci:get("gluon", "manman_sync", "node_id")
})

View File

@ -0,0 +1,36 @@
return function(form, uci)
if active_vpn == nil then
return
end
local pkg_i18n = i18n 'gluon-config-mode-manman-sync'
local msg = pkg_i18n.translate(
'Sync data from ManMan ' ..
'by entering ManMan location id here.\n' ..
'This will automatically keep name, location and ips ' ..
'in sync with the values specified in ManMan.'
)
local s = form:section(Section, nil, msg)
local o
local manman = s:option(Flag, "manman_sync", pkg_i18n.translate("Enable ManMan sync"))
manman.default = uci:get_bool("gluon", "manman_sync", "enabled")
function manman:write(data)
uci:set("gluon", "manman_sync", "enabled", data)
end
local id = s:option(Value, "manman_id", pkg_i18n.translate("ManMan location ID"))
id:depends(manman, true)
id.default = uci:get("gluon", "manman_sync", "node_id")
id.datatype = "nfloat" -- TODO: int
function id:write(data)
uci:set("gluon", "manman_sync", "node_id", data)
end
function s:write()
uci:save('gluon')
end
end

View File

@ -0,0 +1,13 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-manman-sync
PKG_VERSION:=1
include ../gluon.mk
define Package/gluon-manman-sync
DEPENDS:=+gluon-core +micrond
TITLE:=Sync configuration with data from manman
endef
$(eval $(call BuildPackageGluon,gluon-manman-sync))

View File

@ -0,0 +1,21 @@
#!/usr/bin/lua
-- Setup a cron for manman-sync if enabled
local uci = require('simple-uci').cursor()
local urandom = io.open('/dev/urandom', 'r')
local seed1, seed2 = urandom:read(2):byte(1, 2)
math.randomseed(seed1*0x100 + seed2)
urandom:close()
-- Perform sync at a random time each hour
local minute = math.random(0, 59)
local f = io.open('/usr/lib/micron.d/manman-sync', 'w')
-- Only setup cron if enabled
-- Write file regardless to clear old cron
if uci:get_bool('gluon', 'manman_sync', 'enabled') then
f:write(string.format('%i * * * * /usr/bin/manman-sync\n', minute))
end
f:close()

View File

@ -0,0 +1,20 @@
#!/usr/bin/lua
local uci = require('simple-uci').cursor()
local manman = 'https://manman.graz.funkfeuer.at'
if uci:get_bool('gluon', 'manman_sync', 'enabled') then
if not uci:get('gluon', 'manman_sync', 'node_id') then
print('E: manman node_id missing')
return 2
end
-- check manman location, abort if not reachable
-- try to fetch data
-- check if anything changed since last time
-- if yes, apply changes and do gluon-reload
else
print('manman-sync not enabled, skipping')
end

View File

@ -0,0 +1,6 @@
all: respondd.so
CFLAGS += -Wall
respondd.so: respondd.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil -luci

View File

@ -0,0 +1,78 @@
/*
Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <respondd.h>
#include <json-c/json.h>
#include <libgluonutil.h>
#include <uci.h>
#include <string.h>
static struct json_object * get_autoupdater(void) {
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
ctx->flags &= ~UCI_FLAG_STRICT;
struct uci_package *p;
if (uci_load(ctx, "gluon", &p))
goto error;
struct uci_section *s = uci_lookup_section(ctx, p, "manman_sync");
if (!s)
goto error;
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "nodeid", gluonutil_wrap_string(uci_lookup_option_string(ctx, s, "nodeid")));
const char *enabled = uci_lookup_option_string(ctx, s, "enabled");
json_object_object_add(ret, "enabled", json_object_new_boolean(enabled && !strcmp(enabled, "1")));
uci_free_context(ctx);
return ret;
error:
uci_free_context(ctx);
return NULL;
}
static struct json_object * respondd_provider_nodeinfo(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "manman", get_autoupdater());
return ret;
}
const struct respondd_provider_info respondd_providers[] = {
{"nodeinfo", respondd_provider_nodeinfo},
{}
};