Merge pull request 'machines update: support --target-host localhost' (#4623) from jfly/clan-core:localhost-as-target-host-without-ssh into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4623
This commit is contained in:
DavHau
2025-08-12 05:10:53 +00:00
3 changed files with 15 additions and 22 deletions

View File

@@ -174,7 +174,7 @@
##############
print("TEST: update with --build-host local")
print("TEST: update with --build-host localhost --target-host localhost")
with open(machine_config_path, "w") as f:
f.write("""
{
@@ -197,15 +197,6 @@
check=True
)
# allow machine to ssh into itself
subprocess.run([
"ssh",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
f"root@192.168.1.1",
"mkdir -p /root/.ssh && chmod 700 /root/.ssh && echo \"$(cat \"${../assets/ssh/privkey}\")\" > /root/.ssh/id_ed25519 && chmod 600 /root/.ssh/id_ed25519",
], check=True)
# install the clan-cli package into the container's Nix store
subprocess.run(
[
@@ -225,7 +216,7 @@
},
)
# Run ssh on the host to run the clan update command via --build-host local
# Run ssh on the host to run the clan update command via --build-host localhost
subprocess.run([
"ssh",
"-o", "UserKnownHostsFile=/dev/null",
@@ -239,8 +230,8 @@
"--host-key-check", "none",
"--upload-inputs", # Use local store instead of fetching from network
"--build-host", "localhost",
"--target-host", "localhost",
"test-update-machine",
"--target-host", f"root@localhost",
], check=True)
# Verify the update was successful

View File

@@ -144,10 +144,14 @@ def update_command(args: argparse.Namespace) -> None:
build_host = machine.build_host()
# Figure out the target host
if args.target_host:
target_host = Remote.from_ssh_uri(
machine_name=machine.name,
address=args.target_host,
).override(host_key_check=host_key_check)
target_host: Host | None = None
if args.target_host == "localhost":
target_host = LocalHost()
else:
target_host = Remote.from_ssh_uri(
machine_name=machine.name,
address=args.target_host,
).override(host_key_check=host_key_check)
else:
target_host = machine.target_host().override(
host_key_check=host_key_check

View File

@@ -18,7 +18,7 @@ class LocalHost:
command_prefix: str = "localhost"
_user: str = field(default_factory=lambda: os.environ.get("USER", "root"))
_askpass_path: str | None = None
_sudo: bool = False
@property
def target(self) -> str:
@@ -52,11 +52,10 @@ class LocalHost:
env.update(extra_env)
# Handle sudo if needed
if self._askpass_path is not None:
if self._sudo:
# Prepend sudo command
sudo_cmd = ["sudo", "-A", "--"]
cmd = sudo_cmd + cmd
env["SUDO_ASKPASS"] = self._askpass_path
# Set options
opts.env = env
@@ -85,9 +84,8 @@ class LocalHost:
yield self
return
# For local execution, we can use sudo with askpass if GUI is available
# This is a simplified version - could be enhanced with sudo askpass proxy
yield self
# This is a simplified version - could be enhanced with sudo askpass proxy.
yield LocalHost(_sudo=True)
@contextmanager
def host_connection(self) -> Iterator["LocalHost"]: