machines update: refactor - simplify

This is an attempt to reduce cognitive load when trying to understand the host related logic in run_machine_update.

The change should not affect behavior.

Done:
- make it very clear early on, that build_host == target_host if build_host is not set.
- rename some variables to make relations more clear
- remove `deploy_host` variable. unnecessary complexity
- remove `become_root` variable. After simplifying via boolean algebra, this boils down to `build_host == target_host`.
This commit is contained in:
DavHau
2025-07-28 18:43:04 +07:00
committed by Jörg Thalheim
parent 7961a92d32
commit 85dda9e125

View File

@@ -125,21 +125,24 @@ def run_machine_update(
with ExitStack() as stack: with ExitStack() as stack:
target_host = stack.enter_context(target_host.ssh_control_master()) target_host = stack.enter_context(target_host.ssh_control_master())
if build_host: # If no build host is specified, use the target host as the build host.
if build_host is None:
build_host = target_host
else:
build_host = stack.enter_context(build_host.ssh_control_master()) build_host = stack.enter_context(build_host.ssh_control_master())
sudo_host = stack.enter_context(target_host.become_root()) # Some operations require root privileges on the target host.
target_host_root = stack.enter_context(target_host.become_root())
generate_facts([machine], service=None, regenerate=False) generate_facts([machine], service=None, regenerate=False)
generate_vars([machine], generator_name=None, regenerate=False) generate_vars([machine], generator_name=None, regenerate=False)
upload_secrets(machine, sudo_host) # Upload secrets to the target host using root
upload_secret_vars(machine, sudo_host) upload_secrets(machine, target_host_root)
upload_secret_vars(machine, target_host_root)
if build_host: # Upload the flake's source to the build host.
path = upload_sources(machine, build_host, force_fetch_local) path = upload_sources(machine, build_host, force_fetch_local)
else:
path = upload_sources(machine, target_host, force_fetch_local)
nix_options = machine.flake.nix_options if machine.flake.nix_options else [] nix_options = machine.flake.nix_options if machine.flake.nix_options else []
@@ -157,8 +160,6 @@ def run_machine_update(
f"{path}#{machine.name}", f"{path}#{machine.name}",
] ]
become_root = True
if machine._class_ == "nixos": if machine._class_ == "nixos":
nix_options += [ nix_options += [
"--fast", "--fast",
@@ -166,8 +167,7 @@ def run_machine_update(
"", "",
] ]
if build_host: if build_host != target_host:
become_root = False
nix_options += ["--target-host", target_host.target] nix_options += ["--target-host", target_host.target]
if target_host.user != "root": if target_host.user != "root":
@@ -181,13 +181,14 @@ def run_machine_update(
*nix_options, *nix_options,
] ]
if become_root and not build_host: # If we build on the target host, we need to become root for building.
target_host = sudo_host # TODO: explain why
# TODO: why are we not just using --use-remote-sudo here as well?
if build_host == target_host:
build_host = target_host_root
deploy_host = build_host if build_host else target_host remote_env = build_host.nix_ssh_env(control_master=False)
ret = build_host.run(
remote_env = deploy_host.nix_ssh_env(control_master=False)
ret = deploy_host.run(
switch_cmd, switch_cmd,
RunOpts( RunOpts(
check=False, check=False,
@@ -222,7 +223,7 @@ def run_machine_update(
machine.info( machine.info(
"Mobile machine detected, applying workaround deployment method" "Mobile machine detected, applying workaround deployment method"
) )
ret = deploy_host.run( ret = build_host.run(
["nixos--rebuild", "test", *nix_options] if is_mobile else switch_cmd, ["nixos--rebuild", "test", *nix_options] if is_mobile else switch_cmd,
RunOpts( RunOpts(
log=Log.BOTH, log=Log.BOTH,