make vm inspect non-async

This commit is contained in:
Jörg Thalheim
2023-12-08 13:59:24 +01:00
parent b8b4bedfdb
commit a98b5e205f
2 changed files with 20 additions and 9 deletions

View File

@@ -1,10 +1,11 @@
import argparse import argparse
import asyncio
import json import json
import shlex
import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from ..async_cmd import run from ..errors import ClanError
from ..nix import nix_config, nix_eval from ..nix import nix_config, nix_eval
@@ -16,9 +17,10 @@ class VmConfig:
cores: int cores: int
memory_size: int memory_size: int
graphics: bool graphics: bool
wayland: bool = False
async def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig: def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig:
config = nix_config() config = nix_config()
system = config["system"] system = config["system"]
@@ -27,8 +29,18 @@ async def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig:
f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.system.clan.vm.config' f'{flake_url}#clanInternals.machines."{system}"."{flake_attr}".config.system.clan.vm.config'
] ]
) )
out = await run(cmd) proc = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
data = json.loads(out.stdout) assert proc.stdout is not None
if proc.returncode != 0:
raise ClanError(
f"""
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
"""
)
data = json.loads(proc.stdout)
return VmConfig(flake_url=flake_url, flake_attr=flake_attr, **data) return VmConfig(flake_url=flake_url, flake_attr=flake_attr, **data)
@@ -43,8 +55,8 @@ def inspect_command(args: argparse.Namespace) -> None:
machine=args.machine, machine=args.machine,
flake=args.flake or Path.cwd(), flake=args.flake or Path.cwd(),
) )
res = asyncio.run( res = inspect_vm(
inspect_vm(flake_url=inspect_options.flake, flake_attr=inspect_options.machine) flake_url=inspect_options.flake, flake_attr=inspect_options.machine
) )
print("Cores:", res.cores) print("Cores:", res.cores)
print("Memory size:", res.memory_size) print("Memory size:", res.memory_size)

View File

@@ -1,5 +1,4 @@
import argparse import argparse
import asyncio
import json import json
import logging import logging
import os import os
@@ -286,7 +285,7 @@ def run_command(args: argparse.Namespace) -> None:
) )
flake_url = run_options.flake_url or run_options.flake flake_url = run_options.flake_url or run_options.flake
vm = asyncio.run(inspect_vm(flake_url=flake_url, flake_attr=run_options.machine)) vm = inspect_vm(flake_url=flake_url, flake_attr=run_options.machine)
run_vm(vm, run_options.nix_options) run_vm(vm, run_options.nix_options)