From d74537f4bc281597f1be4229316d0eae812f9642 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Mon, 11 Mar 2024 14:28:34 +0100 Subject: [PATCH 01/10] refactor: configuration now is static instead of diff --- container/.gitignore | 1 - container/Containerfile | 5 +-- container/conf.patch | 82 ----------------------------------------- container/dnsmasq.conf | 19 ++++++++++ 4 files changed, 20 insertions(+), 87 deletions(-) delete mode 100644 container/.gitignore delete mode 100644 container/conf.patch create mode 100644 container/dnsmasq.conf diff --git a/container/.gitignore b/container/.gitignore deleted file mode 100644 index ce32b14..0000000 --- a/container/.gitignore +++ /dev/null @@ -1 +0,0 @@ -dnsmasq.conf.example* diff --git a/container/Containerfile b/container/Containerfile index 55d1d8d..bf7c7f5 100644 --- a/container/Containerfile +++ b/container/Containerfile @@ -13,14 +13,11 @@ RUN mkdir -p /tmp/dnsmasq \ WORKDIR /tmp/dnsmasq # Build dnsmasq RUN make -# Apply configuration patch -COPY conf.patch /tmp/dnsmasq/conf.patch -RUN patch < conf.patch FROM base as dist # Copy built dnsmasq and configuration COPY --from=build /tmp/dnsmasq/src/dnsmasq /usr/local/sbin/dnsmasq -COPY --from=build /tmp/dnsmasq/dnsmasq.conf.example /etc/dnsmasq.conf +COPY dnsmasq.conf /etc/dnsmasq.conf # Since we're defining a config directory, we need to create it RUN mkdir -p /etc/dnsmasq.d # Set custom entrypoint and command diff --git a/container/conf.patch b/container/conf.patch deleted file mode 100644 index 55808c2..0000000 --- a/container/conf.patch +++ /dev/null @@ -1,82 +0,0 @@ ---- dnsmasq.conf.example 2024-02-19 17:40:09.563152181 +0100 -+++ dnsmasq.conf.example.patch 2024-03-08 08:45:42.436961452 +0100 -@@ -4,6 +4,13 @@ - # as the long options legal on the command line. See - # "/usr/sbin/dnsmasq --help" or "man 8 dnsmasq" for details. - -+# To allow running in container, do not run as a daemon -+keep-in-foreground -+# Log to stdout -+log-facility=/dev/stdout -+# allow asking for domain to all available servers -+all-servers -+ - # Listen on this specific port instead of the standard DNS port - # (53). Setting this to zero completely disables DNS function, - # leaving only DHCP and/or TFTP. -@@ -16,9 +23,9 @@ - # these requests from bringing up the link unnecessarily. - - # Never forward plain names (without a dot or domain part) --#domain-needed -+domain-needed - # Never forward addresses in the non-routed address spaces. --#bogus-priv -+bogus-priv - - # Uncomment these to enable DNSSEC validation and caching: - # (Requires dnsmasq to be built with DNSSEC option.) -@@ -55,7 +62,7 @@ - # If you don't want dnsmasq to read /etc/resolv.conf or any other - # file, getting its servers from this file instead (see below), then - # uncomment this. --#no-resolv -+no-resolv - - # If you don't want dnsmasq to poll /etc/resolv.conf or other resolv - # files for changes and re-read them then uncomment this. -@@ -135,7 +142,7 @@ - - # If you don't want dnsmasq to read /etc/hosts, uncomment the - # following line. --#no-hosts -+no-hosts - # or if you want it to read another file, as well as /etc/hosts, use - # this. - #addn-hosts=/etc/banner_add_hosts -@@ -442,7 +449,7 @@ - #dhcp-option-force=209,configs/common - # Path prefix - #dhcp-option-force=210,/tftpboot/pxelinux/files/ --# Reboot time. (Note 'i' to send 32-bit value) -+# Reboot time. (Note 'i'una customizzazione dentro to send 32-bit value) - #dhcp-option-force=211,30i - - # Set the boot filename for netboot/PXE. You will only need -@@ -555,7 +562,7 @@ - # server for your campus/company accidentally. The ISC server uses - # the same option, and this URL provides more information: - # http://www.isc.org/files/auth.html --#dhcp-authoritative -+dhcp-authoritative - - # Set the DHCP server to enable DHCPv4 Rapid Commit Option per RFC 4039. - # In this mode it will respond to a DHCPDISCOVER message including a Rapid Commit -@@ -572,7 +579,7 @@ - #dhcp-script=/bin/echo - - # Set the cachesize here. --#cache-size=150 -+cache-size=4000 - - # If you want to disable negative caching, uncomment this. - #no-negcache -@@ -681,7 +688,7 @@ - #conf-dir=/etc/dnsmasq.d,.bak - - # Include all files in a directory which end in .conf --#conf-dir=/etc/dnsmasq.d/,*.conf -+conf-dir=/etc/dnsmasq.d/,*.conf - - # If a DHCP client claims that its name is "wpad", ignore that. - # This fixes a security hole. see CERT Vulnerability VU#598349 diff --git a/container/dnsmasq.conf b/container/dnsmasq.conf new file mode 100644 index 0000000..528896c --- /dev/null +++ b/container/dnsmasq.conf @@ -0,0 +1,19 @@ +# REF: https://dnsmasq.org/docs/dnsmasq-man.html + +# container related configuration +keep-in-foreground +log-facility=/dev/stdout +cache-size=4000 + +# DNS +all-servers +domain-needed +bogus-priv +no-resolv +no-hosts + +# DHCP +dhcp-authoritative + +# Conf Directories +conf-dir=/etc/dnsmasq.d/,*.conf From fe8a445be735a5df7ad4a1fe1a784a2172e64036 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Mon, 11 Mar 2024 14:29:35 +0100 Subject: [PATCH 02/10] feat: added additional hosts directory --- container/Containerfile | 6 +++--- container/dnsmasq.conf | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/container/Containerfile b/container/Containerfile index bf7c7f5..ee284b2 100644 --- a/container/Containerfile +++ b/container/Containerfile @@ -18,12 +18,12 @@ FROM base as dist # Copy built dnsmasq and configuration COPY --from=build /tmp/dnsmasq/src/dnsmasq /usr/local/sbin/dnsmasq COPY dnsmasq.conf /etc/dnsmasq.conf -# Since we're defining a config directory, we need to create it -RUN mkdir -p /etc/dnsmasq.d +# Creating directory for custom configuration and additional hosts +RUN mkdir -p /etc/dnsmasq.d /etc/dnsmasq_hosts.d # Set custom entrypoint and command COPY entrypoint.sh /entrypoint EXPOSE 53/udp EXPOSE 67/udp -VOLUME [ "/etc/dnsmasq.d" ] +VOLUME [ "/etc/dnsmasq.d", "/etc/dnsmasq_hosts.d" ] ENTRYPOINT [ "/entrypoint" ] CMD [ "dnsmasq" ] diff --git a/container/dnsmasq.conf b/container/dnsmasq.conf index 528896c..751a5ce 100644 --- a/container/dnsmasq.conf +++ b/container/dnsmasq.conf @@ -16,4 +16,5 @@ no-hosts dhcp-authoritative # Conf Directories +hostsdir=/etc/dnsmasq_hosts.d conf-dir=/etc/dnsmasq.d/,*.conf From 19412cd961a66094b47a5b36f1d11ef06fffb39c Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Mon, 11 Mar 2024 15:24:15 +0100 Subject: [PATCH 03/10] feat: add traefik hosts to resolve using dns --- imageroot/actions/create-module/10setup | 4 ++-- imageroot/dnsmasq.service | 1 + imageroot/events/hosts-changed/10handler | 30 ++++++++++++++++++++++++ imageroot/update-module.d/10hosts_dir | 9 +++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100755 imageroot/events/hosts-changed/10handler create mode 100644 imageroot/update-module.d/10hosts_dir diff --git a/imageroot/actions/create-module/10setup b/imageroot/actions/create-module/10setup index 6957756..4ad9db2 100755 --- a/imageroot/actions/create-module/10setup +++ b/imageroot/actions/create-module/10setup @@ -5,8 +5,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later # -# Create the dnsmasq.d directory -mkdir -p dnsmasq.d +# Create the dnsmasq.d and hosts_dir directory +mkdir -p dnsmasq.d dnsmasq_hosts.d # Install systemd service install -m 644 ../dnsmasq.service "/etc/systemd/system/${MODULE_ID}.service" systemctl daemon-reload diff --git a/imageroot/dnsmasq.service b/imageroot/dnsmasq.service index 089fbf7..9498449 100644 --- a/imageroot/dnsmasq.service +++ b/imageroot/dnsmasq.service @@ -23,6 +23,7 @@ ExecStart=/usr/bin/podman run \ --network=host \ --cap-add=NET_ADMIN,NET_RAW \ --volume=/var/lib/nethserver/%N/state/dnsmasq.d:/etc/dnsmasq.d:Z \ + --volume=/var/lib/nethserver/%N/state/dnsmasq_hosts.d:/etc/dnsmasq_hosts.d:Z \ ${DNSMASQ_SERVER_IMAGE} ExecStop=/usr/bin/podman stop \ --ignore -t 10 \ diff --git a/imageroot/events/hosts-changed/10handler b/imageroot/events/hosts-changed/10handler new file mode 100755 index 0000000..210a947 --- /dev/null +++ b/imageroot/events/hosts-changed/10handler @@ -0,0 +1,30 @@ +#!/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 +import network + +# check if interface is present inside the configuration +config = json.load(open("config.json")) +if 'interface' in config and config['interface'] != '': + # interface is set, assuming it's valid, parse data and init variables + data = json.load(sys.stdin) + agent_source_id = os.getenv("AGENT_EVENT_SOURCE") + interfaces = network.list_interfaces() + interface = next(interface for interface in interfaces if interface["name"] == config["interface"]) + ip = interface["addresses"][0]["address"] + + # if event is triggered by traefik and the node_id is the same as the current node, reload hosts + if 'traefik' in agent_source_id and os.environ['NODE_ID'] == str(data['node_id']): + redis_client = agent.redis_connect() + with open(f'dnsmasq_hosts.d/traefik_hosts', 'w') as file: + for host in redis_client.smembers(f'{agent_source_id}/hosts'): + file.write(f'{ip} {host}\n') diff --git a/imageroot/update-module.d/10hosts_dir b/imageroot/update-module.d/10hosts_dir new file mode 100644 index 0000000..063d6b1 --- /dev/null +++ b/imageroot/update-module.d/10hosts_dir @@ -0,0 +1,9 @@ +#!/usr/bin/env sh + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +# introduced dnsmasq_hosts.d in 0.4.0 +mkdir -p dnsmasq_hosts.d From 2bb219be774b1d494f7a19d8fd2ec09b86304eca Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Mon, 11 Mar 2024 16:36:37 +0100 Subject: [PATCH 04/10] feat: added reload of hosts during ExecStartPre --- imageroot/bin/reload_hosts | 35 ++++++++++++++++++++++++ imageroot/dnsmasq.service | 1 + imageroot/events/hosts-changed/10handler | 23 ++++------------ imageroot/update-module.d/10hosts_dir | 0 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100755 imageroot/bin/reload_hosts mode change 100644 => 100755 imageroot/update-module.d/10hosts_dir diff --git a/imageroot/bin/reload_hosts b/imageroot/bin/reload_hosts new file mode 100755 index 0000000..d361df2 --- /dev/null +++ b/imageroot/bin/reload_hosts @@ -0,0 +1,35 @@ +#!/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 +import network + +""" +Generate a host file from traefik that will be used to resolve reverse proxies. +""" +traefik_agent = sys.argv[1] if len(sys.argv) > 1 else None +# if traefik_agent is not set, try to resolve it +if traefik_agent is None: + traefik_agent = agent.resolve_agent_id("traefik@node") + if traefik_agent is None: + raise RuntimeError("traefik agent not found") + +# read dnsmasq configuration +config = json.load(open("config.json")) +if 'interface' in config and config['interface'] != '': + # retrieve the first network interface that matches the configuration, then get the first IP address provided + interface = next(interface for interface in network.list_interfaces() if interface["name"] == config["interface"]) + ip = interface["addresses"][0]["address"] + # prepare redis to fetch the hosts + redis_client = agent.redis_connect() + with open(f'dnsmasq_hosts.d/traefik_hosts', 'w') as file: + file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n") + for host in redis_client.smembers(f'{traefik_agent}/hosts'): + file.write(f'{ip} {host}\n') diff --git a/imageroot/dnsmasq.service b/imageroot/dnsmasq.service index 9498449..6bbd7c3 100644 --- a/imageroot/dnsmasq.service +++ b/imageroot/dnsmasq.service @@ -12,6 +12,7 @@ Restart=on-failure TimeoutStopSec=70 ExecStartPre=/bin/rm \ -f %t/%n.ctr-id +ExecStartPre=runagent -m %N ../bin/reload_hosts ExecStart=/usr/bin/podman run \ --cidfile=%t/%n.ctr-id \ --cgroups=no-conmon \ diff --git a/imageroot/events/hosts-changed/10handler b/imageroot/events/hosts-changed/10handler index 210a947..e15fbd7 100755 --- a/imageroot/events/hosts-changed/10handler +++ b/imageroot/events/hosts-changed/10handler @@ -10,21 +10,10 @@ import os import sys import agent -import network -# check if interface is present inside the configuration -config = json.load(open("config.json")) -if 'interface' in config and config['interface'] != '': - # interface is set, assuming it's valid, parse data and init variables - data = json.load(sys.stdin) - agent_source_id = os.getenv("AGENT_EVENT_SOURCE") - interfaces = network.list_interfaces() - interface = next(interface for interface in interfaces if interface["name"] == config["interface"]) - ip = interface["addresses"][0]["address"] - - # if event is triggered by traefik and the node_id is the same as the current node, reload hosts - if 'traefik' in agent_source_id and os.environ['NODE_ID'] == str(data['node_id']): - redis_client = agent.redis_connect() - with open(f'dnsmasq_hosts.d/traefik_hosts', 'w') as file: - for host in redis_client.smembers(f'{agent_source_id}/hosts'): - file.write(f'{ip} {host}\n') +# parse data and init variables +data = json.load(sys.stdin) +agent_source_id = os.getenv("AGENT_EVENT_SOURCE") +# execute only if traefik is the source of the event and the node_id is the same as the current node +if 'traefik' in agent_source_id and os.environ['NODE_ID'] == str(data['node_id']): + agent.run_helper("../bin/reload_hosts", agent_source_id) diff --git a/imageroot/update-module.d/10hosts_dir b/imageroot/update-module.d/10hosts_dir old mode 100644 new mode 100755 From 0f4aa3a61ef57f5c91e35e9d0f36e3f8ae10a3ef Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Wed, 13 Mar 2024 17:53:45 +0100 Subject: [PATCH 05/10] fix: fixup from review --- imageroot/bin/reload_hosts | 2 +- imageroot/events/hosts-changed/10handler | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/imageroot/bin/reload_hosts b/imageroot/bin/reload_hosts index d361df2..ba4403a 100755 --- a/imageroot/bin/reload_hosts +++ b/imageroot/bin/reload_hosts @@ -28,7 +28,7 @@ if 'interface' in config and config['interface'] != '': interface = next(interface for interface in network.list_interfaces() if interface["name"] == config["interface"]) ip = interface["addresses"][0]["address"] # prepare redis to fetch the hosts - redis_client = agent.redis_connect() + redis_client = agent.redis_connect(use_replica=True) with open(f'dnsmasq_hosts.d/traefik_hosts', 'w') as file: file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n") for host in redis_client.smembers(f'{traefik_agent}/hosts'): diff --git a/imageroot/events/hosts-changed/10handler b/imageroot/events/hosts-changed/10handler index e15fbd7..b8d6c92 100755 --- a/imageroot/events/hosts-changed/10handler +++ b/imageroot/events/hosts-changed/10handler @@ -16,4 +16,4 @@ data = json.load(sys.stdin) agent_source_id = os.getenv("AGENT_EVENT_SOURCE") # execute only if traefik is the source of the event and the node_id is the same as the current node if 'traefik' in agent_source_id and os.environ['NODE_ID'] == str(data['node_id']): - agent.run_helper("../bin/reload_hosts", agent_source_id) + agent.run_helper("reload_hosts", agent_source_id) From ea40d6106d1fa9cd32fc505f942372ca2c75defb Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Fri, 15 Mar 2024 10:00:56 +0100 Subject: [PATCH 06/10] refactor: added fqdn-changed event --- imageroot/bin/reload_hosts | 29 +++++++++++++----------- imageroot/dnsmasq.service | 2 +- imageroot/events/fqdn-changed/10handler | 16 +++++++++++++ imageroot/events/hosts-changed/10handler | 7 +++--- 4 files changed, 37 insertions(+), 17 deletions(-) create mode 100755 imageroot/events/fqdn-changed/10handler diff --git a/imageroot/bin/reload_hosts b/imageroot/bin/reload_hosts index ba4403a..2a5fe95 100755 --- a/imageroot/bin/reload_hosts +++ b/imageroot/bin/reload_hosts @@ -6,30 +6,33 @@ # import json -import sys +import subprocess import agent import network """ -Generate a host file from traefik that will be used to resolve reverse proxies. +Generate a host file from traefik that will be used to resolve CNAMEs +Then save all CNAMES in a config file for DNSMasq """ -traefik_agent = sys.argv[1] if len(sys.argv) > 1 else None -# if traefik_agent is not set, try to resolve it -if traefik_agent is None: - traefik_agent = agent.resolve_agent_id("traefik@node") - if traefik_agent is None: - raise RuntimeError("traefik agent not found") -# read dnsmasq configuration +fqdn = subprocess.run(['hostname', '-f'], capture_output=True, text=True, check=True).stdout.strip() + config = json.load(open("config.json")) if 'interface' in config and config['interface'] != '': # retrieve the first network interface that matches the configuration, then get the first IP address provided interface = next(interface for interface in network.list_interfaces() if interface["name"] == config["interface"]) ip = interface["addresses"][0]["address"] - # prepare redis to fetch the hosts + # save a host file for dnsmasq, so that it can resolve the node fqdn + with open(f'dnsmasq_hosts.d/00_node_hosts', 'w') as file: + file.write('# This file is automatically generated by NethServer, manual changes will be lost.\n') + file.write(f'{ip} {fqdn}\n') + + # save a new configuration file for dnsmasq, so that it can resolve the CNAMEs to the node fqdn redis_client = agent.redis_connect(use_replica=True) - with open(f'dnsmasq_hosts.d/traefik_hosts', 'w') as file: + with open("dnsmasq.d/01cnames.conf", "w") as file: file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n") - for host in redis_client.smembers(f'{traefik_agent}/hosts'): - file.write(f'{ip} {host}\n') + file.write('cname=') + for host in redis_client.smembers(f'{agent.resolve_agent_id("traefik@node")}/hosts'): + file.write(f'{host},') + file.write(f'{fqdn}\n') diff --git a/imageroot/dnsmasq.service b/imageroot/dnsmasq.service index 6bbd7c3..a75e8aa 100644 --- a/imageroot/dnsmasq.service +++ b/imageroot/dnsmasq.service @@ -12,7 +12,7 @@ Restart=on-failure TimeoutStopSec=70 ExecStartPre=/bin/rm \ -f %t/%n.ctr-id -ExecStartPre=runagent -m %N ../bin/reload_hosts +ExecStartPre=runagent -m %N reload_hosts ExecStart=/usr/bin/podman run \ --cidfile=%t/%n.ctr-id \ --cgroups=no-conmon \ diff --git a/imageroot/events/fqdn-changed/10handler b/imageroot/events/fqdn-changed/10handler new file mode 100755 index 0000000..87c94e7 --- /dev/null +++ b/imageroot/events/fqdn-changed/10handler @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +import os + +import agent + +# execute only if traefik is the source of the event and the node_id is the same as the current node +if os.getenv("AGENT_EVENT_SOURCE").startswith(f'node/{os.environ['NODE_ID']}'): + agent.run_helper("reload_hosts") + +agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID')) diff --git a/imageroot/events/hosts-changed/10handler b/imageroot/events/hosts-changed/10handler index b8d6c92..cf3f7a3 100755 --- a/imageroot/events/hosts-changed/10handler +++ b/imageroot/events/hosts-changed/10handler @@ -13,7 +13,8 @@ import agent # parse data and init variables data = json.load(sys.stdin) -agent_source_id = os.getenv("AGENT_EVENT_SOURCE") # execute only if traefik is the source of the event and the node_id is the same as the current node -if 'traefik' in agent_source_id and os.environ['NODE_ID'] == str(data['node_id']): - agent.run_helper("reload_hosts", agent_source_id) +if os.environ['NODE_ID'] == str(data['node_id']): + agent.run_helper("reload_hosts") + +agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID')) From b8f40af74587dbb0b0fb3818c80f1977816049ad Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Fri, 15 Mar 2024 11:39:42 +0100 Subject: [PATCH 07/10] fix: importing changes coming from core Using same script to run both events --- imageroot/events/fqdn-changed/10handler | 17 +---------------- imageroot/events/hosts-changed/10handler | 6 +++--- 2 files changed, 4 insertions(+), 19 deletions(-) mode change 100755 => 120000 imageroot/events/fqdn-changed/10handler diff --git a/imageroot/events/fqdn-changed/10handler b/imageroot/events/fqdn-changed/10handler deleted file mode 100755 index 87c94e7..0000000 --- a/imageroot/events/fqdn-changed/10handler +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 - -# -# Copyright (C) 2024 Nethesis S.r.l. -# SPDX-License-Identifier: GPL-3.0-or-later -# - -import os - -import agent - -# execute only if traefik is the source of the event and the node_id is the same as the current node -if os.getenv("AGENT_EVENT_SOURCE").startswith(f'node/{os.environ['NODE_ID']}'): - agent.run_helper("reload_hosts") - -agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID')) diff --git a/imageroot/events/fqdn-changed/10handler b/imageroot/events/fqdn-changed/10handler new file mode 120000 index 0000000..d88bf09 --- /dev/null +++ b/imageroot/events/fqdn-changed/10handler @@ -0,0 +1 @@ +../hosts-changed/10handler \ No newline at end of file diff --git a/imageroot/events/hosts-changed/10handler b/imageroot/events/hosts-changed/10handler index cf3f7a3..95e0cd9 100755 --- a/imageroot/events/hosts-changed/10handler +++ b/imageroot/events/hosts-changed/10handler @@ -13,8 +13,8 @@ import agent # parse data and init variables data = json.load(sys.stdin) -# execute only if traefik is the source of the event and the node_id is the same as the current node -if os.environ['NODE_ID'] == str(data['node_id']): - agent.run_helper("reload_hosts") +# skip if the event comes from another node +if os.environ['NODE_ID'] != str(data['node_id']): + sys.exit(0) agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID')) From 4d17d4bc2398d0d540898ee4fd37bd09867b0eb1 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Fri, 15 Mar 2024 14:33:56 +0100 Subject: [PATCH 08/10] refactor: unified setup script with update --- imageroot/actions/create-module/10setup | 13 +------------ imageroot/update-module.d/10hosts_dir | 9 --------- imageroot/update-module.d/{90systemd => 10setup} | 6 +++++- 3 files changed, 6 insertions(+), 22 deletions(-) mode change 100755 => 120000 imageroot/actions/create-module/10setup delete mode 100755 imageroot/update-module.d/10hosts_dir rename imageroot/update-module.d/{90systemd => 10setup} (64%) diff --git a/imageroot/actions/create-module/10setup b/imageroot/actions/create-module/10setup deleted file mode 100755 index 4ad9db2..0000000 --- a/imageroot/actions/create-module/10setup +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env sh - -# -# Copyright (C) 2024 Nethesis S.r.l. -# SPDX-License-Identifier: GPL-3.0-or-later -# - -# Create the dnsmasq.d and hosts_dir directory -mkdir -p dnsmasq.d dnsmasq_hosts.d -# Install systemd service -install -m 644 ../dnsmasq.service "/etc/systemd/system/${MODULE_ID}.service" -systemctl daemon-reload diff --git a/imageroot/actions/create-module/10setup b/imageroot/actions/create-module/10setup new file mode 120000 index 0000000..dd9540d --- /dev/null +++ b/imageroot/actions/create-module/10setup @@ -0,0 +1 @@ +../../update-module.d/10setup \ No newline at end of file diff --git a/imageroot/update-module.d/10hosts_dir b/imageroot/update-module.d/10hosts_dir deleted file mode 100755 index 063d6b1..0000000 --- a/imageroot/update-module.d/10hosts_dir +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env sh - -# -# Copyright (C) 2024 Nethesis S.r.l. -# SPDX-License-Identifier: GPL-3.0-or-later -# - -# introduced dnsmasq_hosts.d in 0.4.0 -mkdir -p dnsmasq_hosts.d diff --git a/imageroot/update-module.d/90systemd b/imageroot/update-module.d/10setup similarity index 64% rename from imageroot/update-module.d/90systemd rename to imageroot/update-module.d/10setup index 557f638..96fcdcc 100755 --- a/imageroot/update-module.d/90systemd +++ b/imageroot/update-module.d/10setup @@ -5,7 +5,11 @@ # SPDX-License-Identifier: GPL-3.0-or-later # -# update systemd service +# This file will be executed during module creation and module update, be aware of that. + +# Create the dnsmasq.d and hosts_dir directory +mkdir -p dnsmasq.d dnsmasq_hosts.d +# Install systemd service install -m 644 ../dnsmasq.service "/etc/systemd/system/${MODULE_ID}.service" systemctl daemon-reload # check if the service is started, if yes, restart it From 60baea59c83b2f35f37aa48f41a659ec9787aa23 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Fri, 15 Mar 2024 14:47:45 +0100 Subject: [PATCH 09/10] fix: using `try-restart` instead of checking the unit --- imageroot/update-module.d/10setup | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/imageroot/update-module.d/10setup b/imageroot/update-module.d/10setup index 96fcdcc..889cce2 100755 --- a/imageroot/update-module.d/10setup +++ b/imageroot/update-module.d/10setup @@ -12,7 +12,5 @@ mkdir -p dnsmasq.d dnsmasq_hosts.d # Install systemd service install -m 644 ../dnsmasq.service "/etc/systemd/system/${MODULE_ID}.service" systemctl daemon-reload -# check if the service is started, if yes, restart it -if systemctl is-active --quiet "${MODULE_ID}"; then - systemctl restart "${MODULE_ID}" -fi +# restart service only if it's already running +systemctl try-restart "${MODULE_ID}" From bb5d5d358598f335f191d129c94a57f1474415f1 Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Fri, 15 Mar 2024 14:55:47 +0100 Subject: [PATCH 10/10] fix: latest script fixes --- imageroot/update-module.d/10setup | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imageroot/update-module.d/10setup b/imageroot/update-module.d/10setup index 889cce2..e73eafa 100755 --- a/imageroot/update-module.d/10setup +++ b/imageroot/update-module.d/10setup @@ -5,6 +5,9 @@ # SPDX-License-Identifier: GPL-3.0-or-later # +set -e +exec 1>&2 + # This file will be executed during module creation and module update, be aware of that. # Create the dnsmasq.d and hosts_dir directory