From c5525d746ef833ece25b90bceff2d2c78a149348 Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Wed, 4 Jan 2023 21:08:14 -0500 Subject: [PATCH] Avoid need to preprocess redhat templates This was quite ugly to see. Turns out we could just pass in the extra stuff via context and just use them in the template logic and it all works out. No need to escape `{` and `}` any more. --- packetnetworking/distros/distro_builder.py | 14 +++--- packetnetworking/distros/redhat/bonded.py | 8 ++-- ...c_sysconfig_network-scripts_ifcfg-bondX.j2 | 46 +++++++++---------- ...sysconfig_network-scripts_ifcfg-bondX_0.j2 | 16 +++---- ...ysconfig_network-scripts_ifcfg-template.j2 | 6 +-- ...c_sysconfig_network-scripts_route-bondX.j2 | 6 +-- packetnetworking/utils.py | 8 ++-- 7 files changed, 52 insertions(+), 52 deletions(-) diff --git a/packetnetworking/distros/distro_builder.py b/packetnetworking/distros/distro_builder.py index d287318..c684338 100644 --- a/packetnetworking/distros/distro_builder.py +++ b/packetnetworking/distros/distro_builder.py @@ -129,17 +129,17 @@ def render(self): file_mode = None mode = None + context = self.context() if isinstance(template, dict): + context.update(template.get("context") or {}) file_mode = template.get("file_mode") mode = template.get("mode", None) - fmt = template.get("fmt") template_path = template.get("template_path") - template = template.get("template") - if template_path is not None: + if template_path is None: + template = template.get("template") + else: with open(template_path, "r") as f: template = f.read() - if fmt: - template = template.format(**fmt) template = dedent(template) tmpl = Template( @@ -157,10 +157,10 @@ def render(self): rendered_tasks[path] = { "file_mode": file_mode, "mode": mode, - "content": tmpl.render(self.context()), + "content": tmpl.render(context), } else: - rendered_tasks[path] = tmpl.render(self.context()) + rendered_tasks[path] = tmpl.render(context) except UndefinedError: # having to use print as log.* isn't printing out the json print( diff --git a/packetnetworking/distros/redhat/bonded.py b/packetnetworking/distros/redhat/bonded.py index e832076..ed26888 100644 --- a/packetnetworking/distros/redhat/bonded.py +++ b/packetnetworking/distros/redhat/bonded.py @@ -21,7 +21,7 @@ def build_tasks(self): self.task_template( "etc/sysconfig/network-scripts/ifcfg-{}".format(bond), "bonded/etc_sysconfig_network-scripts_ifcfg-bondX.j2", - fmt={"bond": bond}, + context={"bond": bond}, ) if self.ipv4pub and bond == "bond0": @@ -30,12 +30,12 @@ def build_tasks(self): self.task_template( "etc/sysconfig/network-scripts/ifcfg-{}:0".format(bond), "bonded/etc_sysconfig_network-scripts_ifcfg-bondX_0.j2", - fmt={"bond": bond}, + context={"bond": bond}, ) self.task_template( "etc/sysconfig/network-scripts/route-{}".format(bond), "bonded/etc_sysconfig_network-scripts_route-bondX.j2", - fmt={"bond": bond}, + context={"bond": bond}, ) for i, iface in enumerate(self.network.interfaces): @@ -43,7 +43,7 @@ def build_tasks(self): self.task_template( "etc/sysconfig/network-scripts/ifcfg-" + name, "bonded/etc_sysconfig_network-scripts_ifcfg-template.j2", - fmt={"iface": name, "i": i}, + context={"iface": name, "i": i}, ) self.task_template( diff --git a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX.j2 b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX.j2 index 4f66847..127e2bd 100644 --- a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX.j2 +++ b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX.j2 @@ -1,29 +1,29 @@ -DEVICE={bond} -NAME={bond} -{{% if "{bond}" == "bond0" %}} -{{% if ip4pub %}} -IPADDR={{{{ ip4pub.address }}}} -NETMASK={{{{ ip4pub.netmask }}}} -GATEWAY={{{{ ip4pub.gateway }}}} -{{% else %}} -IPADDR={{{{ ip4priv.address }}}} -NETMASK={{{{ ip4priv.netmask }}}} -GATEWAY={{{{ ip4priv.gateway }}}} -{{% endif %}} -{{% endif %}} +DEVICE={{bond}} +NAME={{bond}} +{% if bond == "bond0" %} +{% if ip4pub %} +IPADDR={{ ip4pub.address }} +NETMASK={{ ip4pub.netmask }} +GATEWAY={{ ip4pub.gateway }} +{% else %} +IPADDR={{ ip4priv.address }} +NETMASK={{ ip4priv.netmask }} +GATEWAY={{ ip4priv.gateway }} +{% endif %} +{% endif %} BOOTPROTO=none ONBOOT=yes USERCTL=no TYPE=Bond -BONDING_OPTS="mode={{{{ net.bonding.mode }}}} miimon=100 downdelay=200 updelay=200" +BONDING_OPTS="mode={{ net.bonding.mode }} miimon=100 downdelay=200 updelay=200" -{{% if "{bond}" == "bond0" %}} -{{% if ip6pub %}} +{% if bond == "bond0" %} +{% if ip6pub %} IPV6INIT=yes -IPV6ADDR={{{{ ip6pub.address }}}}/{{{{ ip6pub.cidr }}}} -IPV6_DEFAULTGW={{{{ ip6pub.gateway }}}} -{{% endif %}} -{{% endif %}} -{{% for dns in resolvers %}} -DNS{{{{ loop.index }}}}={{{{ dns }}}} -{{% endfor %}} +IPV6ADDR={{ ip6pub.address }}/{{ ip6pub.cidr }} +IPV6_DEFAULTGW={{ ip6pub.gateway }} +{% endif %} +{% endif %} +{% for dns in resolvers %} +DNS{{ loop.index }}={{ dns }} +{% endfor %} diff --git a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX_0.j2 b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX_0.j2 index 2c36ec8..6a74bd6 100644 --- a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX_0.j2 +++ b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-bondX_0.j2 @@ -1,11 +1,11 @@ -DEVICE={bond}:0 -NAME={bond}:0 -IPADDR={{{{ ip4priv.address }}}} -NETMASK={{{{ ip4priv.netmask }}}} -GATEWAY={{{{ ip4priv.gateway }}}} +DEVICE={{bond}}:0 +NAME={{bond}}:0 +IPADDR={{ ip4priv.address }} +NETMASK={{ ip4priv.netmask }} +GATEWAY={{ ip4priv.gateway }} BOOTPROTO=none ONBOOT=yes USERCTL=no -{{% for dns in resolvers %}} -DNS{{{{ loop.index }}}}={{{{ dns }}}} -{{% endfor %}} +{% for dns in resolvers %} +DNS{{ loop.index }}={{ dns }} +{% endfor %} diff --git a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-template.j2 b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-template.j2 index 9b8a489..6d3223a 100644 --- a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-template.j2 +++ b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_ifcfg-template.j2 @@ -1,6 +1,6 @@ -DEVICE={iface} +DEVICE={{iface}} ONBOOT=yes -HWADDR={{{{ interfaces[{i}].mac }}}} -MASTER={{{{ interfaces[{i}].bond }}}} +HWADDR={{ interfaces[i].mac }} +MASTER={{ interfaces[i].bond }} SLAVE=yes BOOTPROTO=none diff --git a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_route-bondX.j2 b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_route-bondX.j2 index 0ee177c..5dea577 100644 --- a/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_route-bondX.j2 +++ b/packetnetworking/distros/redhat/templates/bonded/etc_sysconfig_network-scripts_route-bondX.j2 @@ -1,3 +1,3 @@ -{{% for subnet in private_subnets %}} -{{{{ subnet }}}} via {{{{ ip4priv.gateway }}}} dev {bond}:0 -{{% endfor %}} +{% for subnet in private_subnets %} +{{ subnet }} via {{ ip4priv.gateway }} dev {{bond}}:0 +{% endfor %} diff --git a/packetnetworking/utils.py b/packetnetworking/utils.py index ae9db14..e0e0fc0 100644 --- a/packetnetworking/utils.py +++ b/packetnetworking/utils.py @@ -139,18 +139,18 @@ def where(self, *args, **kwargs): class Tasks(object): - def task(self, task, content, write_mode=None, mode=None, fmt=None): + def task(self, task, content, write_mode=None, mode=None, context=None): self.tasks[task] = { "file_mode": write_mode, "mode": mode, "template": content, - "fmt": fmt, + "context": context, } return self.tasks[task] - def task_template(self, task, path, write_mode=None, mode=None, fmt=None): + def task_template(self, task, path, write_mode=None, mode=None, context=None): path = os.path.join(package_dir, self.templates_base, path) - t = self.task(task, None, write_mode, mode, fmt) + t = self.task(task, None, write_mode, mode, context) t["template_path"] = path return t