Skip to content

Commit

Permalink
Reconfigure on module-added event
Browse files Browse the repository at this point in the history
- Put config file generation in "expand-config" helper command. Handle
  the special case when the configuration does not exist with an
  hardcoded default.

- Invoke the helper in create-module and at every service startup.

- Restart (and reconfigure) DNSMasq if a Samba instance appears on the
  local node.
  • Loading branch information
DavidePrincipi committed Dec 11, 2024
1 parent c6ec5e8 commit 4989a99
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
22 changes: 2 additions & 20 deletions imageroot/actions/configure-module/20configure
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,11 @@

import json
import sys
import agent

request = json.load(sys.stdin)

# write dnsmasq configuration to easy json file, will be used by UI
open("config.json", "w").write(json.dumps(request))

# convert json to configuration file
with open("dnsmasq.d/00config.conf", "w") as file:
file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n")
# write interface only if dhcp-server or dns-server are enabled
if request["dhcp-server"]["enabled"] or request["dns-server"]["enabled"]:
file.write("interface=" + request["interface"] + "\n")

# write dhcp-server configuration
if request["dhcp-server"]["enabled"]:
file.write("dhcp-range=set:default," + request["dhcp-server"]["start"] + "," + request["dhcp-server"]["end"] + "," + str(request["dhcp-server"]["lease"]) + "h\n")

# write dns-server configuration
if request["dns-server"]["enabled"]:
file.write("server=" + request["dns-server"]["primary-server"] + "\n")
if request["dns-server"]["secondary-server"] != "":
file.write("server=" + request["dns-server"]["secondary-server"] + "\n")

else:
# shut down dns server if not enabled
file.write("port=0\n")
agent.run_helper('expand-config').check_returncode()
19 changes: 3 additions & 16 deletions imageroot/actions/create-module/20default_config
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,9 @@
#

import json
import agent

config = {
"interface": "",
"dhcp-server": {
"enabled": False,
"start": "",
"end": "",
"lease": 12
},
"dns-server": {
"enabled": False,
"primary-server": "",
"secondary-server": ""
},
}

open("config.json", "w").write(json.dumps(config))
# Force the initialization of config.json for get-configuration:
agent.run_helper('expand-config').check_returncode()

open("dns-records.json", "w").write(json.dumps({"records": []}))
52 changes: 52 additions & 0 deletions imageroot/bin/expand-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import sys
import agent

# Read configuration from a JSON file or initialize it.
try:
config = json.load(open("config.json"))
except FileNotFoundError:
print(agent.SD_NOTICE, "Generating a new config.json file...", file=sys.stderr)
config = {
"interface": "",
"dhcp-server": {
"enabled": False,
"start": "",
"end": "",
"lease": 12
},
"dns-server": {
"enabled": False,
"primary-server": "",
"secondary-server": ""
},
}
json.dump(config, fp=open("config.json", "w"))


# convert json to configuration file
with open("dnsmasq.d/00config.conf", "w") as file:
file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n")
# write interface only if dhcp-server or dns-server are enabled
if config["dhcp-server"]["enabled"] or config["dns-server"]["enabled"]:
file.write("interface=" + config["interface"] + "\n")

# write dhcp-server configuration
if config["dhcp-server"]["enabled"]:
file.write("dhcp-range=set:default," + config["dhcp-server"]["start"] + "," + config["dhcp-server"]["end"] + "," + str(config["dhcp-server"]["lease"]) + "h\n")

# write dns-server configuration
if config["dns-server"]["enabled"]:
file.write("server=" + config["dns-server"]["primary-server"] + "\n")
if config["dns-server"]["secondary-server"] != "":
file.write("server=" + config["dns-server"]["secondary-server"] + "\n")
else:
# shut down dns server if not enabled
file.write("port=0\n")
1 change: 1 addition & 0 deletions imageroot/dnsmasq.service
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TimeoutStopSec=70
ExecStartPre=/bin/rm \
-f %t/%n.ctr-id
ExecStartPre=runagent -m %N reload_hosts
ExecStartPre=runagent -m %N expand-config
ExecStart=/usr/bin/podman run \
--cidfile=%t/%n.ctr-id \
--cgroups=no-conmon \
Expand Down
20 changes: 20 additions & 0 deletions imageroot/events/module-added/10restart_dnsmasq
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import os
import sys

import agent

data = json.load(sys.stdin)

# skip if the event comes from another node
if os.environ['NODE_ID'] != str(data['node']):
sys.exit(0)

agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID'))

0 comments on commit 4989a99

Please sign in to comment.