enable ASYNC, DTZ, YTT and EM lints

This commit is contained in:
Jörg Thalheim
2024-09-02 13:55:46 +02:00
parent d5440594be
commit 15ff74f7c2
98 changed files with 526 additions and 421 deletions

View File

@@ -20,16 +20,16 @@ log = logging.getLogger(__name__)
def create_machine(flake: FlakeId, machine: Machine) -> None:
hostname_regex = r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$"
if not re.match(hostname_regex, machine.name):
raise ClanError(
"Machine name must be a valid hostname", location="Create Machine"
)
msg = "Machine name must be a valid hostname"
raise ClanError(msg, location="Create Machine")
inventory = load_inventory_json(flake.path)
full_inventory = load_inventory_eval(flake.path)
if machine.name in full_inventory.machines.keys():
raise ClanError(f"Machine with the name {machine.name} already exists")
msg = f"Machine with the name {machine.name} already exists"
raise ClanError(msg)
print(f"Define machine {machine.name}", machine)

View File

@@ -15,7 +15,8 @@ def delete_machine(flake: FlakeId, name: str) -> None:
machine = inventory.machines.pop(name, None)
if machine is None:
raise ClanError(f"Machine {name} does not exist")
msg = f"Machine {name} does not exist"
raise ClanError(msg)
save_inventory(inventory, flake.path, f"Delete machine {name}")

View File

@@ -159,7 +159,8 @@ def generate_machine_hardware_info(
if out.returncode != 0:
log.error(f"Failed to inspect {machine_name}. Address: {hostname}")
log.error(out)
raise ClanError(f"Failed to inspect {machine_name}. Address: {hostname}")
msg = f"Failed to inspect {machine_name}. Address: {hostname}"
raise ClanError(msg)
hw_file = Path(
f"{clan_dir}/machines/{machine_name}/{hw_nix_file if report_type == 'nixos-generate-config' else facter_file}"
@@ -170,8 +171,9 @@ def generate_machine_hardware_info(
is_template = hw_file.exists() and "throw" in hw_file.read_text()
if hw_file.exists() and not force and not is_template:
msg = "File exists."
raise ClanError(
"File exists.",
msg,
description="Hardware file already exists. To force overwrite the existing configuration use '--force'.",
location=f"{__name__} {hw_file}",
)
@@ -205,8 +207,9 @@ def generate_machine_hardware_info(
backup_file.replace(hw_file)
# TODO: Undo the commit
msg = "Invalid hardware-configuration.nix file"
raise ClanError(
"Invalid hardware-configuration.nix file",
msg,
description="The hardware-configuration.nix file is invalid. Please check the file and try again.",
location=f"{__name__} {hw_file}",
) from e

View File

@@ -132,7 +132,8 @@ def install_command(args: argparse.Namespace) -> None:
json_ssh_deploy = json.loads(qrcode_scan(args.png))
if not json_ssh_deploy and not args.target_host:
raise ClanError("No target host provided, please provide a target host.")
msg = "No target host provided, please provide a target host."
raise ClanError(msg)
if json_ssh_deploy:
target_host = f"root@{find_reachable_host_from_deploy_json(json_ssh_deploy)}"
@@ -171,13 +172,12 @@ def find_reachable_host_from_deploy_json(deploy_json: dict[str, str]) -> str:
host = addr
break
if not host:
raise ClanError(
f"""
msg = f"""
Could not reach any of the host addresses provided in the json string.
Please doublecheck if they are reachable from your machine.
Try `ping [ADDR]` with one of the addresses: {deploy_json['addrs']}
"""
)
raise ClanError(msg)
return host

View File

@@ -44,7 +44,8 @@ def get_inventory_machine_details(
inventory = load_inventory_eval(flake_url)
machine = inventory.machines.get(machine_name)
if machine is None:
raise ClanError(f"Machine {machine_name} not found in inventory")
msg = f"Machine {machine_name} not found in inventory"
raise ClanError(msg)
hw_config_path = (
Path(flake_url) / "machines" / Path(machine_name) / "hardware-configuration.nix"
@@ -73,7 +74,8 @@ def list_nixos_machines(flake_url: str | Path) -> list[str]:
data = json.loads(res)
return data
except json.JSONDecodeError as e:
raise ClanError(f"Error decoding machines from flake: {e}") from e
msg = f"Error decoding machines from flake: {e}"
raise ClanError(msg) from e
@dataclass
@@ -88,12 +90,14 @@ def check_machine_online(
) -> Literal["Online", "Offline"]:
machine = load_inventory_eval(flake_url).machines.get(machine_name)
if not machine:
raise ClanError(f"Machine {machine_name} not found in inventory")
msg = f"Machine {machine_name} not found in inventory"
raise ClanError(msg)
hostname = machine.deploy.targetHost
if not hostname:
raise ClanError(f"Machine {machine_name} does not specify a targetHost")
msg = f"Machine {machine_name} does not specify a targetHost"
raise ClanError(msg)
timeout = opts.timeout if opts and opts.timeout else 20

View File

@@ -102,7 +102,8 @@ class Machine:
elif self.flake.is_remote():
return Path(nix_metadata(self.flake.url)["path"])
else:
raise ClanError(f"Unsupported flake url: {self.flake}")
msg = f"Unsupported flake url: {self.flake}"
raise ClanError(msg)
@property
def target_host(self) -> Host:
@@ -210,7 +211,8 @@ class Machine:
outpath = run_no_stdout(nix_build(args)).stdout.strip()
return Path(outpath)
else:
raise ValueError(f"Unknown method {method}")
msg = f"Unknown method {method}"
raise ValueError(msg)
def eval_nix(
self,
@@ -234,7 +236,8 @@ class Machine:
self._eval_cache[attr] = output
return output
else:
raise ClanError("eval_nix returned not a string")
msg = "eval_nix returned not a string"
raise ClanError(msg)
def build_nix(
self,
@@ -259,4 +262,5 @@ class Machine:
self._build_cache[attr] = output
return output
else:
raise ClanError("build_nix returned not a Path")
msg = "build_nix returned not a Path"
raise ClanError(msg)

View File

@@ -12,11 +12,9 @@ def validate_hostname(hostname: str) -> bool:
def machine_name_type(arg_value: str) -> str:
if len(arg_value) > 63:
raise argparse.ArgumentTypeError(
"Machine name must be less than 63 characters long"
)
msg = "Machine name must be less than 63 characters long"
raise argparse.ArgumentTypeError(msg)
if not VALID_HOSTNAME.match(arg_value):
raise argparse.ArgumentTypeError(
"Invalid character in machine name. Allowed characters are a-z, 0-9, ., and -. Must not start with a number"
)
msg = "Invalid character in machine name. Allowed characters are a-z, 0-9, ., and -. Must not start with a number"
raise argparse.ArgumentTypeError(msg)
return arg_value

View File

@@ -80,9 +80,8 @@ def upload_sources(
try:
return json.loads(proc.stdout)["path"]
except (json.JSONDecodeError, OSError) as e:
raise ClanError(
f"failed to parse output of {shlex.join(cmd)}: {e}\nGot: {proc.stdout}"
) from e
msg = f"failed to parse output of {shlex.join(cmd)}: {e}\nGot: {proc.stdout}"
raise ClanError(msg) from e
@API.register
@@ -96,7 +95,8 @@ def update_machines(base_path: str, machines: list[InventoryMachine]) -> None:
flake=FlakeId(base_path),
)
if not machine.deploy.targetHost:
raise ClanError(f"'TargetHost' is not set for machine '{machine.name}'")
msg = f"'TargetHost' is not set for machine '{machine.name}'"
raise ClanError(msg)
# Copy targetHost to machine
m.target_host_address = machine.deploy.targetHost
group_machines.append(m)
@@ -161,7 +161,8 @@ def deploy_machine(machines: MachineGroup) -> None:
def update(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
msg = "Could not find clan flake toplevel directory"
raise ClanError(msg)
machines = []
if len(args.machines) == 1 and args.target_host is not None:
machine = Machine(