From b50a4ed7f4a287f33f85cbb64115dd36e8fa3089 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Thu, 27 Jul 2023 11:11:06 +0000 Subject: [PATCH] revert netplan fix. --- sbin/linuxmuster-import-subnets | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/sbin/linuxmuster-import-subnets b/sbin/linuxmuster-import-subnets index f82ffcc..d37c950 100755 --- a/sbin/linuxmuster-import-subnets +++ b/sbin/linuxmuster-import-subnets @@ -2,7 +2,7 @@ # # linuxmuster-import-subnets # thomas@linuxmuster.net -# 20230725 +# 20211219 # import ast @@ -84,6 +84,43 @@ nat_rule_xml = nat_rule_xml.replace( # functions begin +# update static routes in netplan configuration +def updateNetplan(subnets): + printScript('Processing netplan configuration:') + cfgfile = constants.NETCFG + # read netplan config file + with open(cfgfile) as config: + netcfg = yaml.safe_load(config) + iface = str(netcfg['network']['ethernets']).split('\'')[1] + ifcfg = netcfg['network']['ethernets'][iface] + # first delete the old routes if there are any + try: + del ifcfg['routes'] + changed = True + printScript('* Removed old routes.') + except: + changed = False + # only if there are subnets beside server network + if len(subnets) > 0: + changed = True + ifcfg['routes'] = [] + for item in subnets: + subnet = item.split(':')[0] + # tricky: concenate dict object for yaml using eval + subroute = eval('{"to": ' + '\'' + subnet + '\'' + + ', "via": ' + '\'' + servernet_router + '\'' + '}') + ifcfg['routes'].append(subroute) + printScript('* Added new routes for all subnets.') + # save netcfg + if changed: + with open(cfgfile, 'w') as config: + config.write(yaml.dump(netcfg, default_flow_style=False)) + os.system('netplan apply') + printScript('* Applied new configuration.') + # send changed configuration back and apply it + return changed + + # update vlan gateway on firewall def updateFwGw(servernet_router, content): soup = BeautifulSoup(content, 'lxml') @@ -323,6 +360,9 @@ else: os.system('systemctl restart isc-dhcp-server.service') +# update netplan config with new routes for server (localhost) +changed = updateNetplan(subnets) + # update firewall if not skipfw: changed = updateFw(subnets, firewallip, ipnet_setup,