Updated to main
This commit is contained in:
@@ -9,10 +9,9 @@ import sys
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Tuple, get_origin
|
||||
|
||||
from clan_cli.dirs import get_clan_flake_toplevel
|
||||
from clan_cli.dirs import get_clan_flake_toplevel, machine_settings_file
|
||||
from clan_cli.errors import ClanError
|
||||
from clan_cli.git import commit_file
|
||||
from clan_cli.machines.folders import machine_settings_file
|
||||
from clan_cli.nix import nix_eval
|
||||
|
||||
script_dir = Path(__file__).parent
|
||||
@@ -154,6 +153,39 @@ def read_machine_option_value(
|
||||
return out
|
||||
|
||||
|
||||
def get_or_set_option(args: argparse.Namespace) -> None:
|
||||
if args.value == []:
|
||||
print(read_machine_option_value(args.machine, args.option, args.show_trace))
|
||||
else:
|
||||
# load options
|
||||
if args.options_file is None:
|
||||
options = options_for_machine(
|
||||
machine_name=args.machine, show_trace=args.show_trace
|
||||
)
|
||||
else:
|
||||
with open(args.options_file) as f:
|
||||
options = json.load(f)
|
||||
# compute settings json file location
|
||||
if args.settings_file is None:
|
||||
get_clan_flake_toplevel()
|
||||
settings_file = machine_settings_file(args.flake, args.machine)
|
||||
else:
|
||||
settings_file = args.settings_file
|
||||
# set the option with the given value
|
||||
set_option(
|
||||
option=args.option,
|
||||
value=args.value,
|
||||
options=options,
|
||||
settings_file=settings_file,
|
||||
option_description=args.option,
|
||||
show_trace=args.show_trace,
|
||||
)
|
||||
if not args.quiet:
|
||||
new_value = read_machine_option_value(args.machine, args.option)
|
||||
print(f"New Value for {args.option}:")
|
||||
print(new_value)
|
||||
|
||||
|
||||
def find_option(
|
||||
option: str, value: Any, options: dict, option_description: Optional[str] = None
|
||||
) -> Tuple[str, Any]:
|
||||
@@ -258,38 +290,6 @@ def set_option(
|
||||
commit_file(settings_file, commit_message=f"Set option {option_description}")
|
||||
|
||||
|
||||
def get_or_set_option(args: argparse.Namespace) -> None:
|
||||
if args.value == []:
|
||||
print(read_machine_option_value(args.machine, args.option, args.show_trace))
|
||||
else:
|
||||
# load options
|
||||
if args.options_file is None:
|
||||
options = options_for_machine(
|
||||
machine_name=args.machine, show_trace=args.show_trace
|
||||
)
|
||||
else:
|
||||
with open(args.options_file) as f:
|
||||
options = json.load(f)
|
||||
# compute settings json file location
|
||||
if args.settings_file is None:
|
||||
get_clan_flake_toplevel()
|
||||
settings_file = machine_settings_file(args.machine)
|
||||
else:
|
||||
settings_file = args.settings_file
|
||||
# set the option with the given value
|
||||
set_option(
|
||||
option=args.option,
|
||||
value=args.value,
|
||||
options=options,
|
||||
settings_file=settings_file,
|
||||
option_description=args.option,
|
||||
show_trace=args.show_trace,
|
||||
)
|
||||
if not args.quiet:
|
||||
new_value = read_machine_option_value(args.machine, args.option)
|
||||
print(f"New Value for {args.option}:")
|
||||
print(new_value)
|
||||
|
||||
|
||||
# takes a (sub)parser and configures it
|
||||
def register_parser(
|
||||
@@ -302,7 +302,11 @@ def register_parser(
|
||||
|
||||
# inject callback function to process the input later
|
||||
parser.set_defaults(func=get_or_set_option)
|
||||
|
||||
parser.add_argument(
|
||||
"flake",
|
||||
type=str,
|
||||
help="name of the flake to set machine options for",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--machine",
|
||||
"-m",
|
||||
|
||||
@@ -3,14 +3,16 @@ import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from clan_cli.dirs import get_clan_flake_toplevel, nixpkgs_source
|
||||
from clan_cli.dirs import (
|
||||
get_flake_path,
|
||||
machine_settings_file,
|
||||
nixpkgs_source,
|
||||
specific_machine_dir,
|
||||
)
|
||||
from clan_cli.git import commit_file, find_git_repo_root
|
||||
from clan_cli.machines.folders import machine_folder, machine_settings_file
|
||||
from clan_cli.nix import nix_eval
|
||||
|
||||
|
||||
@@ -52,26 +54,26 @@ def verify_machine_config(
|
||||
|
||||
def config_for_machine(machine_name: str) -> dict:
|
||||
# read the config from a json file located at {flake}/machines/{machine_name}/settings.json
|
||||
if not machine_folder(machine_name).exists():
|
||||
if not specific_machine_dir(flake_name, machine_name).exists():
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Machine {machine_name} not found. Create the machine first`",
|
||||
)
|
||||
settings_path = machine_settings_file(machine_name)
|
||||
settings_path = machine_settings_file(flake_name, machine_name)
|
||||
if not settings_path.exists():
|
||||
return {}
|
||||
with open(settings_path) as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def set_config_for_machine(machine_name: str, config: dict) -> None:
|
||||
def set_config_for_machine(flake_name: str, machine_name: str, config: dict) -> None:
|
||||
# write the config to a json file located at {flake}/machines/{machine_name}/settings.json
|
||||
if not machine_folder(machine_name).exists():
|
||||
if not specific_machine_dir(flake_name, machine_name).exists():
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Machine {machine_name} not found. Create the machine first`",
|
||||
)
|
||||
settings_path = machine_settings_file(machine_name)
|
||||
settings_path = machine_settings_file(flake_name, machine_name)
|
||||
settings_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(settings_path, "w") as f:
|
||||
json.dump(config, f)
|
||||
@@ -81,50 +83,76 @@ def set_config_for_machine(machine_name: str, config: dict) -> None:
|
||||
commit_file(settings_path, repo_dir)
|
||||
|
||||
|
||||
def schema_for_machine(
|
||||
machine_name: str, config: Optional[dict] = None, flake: Optional[Path] = None
|
||||
) -> dict:
|
||||
if flake is None:
|
||||
flake = get_clan_flake_toplevel()
|
||||
# use nix eval to lib.evalModules .#nixosConfigurations.<machine_name>.options.clan
|
||||
with NamedTemporaryFile(mode="w") as clan_machine_settings_file:
|
||||
env = os.environ.copy()
|
||||
inject_config_flags = []
|
||||
if config is not None:
|
||||
json.dump(config, clan_machine_settings_file, indent=2)
|
||||
clan_machine_settings_file.seek(0)
|
||||
env["CLAN_MACHINE_SETTINGS_FILE"] = clan_machine_settings_file.name
|
||||
inject_config_flags = [
|
||||
"--impure", # needed to access CLAN_MACHINE_SETTINGS_FILE
|
||||
]
|
||||
proc = subprocess.run(
|
||||
nix_eval(
|
||||
flags=inject_config_flags
|
||||
+ [
|
||||
"--impure",
|
||||
"--show-trace",
|
||||
"--expr",
|
||||
f"""
|
||||
let
|
||||
flake = builtins.getFlake (toString {flake});
|
||||
lib = import {nixpkgs_source()}/lib;
|
||||
options = flake.nixosConfigurations.{machine_name}.options;
|
||||
clanOptions = options.clan;
|
||||
jsonschemaLib = import {Path(__file__).parent / "jsonschema"} {{ inherit lib; }};
|
||||
jsonschema = jsonschemaLib.parseOptions clanOptions;
|
||||
in
|
||||
jsonschema
|
||||
""",
|
||||
],
|
||||
),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=flake,
|
||||
env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
print(proc.stderr, file=sys.stderr)
|
||||
raise Exception(
|
||||
f"Failed to read schema for machine {machine_name}:\n{proc.stderr}"
|
||||
)
|
||||
return json.loads(proc.stdout)
|
||||
def schema_for_machine(flake_name: str, machine_name: str) -> dict:
|
||||
flake = get_flake_path(flake_name)
|
||||
|
||||
# use nix eval to lib.evalModules .#nixosModules.machine-{machine_name}
|
||||
proc = subprocess.run(
|
||||
nix_eval(
|
||||
flags=[
|
||||
"--impure",
|
||||
"--show-trace",
|
||||
"--expr",
|
||||
f"""
|
||||
let
|
||||
flake = builtins.getFlake (toString {flake});
|
||||
lib = import {nixpkgs_source()}/lib;
|
||||
options = flake.nixosConfigurations.{machine_name}.options;
|
||||
clanOptions = options.clan;
|
||||
jsonschemaLib = import {Path(__file__).parent / "jsonschema"} {{ inherit lib; }};
|
||||
jsonschema = jsonschemaLib.parseOptions clanOptions;
|
||||
in
|
||||
jsonschema
|
||||
""",
|
||||
],
|
||||
),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
# def schema_for_machine(
|
||||
# machine_name: str, config: Optional[dict] = None, flake: Optional[Path] = None
|
||||
# ) -> dict:
|
||||
# if flake is None:
|
||||
# flake = get_clan_flake_toplevel()
|
||||
# # use nix eval to lib.evalModules .#nixosConfigurations.<machine_name>.options.clan
|
||||
# with NamedTemporaryFile(mode="w") as clan_machine_settings_file:
|
||||
# env = os.environ.copy()
|
||||
# inject_config_flags = []
|
||||
# if config is not None:
|
||||
# json.dump(config, clan_machine_settings_file, indent=2)
|
||||
# clan_machine_settings_file.seek(0)
|
||||
# env["CLAN_MACHINE_SETTINGS_FILE"] = clan_machine_settings_file.name
|
||||
# inject_config_flags = [
|
||||
# "--impure", # needed to access CLAN_MACHINE_SETTINGS_FILE
|
||||
# ]
|
||||
# proc = subprocess.run(
|
||||
# nix_eval(
|
||||
# flags=inject_config_flags
|
||||
# + [
|
||||
# "--impure",
|
||||
# "--show-trace",
|
||||
# "--expr",
|
||||
# f"""
|
||||
# let
|
||||
# flake = builtins.getFlake (toString {flake});
|
||||
# lib = import {nixpkgs_source()}/lib;
|
||||
# options = flake.nixosConfigurations.{machine_name}.options;
|
||||
# clanOptions = options.clan;
|
||||
# jsonschemaLib = import {Path(__file__).parent / "jsonschema"} {{ inherit lib; }};
|
||||
# jsonschema = jsonschemaLib.parseOptions clanOptions;
|
||||
# in
|
||||
# jsonschema
|
||||
# """,
|
||||
# ],
|
||||
# ),
|
||||
# capture_output=True,
|
||||
# text=True,
|
||||
# cwd=flake,
|
||||
# env=env,
|
||||
# )
|
||||
# if proc.returncode != 0:
|
||||
# print(proc.stderr, file=sys.stderr)
|
||||
# raise Exception(
|
||||
# f"Failed to read schema for machine {machine_name}:\n{proc.stderr}"
|
||||
# )
|
||||
# return json.loads(proc.stdout)
|
||||
|
||||
Reference in New Issue
Block a user