add support for build machines

This commit is contained in:
Jörg Thalheim 2024-02-06 16:11:55 +01:00
parent 2315dba2a9
commit be3a75bbd7
3 changed files with 24 additions and 21 deletions

View File

@ -97,6 +97,23 @@ class Machine:
self.name, self.target_host_address, meta={"machine": self} self.name, self.target_host_address, meta={"machine": self}
) )
@property
def build_host(self) -> Host:
"""
The host where the machine is built and deployed from.
Can be the same as the target host.
"""
build_host = self.deployment_info.get("buildHost")
if build_host is None:
return self.target_host
# enable ssh agent forwarding to allow the build host to access the target host
return parse_deployment_address(
self.name,
build_host,
forward_agent=True,
meta={"machine": self, "target_host": self.target_host},
)
def eval_nix(self, attr: str, refresh: bool = False) -> str: def eval_nix(self, attr: str, refresh: bool = False) -> str:
""" """
eval a nix attribute of the machine eval a nix attribute of the machine

View File

@ -110,11 +110,6 @@ def deploy_nixos(hosts: HostGroup) -> None:
generate_secrets(machine) generate_secrets(machine)
upload_secrets(machine) upload_secrets(machine)
target_host = h.meta.get("target_host")
if target_host:
target_user = h.meta.get("target_user")
if target_user:
target_host = f"{target_user}@{target_host}"
extra_args = h.meta.get("extra_args", []) extra_args = h.meta.get("extra_args", [])
cmd = [ cmd = [
"nixos-rebuild", "nixos-rebuild",
@ -132,7 +127,8 @@ def deploy_nixos(hosts: HostGroup) -> None:
"--flake", "--flake",
f"{path}#{machine.name}", f"{path}#{machine.name}",
] ]
if target_host: if target_host := h.meta.get("target_host"):
target_host = f"{target_host.user or 'root'}@{target_host.host}"
cmd.extend(["--target-host", target_host]) cmd.extend(["--target-host", target_host])
ret = h.run(cmd, check=False) ret = h.run(cmd, check=False)
# re-retry switch if the first time fails # re-retry switch if the first time fails
@ -157,16 +153,10 @@ def get_all_machines(clan_dir: Path) -> HostGroup:
for name, machine_data in machines.items(): for name, machine_data in machines.items():
machine = Machine(name=name, flake=clan_dir, deployment_info=machine_data) machine = Machine(name=name, flake=clan_dir, deployment_info=machine_data)
try: try:
machine.target_host_address hosts.append(machine.build_host)
except ClanError: except ClanError:
ignored_machines.append(name) ignored_machines.append(name)
continue continue
host = parse_deployment_address(
name,
host=machine.target_host_address,
meta={"machine": machine},
)
hosts.append(host)
if not hosts and ignored_machines != []: if not hosts and ignored_machines != []:
print( print(
"WARNING: No machines to update. The following defined machines were ignored because they do not have `clan.networking.targetHost` nixos option set:", "WARNING: No machines to update. The following defined machines were ignored because they do not have `clan.networking.targetHost` nixos option set:",
@ -182,7 +172,7 @@ def get_selected_machines(machine_names: list[str], flake_dir: Path) -> HostGrou
hosts = [] hosts = []
for name in machine_names: for name in machine_names:
machine = Machine(name=name, flake=flake_dir) machine = Machine(name=name, flake=flake_dir)
hosts.append(machine.target_host) hosts.append(machine.build_host)
return HostGroup(hosts) return HostGroup(hosts)

View File

@ -16,12 +16,7 @@ from enum import Enum
from pathlib import Path from pathlib import Path
from shlex import quote from shlex import quote
from threading import Thread from threading import Thread
from typing import ( from typing import IO, Any, Generic, TypeVar
IO,
Any,
Generic,
TypeVar,
)
# https://no-color.org # https://no-color.org
DISABLE_COLOR = not sys.stderr.isatty() or os.environ.get("NO_COLOR", "") != "" DISABLE_COLOR = not sys.stderr.isatty() or os.environ.get("NO_COLOR", "") != ""
@ -753,7 +748,7 @@ class HostGroup:
def parse_deployment_address( def parse_deployment_address(
machine_name: str, host: str, meta: dict[str, Any] = {} machine_name: str, host: str, forward_agent: bool = True, meta: dict[str, Any] = {}
) -> Host: ) -> Host:
parts = host.split("@") parts = host.split("@")
user: str | None = None user: str | None = None
@ -780,6 +775,7 @@ def parse_deployment_address(
user=user, user=user,
port=port, port=port,
command_prefix=machine_name, command_prefix=machine_name,
forward_agent=forward_agent,
meta=meta, meta=meta,
ssh_options=options, ssh_options=options,
) )