From b97db583163c27077527870faa25ccdd39538bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Sep 2023 20:33:51 +0200 Subject: [PATCH 1/2] deploy: use nix-flake-archive instead of rsync to upload --- pkgs/clan-cli/clan_cli/machines/update.py | 41 +++++++---------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/update.py b/pkgs/clan-cli/clan_cli/machines/update.py index 11a26d36..ad259cf0 100644 --- a/pkgs/clan-cli/clan_cli/machines/update.py +++ b/pkgs/clan-cli/clan_cli/machines/update.py @@ -1,5 +1,6 @@ import argparse import json +import os import subprocess from ..ssh import Host, HostGroup, HostKeyCheck @@ -10,32 +11,20 @@ def deploy_nixos(hosts: HostGroup) -> None: Deploy to all hosts in parallel """ - flake_store_paths = {} - for h in hosts.hosts: - flake_uri = str(h.meta.get("flake_uri", ".#")) - if flake_uri not in flake_store_paths: - res = subprocess.run( - [ - "nix", - "--extra-experimental-features", - "nix-command flakes", - "flake", - "metadata", - "--json", - flake_uri, - ], - check=True, - text=True, - stdout=subprocess.PIPE, - ) - data = json.loads(res.stdout) - flake_store_paths[flake_uri] = 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 "" + env = os.environ.copy() + env["NIX_SSHOPTS"] = ssh_arg + res = subprocess.run( + ["nix", "flake", "archive", "--to", f"ssh://{target}", "--json"], + check=True, + text=True, + stdout=subprocess.PIPE, + env=env + ) + data = json.loads(res.stdout) + path = data["path"] if h.host_key_check != HostKeyCheck.STRICT: ssh_arg += " -o StrictHostKeyChecking=no" @@ -44,10 +33,6 @@ def deploy_nixos(hosts: HostGroup) -> None: 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", "") if flake_attr: flake_attr = "#" + flake_attr @@ -71,7 +56,7 @@ def deploy_nixos(hosts: HostGroup) -> None: "--build-host", "", "--flake", - f"{flake_path}{flake_attr}", + f"{path}{flake_attr}", ] ) if target_host: From 2d934d527a74abed341b334499560b20163aaaa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 14 Sep 2023 11:20:27 +0200 Subject: [PATCH 2/2] update: use run_local command to print executed command --- pkgs/clan-cli/clan_cli/machines/update.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/update.py b/pkgs/clan-cli/clan_cli/machines/update.py index ad259cf0..bed009f8 100644 --- a/pkgs/clan-cli/clan_cli/machines/update.py +++ b/pkgs/clan-cli/clan_cli/machines/update.py @@ -1,6 +1,5 @@ import argparse import json -import os import subprocess from ..ssh import Host, HostGroup, HostKeyCheck @@ -14,14 +13,11 @@ def deploy_nixos(hosts: HostGroup) -> None: def deploy(h: Host) -> None: target = f"{h.user or 'root'}@{h.host}" ssh_arg = f"-p {h.port}" if h.port else "" - env = os.environ.copy() - env["NIX_SSHOPTS"] = ssh_arg - res = subprocess.run( + res = h.run_local( ["nix", "flake", "archive", "--to", f"ssh://{target}", "--json"], check=True, - text=True, stdout=subprocess.PIPE, - env=env + extra_env=dict(NIX_SSHOPTS=ssh_arg), ) data = json.loads(res.stdout) path = data["path"]