clan: add run_no_stdout function suppressing stdout #1471

Closed
kenji wants to merge 2 commits from a-kenji-clan/cli/facts-noisy-1443 into main
17 changed files with 366 additions and 36 deletions

View File

@ -0,0 +1,2 @@
# DO NOT DELETE
# This file is used by the clan cli to discover a clan flake

View File

@ -126,7 +126,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/back
parser_flake = subparsers.add_parser(
"flakes",
help="create a clan flake inside the current directory",
description="create a clan flake inside the current directory",
epilog=(
"""
Examples:
@ -144,7 +143,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started
parser_config = subparsers.add_parser(
"config",
help="set nixos configuration",
description="set nixos configuration",
epilog=(
"""
"""
@ -177,7 +175,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/depl
parser_secrets = subparsers.add_parser(
"secrets",
help="manage secrets",
description="manage secrets",
epilog=(
"""
This subcommand provides an interface to secret facts.
@ -201,7 +198,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/secr
parser_facts = subparsers.add_parser(
"facts",
help="manage facts",
description="manage facts",
epilog=(
"""
@ -238,7 +234,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/secr
parser_machine = subparsers.add_parser(
"machines",
help="manage machines and their configuration",
description="manage machines and their configuration",
epilog=(
"""
This subcommand provides an interface to machines managed by clan.

View File

@ -140,3 +140,23 @@ def run(
raise ClanCmdError(cmd_out)
return cmd_out
def run_no_stdout(
cmd: list[str],
*,
env: dict[str, str] | None = None,
cwd: Path = Path.cwd(),
log: Log = Log.STDERR,
check: bool = True,
error_msg: str | None = None,
) -> CmdOut:
"""
Like run, but automatically suppresses stdout, if not in DEBUG log level.
If in DEBUG log level the stdout of commands will be shown.
"""
if logging.getLogger(__name__.split(".")[0]).isEnabledFor(logging.DEBUG):
return run(cmd, env=env, log=log, check=check, error_msg=error_msg)
else:
log = Log.NONE
return run(cmd, env=env, log=log, check=check, error_msg=error_msg)

View File

@ -8,7 +8,7 @@ import sys
from pathlib import Path
from typing import Any, get_origin
from clan_cli.cmd import run
from clan_cli.cmd import run_no_stdout
from clan_cli.dirs import machine_settings_file
from clan_cli.errors import ClanError
from clan_cli.git import commit_file
@ -116,7 +116,7 @@ def options_for_machine(
f"{clan_dir}#nixosConfigurations.{machine_name}.config.clanCore.optionsNix"
)
cmd = nix_eval(flags=flags)
proc = run(
proc = run_no_stdout(
cmd,
error_msg=f"Failed to read options for machine {machine_name}",
)
@ -136,7 +136,7 @@ def read_machine_option_value(
f"{clan_dir}#nixosConfigurations.{machine_name}.config.{option}",
],
)
proc = run(cmd, error_msg=f"Failed to read option {option}")
proc = run_no_stdout(cmd, error_msg=f"Failed to read option {option}")
value = json.loads(proc.stdout)
# print the value so that the output can be copied and fed as an input.

View File

@ -2,7 +2,7 @@ import json
from pathlib import Path
from typing import Any
from ..cmd import run
from ..cmd import run_no_stdout
from ..errors import ClanError
from ..nix import nix_eval
@ -32,7 +32,7 @@ def schema_from_module_file(
"""
# run the nix expression and parse the output as json
cmd = nix_eval(["--expr", nix_expr])
proc = run(cmd)
proc = run_no_stdout(cmd)
return json.loads(proc.stdout)

View File

@ -2,7 +2,7 @@
import argparse
from pathlib import Path
from ..cmd import CmdOut, run
from ..cmd import CmdOut, run, run_no_stdout
from ..errors import ClanError
from ..nix import nix_command, nix_shell
@ -30,7 +30,7 @@ def create_flake(directory: Path, url: str) -> dict[str, CmdOut]:
response["git init"] = out
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
out = run(command, cwd=directory)
out = run_no_stdout(command, cwd=directory)
response["git add"] = out
command = nix_shell(["nixpkgs#git"], ["git", "config", "user.name", "clan-tool"])

View File

@ -2,7 +2,7 @@ import argparse
from dataclasses import dataclass
from pathlib import Path
from ..cmd import run
from ..cmd import run_no_stdout
from ..dirs import machine_gcroot
from ..errors import ClanError
from ..machines.list import list_machines
@ -30,7 +30,7 @@ class FlakeConfig:
def run_cmd(cmd: list[str]) -> str:
proc = run(cmd)
proc = run_no_stdout(cmd)
return proc.stdout.strip()

View File

@ -12,7 +12,7 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
from .cmd import Log, run
from .cmd import Log, run, run_no_stdout
from .errors import ClanError
from .facts.secret_modules import SecretStoreBase
from .machines.machines import Machine
@ -60,7 +60,7 @@ def get_keymap_and_locale() -> dict[str, str]:
keymap = "en"
# Execute the `localectl status` command
result = run(["localectl", "status"])
result = run_no_stdout(["localectl", "status"])
if result.returncode == 0:
output = result.stdout

View File

@ -4,7 +4,7 @@ from pathlib import Path
from clan_cli.errors import ClanError
from clan_cli.nix import nix_shell
from .cmd import Log, run
from .cmd import Log, run, run_no_stdout
from .locked_open import locked_open
@ -78,7 +78,7 @@ def _commit_file_to_git(
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code"]
+ [str(file_path) for file_path in file_paths],
)
result = run(cmd, check=False, cwd=repo_dir)
result = run_no_stdout(cmd, check=False, cwd=repo_dir)
# if there is no diff, return
if result.returncode == 0:
return
@ -97,6 +97,6 @@ def _commit_file_to_git(
+ [str(file_path) for file_path in file_paths],
)
run(
run_no_stdout(
cmd, error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}"
)

View File

@ -1,7 +1,7 @@
import json
from pathlib import Path
from ..cmd import run
from ..cmd import run_no_stdout
from ..nix import nix_build, nix_config
from .machines import Machine
@ -10,7 +10,7 @@ from .machines import Machine
def get_all_machines(flake_dir: Path) -> list[Machine]:
config = nix_config()
system = config["system"]
json_path = run(
json_path = run_no_stdout(
nix_build([f'{flake_dir}#clanInternals.all-machines-json."{system}"'])
).stdout

View File

@ -6,7 +6,7 @@ from pathlib import Path
from clan_cli.api import API
from ..cmd import Log, run
from ..cmd import run_no_stdout
from ..nix import nix_config, nix_eval
log = logging.getLogger(__name__)
@ -34,10 +34,7 @@ def list_machines(flake_url: str | Path, debug: bool) -> dict[str, MachineInfo]:
]
)
if not debug:
proc = run(cmd, log=Log.NONE)
else:
proc = run(cmd)
proc = run_no_stdout(cmd)
res = proc.stdout.strip()
machines_dict = json.loads(res)

View File

@ -10,7 +10,7 @@ from clan_cli.clan_uri import ClanURI, MachineData
from clan_cli.dirs import vm_state_dir
from clan_cli.qemu.qmp import QEMUMonitorProtocol
from ..cmd import run
from ..cmd import run_no_stdout
from ..errors import ClanError
from ..nix import nix_build, nix_config, nix_eval, nix_metadata
from ..ssh import Host, parse_deployment_address
@ -197,15 +197,15 @@ class Machine:
config_json.flush()
file_info = json.loads(
run(
run_no_stdout(
nix_eval(
[
"--impure",
"--expr",
f'let x = (builtins.fetchTree {{ type = "file"; url = "file://{config_json.name}"; }}); in {{ narHash = x.narHash; path = x.outPath; }}',
]
)
).stdout.strip()
),
).stdout.strip(),
)
args = []
@ -247,10 +247,10 @@ class Machine:
]
if method == "eval":
output = run(nix_eval(args)).stdout.strip()
output = run_no_stdout(nix_eval(args)).stdout.strip()
return output
elif method == "build":
outpath = run(nix_build(args)).stdout.strip()
outpath = run_no_stdout(nix_build(args)).stdout.strip()
return Path(outpath)
else:
raise ValueError(f"Unknown method {method}")

View File

@ -4,7 +4,7 @@ import tempfile
from pathlib import Path
from typing import Any
from .cmd import run
from .cmd import run_no_stdout
from .dirs import nixpkgs_flake, nixpkgs_source
@ -55,12 +55,12 @@ def nix_build(flags: list[str], gcroot: Path | None = None) -> list[str]:
def nix_add_to_gcroots(nix_path: Path, dest: Path) -> None:
cmd = ["nix-store", "--realise", f"{nix_path}", "--add-root", f"{dest}"]
run(cmd)
run_no_stdout(cmd)
def nix_config() -> dict[str, Any]:
cmd = nix_command(["show-config", "--json"])
proc = run(cmd)
proc = run_no_stdout(cmd)
data = json.loads(proc.stdout)
config = {}
for key, value in data.items():
@ -95,7 +95,7 @@ def nix_eval(flags: list[str]) -> list[str]:
def nix_metadata(flake_url: str | Path) -> dict[str, Any]:
cmd = nix_command(["flake", "metadata", "--json", f"{flake_url}"])
proc = run(cmd)
proc = run_no_stdout(cmd)
data = json.loads(proc.stdout)
return data

213
pkgs/clan-cli/flake.lock Normal file
View File

@ -0,0 +1,213 @@
{
"nodes": {
"clan-core": {
"inputs": {
"disko": "disko",
"flake-parts": "flake-parts",
"nixos-generators": "nixos-generators",
"nixos-images": "nixos-images",
"nixpkgs": "nixpkgs",
"sops-nix": "sops-nix",
"treefmt-nix": "treefmt-nix"
},
"locked": {
"lastModified": 1716881889,
"narHash": "sha256-f0Mr0Dr0pPcZV4bPj0F8qSkCBOI7GASeo9cCV2A7buc=",
"ref": "refs/heads/main",
"rev": "9394760e3be00abbe353e28f442bf51302b91cf5",
"revCount": 2876,
"type": "git",
"url": "https://git.clan.lol/clan/clan-core"
},
"original": {
"type": "git",
"url": "https://git.clan.lol/clan/clan-core"
}
},
"disko": {
"inputs": {
"nixpkgs": [
"clan-core",
"nixpkgs"
]
},
"locked": {
"lastModified": 1716394172,
"narHash": "sha256-B+pNhV8GFeCj9/MoH+qtGqKbgv6fU4hGaw2+NoYYtB0=",
"owner": "nix-community",
"repo": "disko",
"rev": "23c63fb09334c3e8958b57e2ddc3870b75b9111d",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "disko",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": [
"clan-core",
"nixpkgs"
]
},
"locked": {
"lastModified": 1715865404,
"narHash": "sha256-/GJvTdTpuDjNn84j82cU6bXztE0MSkdnTWClUCRub78=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "8dc45382d5206bd292f9c2768b8058a8fd8311d9",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixlib": {
"locked": {
"lastModified": 1712450863,
"narHash": "sha256-K6IkdtMtq9xktmYPj0uaYc8NsIqHuaAoRBaMgu9Fvrw=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "3c62b6a12571c9a7f65ab037173ee153d539905f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"nixos-2311": {
"locked": {
"lastModified": 1715818734,
"narHash": "sha256-WvAJWCwPj/6quKcsgsvQYyZRxV8ho/yUzj0HZQ34DVU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "95742536dc6debb5a8b8b78b27001c38f369f1e7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "release-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"nixos-generators": {
"inputs": {
"nixlib": "nixlib",
"nixpkgs": [
"clan-core",
"nixpkgs"
]
},
"locked": {
"lastModified": 1716123454,
"narHash": "sha256-U2o4UPM/UsEyIX2p11+YEQgR9HY3PmjZ2mRl/x5e4xo=",
"owner": "nix-community",
"repo": "nixos-generators",
"rev": "a63e0c83dd83fe28cc571b97129e13373436bd82",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixos-generators",
"type": "github"
}
},
"nixos-images": {
"inputs": {
"nixos-2311": "nixos-2311",
"nixos-unstable": [
"clan-core",
"nixpkgs"
]
},
"locked": {
"lastModified": 1716132123,
"narHash": "sha256-rATSWbPaKQfZGaemu0tHL2xfCzVIVwpuTjk+KSBC+k4=",
"owner": "nix-community",
"repo": "nixos-images",
"rev": "8c9cab8c44434c12dafc465fbf61a710c5bceb08",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixos-images",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1716127062,
"narHash": "sha256-2rk8FqB/iQV2d0vQLs684/Tj5PUHaS1sFwG7fng5vXE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8a2555763c48e2410054de3f52f7310ce3241ec5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable-small",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"clan-core": "clan-core"
}
},
"sops-nix": {
"inputs": {
"nixpkgs": [
"clan-core",
"nixpkgs"
],
"nixpkgs-stable": [
"clan-core"
]
},
"locked": {
"lastModified": 1716087663,
"narHash": "sha256-zuSAGlx8Qk0OILGCC2GUyZ58/SJ5R3GZdeUNQ6IS0fQ=",
"owner": "Mic92",
"repo": "sops-nix",
"rev": "0bf1808e70ce80046b0cff821c019df2b19aabf5",
"type": "github"
},
"original": {
"owner": "Mic92",
"repo": "sops-nix",
"type": "github"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"clan-core",
"nixpkgs"
]
},
"locked": {
"lastModified": 1715940852,
"narHash": "sha256-wJqHMg/K6X3JGAE9YLM0LsuKrKb4XiBeVaoeMNlReZg=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "2fba33a182602b9d49f0b2440513e5ee091d838b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

95
pkgs/clan-cli/flake.nix Normal file
View File

@ -0,0 +1,95 @@
{
description = "<Put your description here>";
inputs.clan-core.url = "git+https://git.clan.lol/clan/clan-core";
outputs =
{ self, clan-core, ... }:
let
system = "x86_64-linux";
pkgs = clan-core.inputs.nixpkgs.legacyPackages.${system};
# Usage see: https://docs.clan.lol
clan = clan-core.lib.buildClan {
directory = self;
clanName = "__CHANGE_ME__"; # Ensure this is internet wide unique.
# Prerequisite: boot into the installer
# See: https://docs.clan.lol/getting-started/installer
# local> mkdir -p ./machines/machine1
# local> Edit ./machines/machine1/configuration.nix to your liking
machines = {
# "jon" will be the hostname of the machine
jon = {
imports = [
./modules/shared.nix
./machines/jon/configuration.nix
];
nixpkgs.hostPlatform = system;
# Set this for clan commands use ssh i.e. `clan machines update`
# If you change the hostname, you need to update this line to root@<new-hostname>
# This only works however if you have avahi running on your admin machine else use IP
clan.networking.targetHost = pkgs.lib.mkDefault "root@jon";
# ssh root@flash-installer.local lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT
disko.devices.disk.main = {
device = "/dev/disk/by-id/__CHANGE_ME__";
};
# IMPORTANT! Add your SSH key here
# e.g. > cat ~/.ssh/id_ed25519.pub
users.users.root.openssh.authorizedKeys.keys = throw ''
Don't forget to add your SSH key here!
users.users.root.openssh.authorizedKeys.keys = [ "<YOUR SSH_KEY>" ]
'';
# Zerotier needs one controller to accept new nodes. Once accepted
# the controller can be offline and routing still works.
clan.networking.zerotier.controller.enable = true;
};
# "sara" will be the hostname of the machine
sara = {
imports = [
./modules/shared.nix
./machines/sara/configuration.nix
];
nixpkgs.hostPlatform = system;
# Set this for clan commands use ssh i.e. `clan machines update`
# If you change the hostname, you need to update this line to root@<new-hostname>
# This only works however if you have avahi running on your admin machine else use IP
clan.networking.targetHost = pkgs.lib.mkDefault "root@sara";
# ssh root@flash-installer.local lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT
disko.devices.disk.main = {
device = "/dev/disk/by-id/__CHANGE_ME__";
};
# IMPORTANT! Add your SSH key here
# e.g. > cat ~/.ssh/id_ed25519.pub
users.users.root.openssh.authorizedKeys.keys = throw ''
Don't forget to add your SSH key here!
users.users.root.openssh.authorizedKeys.keys = [ "<YOUR SSH_KEY>" ]
'';
/*
After jon is deployed, uncomment the following line
This will allow sara to share the VPN overlay network with jon
The networkId is generated by the first deployment of jon
*/
# clan.networking.zerotier.networkId = builtins.readFile ../jon/facts/zerotier-network-id;
};
};
};
in
{
# all machines managed by Clan
inherit (clan) nixosConfigurations clanInternals;
# add the Clan cli tool to the dev shell
devShells.${system}.default = pkgs.mkShell {
packages = [ clan-core.packages.${system}.clan-cli ];
};
};
}

View File

@ -0,0 +1,7 @@
{ clan-core, ... }:
{
imports = [
clan-core.clanModules.sshd
clan-core.clanModules.root-password
];
}

View File

@ -35,6 +35,7 @@ def test_create_flake(
hw_config_nix.write("{}")
cli.run(["machines", "list"])
print(capsys.readouterr().out)
assert "machine1" in capsys.readouterr().out
flake_show = subprocess.run(
["nix", "flake", "show", "--json"],