Skip to content

Commit

Permalink
refactor: added fqdn-changed event
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed Mar 15, 2024
1 parent 0f4aa3a commit c98bba4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
30 changes: 17 additions & 13 deletions imageroot/bin/reload_hosts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
#

import json
import sys
import os
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')
2 changes: 1 addition & 1 deletion imageroot/dnsmasq.service
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
21 changes: 21 additions & 0 deletions imageroot/events/fqdn-changed/10handler
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/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

# 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 agent_source_id.startswith(f'node/{os.environ['NODE_ID']}'):
agent.run_helper("reload_hosts")

agent.run('systemctl', 'restart', os.getenv('MODULE_ID'))
5 changes: 2 additions & 3 deletions imageroot/events/hosts-changed/10handler
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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")

0 comments on commit c98bba4

Please sign in to comment.