From 5a8172f232e2ab58c024480d91f64a5f06472408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 28 Nov 2023 10:01:32 +0100 Subject: [PATCH] allow ipv6 addresses in deployment addresses --- pkgs/clan-cli/clan_cli/ssh/__init__.py | 11 ++++++----- pkgs/clan-cli/tests/test_ssh_remote.py | 8 +++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/ssh/__init__.py b/pkgs/clan-cli/clan_cli/ssh/__init__.py index 2aba7e480..47c83b279 100644 --- a/pkgs/clan-cli/clan_cli/ssh/__init__.py +++ b/pkgs/clan-cli/clan_cli/ssh/__init__.py @@ -9,6 +9,7 @@ import shlex import subprocess import sys import time +import urllib.parse from contextlib import ExitStack, contextmanager from enum import Enum from pathlib import Path @@ -775,11 +776,11 @@ def parse_deployment_address( for option in maybe_options[1].split("&"): k, v = option.split("=") options[k] = v - maybe_port = hostname.split(":") - port = None - if len(maybe_port) > 1: - hostname = maybe_port[0] - port = int(maybe_port[1]) + result = urllib.parse.urlsplit("//" + hostname) + if not result.hostname: + raise Exception(f"Invalid hostname: {hostname}") + hostname = result.hostname + port = result.port meta = meta.copy() meta["flake_attr"] = machine_name return Host( diff --git a/pkgs/clan-cli/tests/test_ssh_remote.py b/pkgs/clan-cli/tests/test_ssh_remote.py index 8a440bdc5..ec727f58a 100644 --- a/pkgs/clan-cli/tests/test_ssh_remote.py +++ b/pkgs/clan-cli/tests/test_ssh_remote.py @@ -1,6 +1,12 @@ import subprocess -from clan_cli.ssh import Host, HostGroup +from clan_cli.ssh import Host, HostGroup, parse_deployment_address + + +def test_parse_ipv6() -> None: + host = parse_deployment_address("foo", "[fe80::1%eth0]:2222") + assert host.host == "fe80::1%eth0" + assert host.port == 2222 def test_run(host_group: HostGroup) -> None: