From fd79185ea6c0ebc45583d559b815358c0c9edc49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mina=20Gali=C4=87?= Date: Wed, 3 May 2023 13:58:21 +0100 Subject: [PATCH 1/5] ephemeral: ifconfig up and down --- cloudinit/net/netops/bsd_netops.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cloudinit/net/netops/bsd_netops.py b/cloudinit/net/netops/bsd_netops.py index e87ee1e8444..f2d15ad4f4a 100644 --- a/cloudinit/net/netops/bsd_netops.py +++ b/cloudinit/net/netops/bsd_netops.py @@ -1,16 +1,17 @@ from typing import Optional import cloudinit.net.netops as netops +from cloudinit import subp class BsdNetOps(netops.NetOps): @staticmethod def link_up(interface: str): - pass + subp.subp(["ifconfig", interface, "up"]) @staticmethod def link_down(interface: str): - pass + subp.subp(["ifconfig", interface, "down"]) @staticmethod def add_route( From f3d0de2c3b4223f88da875376ce7bc11bb7df05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mina=20Gali=C4=87?= Date: Wed, 3 May 2023 15:50:58 +0100 Subject: [PATCH 2/5] ephemeral: route add, append and delete - add and append are the same - device routes need to be added in two steps --- cloudinit/net/netops/bsd_netops.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cloudinit/net/netops/bsd_netops.py b/cloudinit/net/netops/bsd_netops.py index f2d15ad4f4a..557f43d8610 100644 --- a/cloudinit/net/netops/bsd_netops.py +++ b/cloudinit/net/netops/bsd_netops.py @@ -21,11 +21,15 @@ def add_route( gateway: Optional[str] = None, source_address: Optional[str] = None ): - pass + subp.subp(["route", "add", route, "-interface", interface]) + if gateway and gateway != "0.0.0.0": + subp.subp( + ["route", "change", route, gateway], + ) @staticmethod def append_route(interface: str, address: str, gateway: str): - pass + return BsdNetOps.add_route(interface, address=address, gateway=gateway) @staticmethod def del_route( @@ -35,11 +39,17 @@ def del_route( gateway: Optional[str] = None, source_address: Optional[str] = None ): - pass + subp.subp( + ["route", "-4", "del"] + + (["-net", address] if "/" in address else ["-host", address]) + + ["-interface", interface] + + ([gateway] if gateway and gateway != "0.0.0.0" else []), + ) @staticmethod def get_default_route() -> str: - pass + std, _ = subp.subp(["route", "-4v", "get", "0.0.0.0/0"]) + return std.splitlines()[-1].strip() @staticmethod def add_addr(interface: str, address: str, broadcast: str): From d6710e89f04e91bd64ef711dd0ad1bdb07664c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mina=20Gali=C4=87?= Date: Thu, 4 May 2023 15:15:48 +0100 Subject: [PATCH 3/5] ephemeral: add and remove ip addresses preemptively remove "inet" so we can add IPv6 in a distant future. --- cloudinit/net/netops/bsd_netops.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cloudinit/net/netops/bsd_netops.py b/cloudinit/net/netops/bsd_netops.py index 557f43d8610..d9b6fe6b920 100644 --- a/cloudinit/net/netops/bsd_netops.py +++ b/cloudinit/net/netops/bsd_netops.py @@ -40,21 +40,35 @@ def del_route( source_address: Optional[str] = None ): subp.subp( - ["route", "-4", "del"] - + (["-net", address] if "/" in address else ["-host", address]) - + ["-interface", interface] + ["route", "del", address] + ([gateway] if gateway and gateway != "0.0.0.0" else []), ) @staticmethod def get_default_route() -> str: - std, _ = subp.subp(["route", "-4v", "get", "0.0.0.0/0"]) + std, _ = subp.subp(["route", "-nv", "get", "0.0.0.0/0"]) return std.splitlines()[-1].strip() @staticmethod def add_addr(interface: str, address: str, broadcast: str): - pass + subp.subp( + [ + "ifconfig", + interface, + address, + "broadcast", + broadcast, + "alias", + ], + ) @staticmethod def del_addr(interface: str, address: str): - pass + subp.subp( + [ + "ifconfig", + interface, + address, + "-alias", + ], + ) From 503fe084931d4b35e5279700aa6927f4a3942d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mina=20Gali=C4=87?= Date: Thu, 1 Jun 2023 15:01:56 +0100 Subject: [PATCH 4/5] Fix assignment error in BSD distro baseclass --- cloudinit/distros/bsd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/distros/bsd.py b/cloudinit/distros/bsd.py index 4664762487a..60490c8a794 100644 --- a/cloudinit/distros/bsd.py +++ b/cloudinit/distros/bsd.py @@ -39,7 +39,7 @@ def __init__(self, name, cfg, paths): cfg["ssh_svcname"] = "sshd" cfg["rsyslog_svcname"] = "rsyslogd" self.osfamily = platform.system().lower() - self.net_ops = bsd_netops + self.net_ops = bsd_netops.BsdNetOps def _read_system_hostname(self): sys_hostname = self._read_hostname(self.hostname_conf_fn) From 933a2aa9484c73681ebb77da3b9db70395eedb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mina=20Gali=C4=87?= Date: Thu, 1 Jun 2023 15:03:04 +0100 Subject: [PATCH 5/5] BsdNetOps append_route: correctly name arguments --- cloudinit/net/netops/bsd_netops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/net/netops/bsd_netops.py b/cloudinit/net/netops/bsd_netops.py index d9b6fe6b920..fd9ea8ca573 100644 --- a/cloudinit/net/netops/bsd_netops.py +++ b/cloudinit/net/netops/bsd_netops.py @@ -29,7 +29,7 @@ def add_route( @staticmethod def append_route(interface: str, address: str, gateway: str): - return BsdNetOps.add_route(interface, address=address, gateway=gateway) + return BsdNetOps.add_route(interface, route=address, gateway=gateway) @staticmethod def del_route(