deploy: use nix-flake-archive instead of rsync to upload

This commit is contained in:
Jörg Thalheim
2023-09-13 20:33:51 +02:00
parent 3d071396d0
commit b97db58316

View File

@@ -1,5 +1,6 @@
import argparse import argparse
import json import json
import os
import subprocess import subprocess
from ..ssh import Host, HostGroup, HostKeyCheck from ..ssh import Host, HostGroup, HostKeyCheck
@@ -10,32 +11,20 @@ def deploy_nixos(hosts: HostGroup) -> None:
Deploy to all hosts in parallel Deploy to all hosts in parallel
""" """
flake_store_paths = {} def deploy(h: Host) -> None:
for h in hosts.hosts: target = f"{h.user or 'root'}@{h.host}"
flake_uri = str(h.meta.get("flake_uri", ".#")) ssh_arg = f"-p {h.port}" if h.port else ""
if flake_uri not in flake_store_paths: env = os.environ.copy()
env["NIX_SSHOPTS"] = ssh_arg
res = subprocess.run( res = subprocess.run(
[ ["nix", "flake", "archive", "--to", f"ssh://{target}", "--json"],
"nix",
"--extra-experimental-features",
"nix-command flakes",
"flake",
"metadata",
"--json",
flake_uri,
],
check=True, check=True,
text=True, text=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=env
) )
data = json.loads(res.stdout) data = json.loads(res.stdout)
flake_store_paths[flake_uri] = data["path"] path = data["path"]
def deploy(h: Host) -> None:
target = f"{h.user or 'root'}@{h.host}"
flake_store_path = flake_store_paths[str(h.meta.get("flake_uri", ".#"))]
flake_path = str(h.meta.get("flake_path", "/etc/nixos"))
ssh_arg = f"-p {h.port}" if h.port else ""
if h.host_key_check != HostKeyCheck.STRICT: if h.host_key_check != HostKeyCheck.STRICT:
ssh_arg += " -o StrictHostKeyChecking=no" ssh_arg += " -o StrictHostKeyChecking=no"
@@ -44,10 +33,6 @@ def deploy_nixos(hosts: HostGroup) -> None:
ssh_arg += " -i " + h.key if h.key else "" ssh_arg += " -i " + h.key if h.key else ""
h.run_local(
f"rsync --checksum -vaF --delete -e 'ssh {ssh_arg}' {flake_store_path}/ {target}:{flake_path}"
)
flake_attr = h.meta.get("flake_attr", "") flake_attr = h.meta.get("flake_attr", "")
if flake_attr: if flake_attr:
flake_attr = "#" + flake_attr flake_attr = "#" + flake_attr
@@ -71,7 +56,7 @@ def deploy_nixos(hosts: HostGroup) -> None:
"--build-host", "--build-host",
"", "",
"--flake", "--flake",
f"{flake_path}{flake_attr}", f"{path}{flake_attr}",
] ]
) )
if target_host: if target_host: