create-host: add delete feature
This commit is contained in:
@@ -2,10 +2,138 @@
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
from models import HostConfig
|
||||
|
||||
|
||||
def remove_from_flake_nix(hostname: str, repo_root: Path) -> bool:
|
||||
"""
|
||||
Remove host entry from flake.nix nixosConfigurations.
|
||||
|
||||
Args:
|
||||
hostname: Hostname to remove
|
||||
repo_root: Path to repository root
|
||||
|
||||
Returns:
|
||||
True if found and removed, False if not found
|
||||
"""
|
||||
flake_path = repo_root / "flake.nix"
|
||||
content = flake_path.read_text()
|
||||
|
||||
# Check if hostname exists
|
||||
hostname_pattern = rf"^ {re.escape(hostname)} = nixpkgs\.lib\.nixosSystem"
|
||||
if not re.search(hostname_pattern, content, re.MULTILINE):
|
||||
return False
|
||||
|
||||
# Match the entire block from "hostname = " to "};"
|
||||
replace_pattern = rf"^ {re.escape(hostname)} = nixpkgs\.lib\.nixosSystem \{{.*?^ \}};\n"
|
||||
new_content, count = re.subn(replace_pattern, "", content, flags=re.MULTILINE | re.DOTALL)
|
||||
|
||||
if count == 0:
|
||||
return False
|
||||
|
||||
flake_path.write_text(new_content)
|
||||
return True
|
||||
|
||||
|
||||
def remove_from_terraform_vms(hostname: str, repo_root: Path) -> bool:
|
||||
"""
|
||||
Remove VM entry from terraform/vms.tf locals.vms map.
|
||||
|
||||
Args:
|
||||
hostname: Hostname to remove
|
||||
repo_root: Path to repository root
|
||||
|
||||
Returns:
|
||||
True if found and removed, False if not found
|
||||
"""
|
||||
terraform_path = repo_root / "terraform" / "vms.tf"
|
||||
content = terraform_path.read_text()
|
||||
|
||||
# Check if hostname exists
|
||||
hostname_pattern = rf'^\s+"{re.escape(hostname)}" = \{{'
|
||||
if not re.search(hostname_pattern, content, re.MULTILINE):
|
||||
return False
|
||||
|
||||
# Match the entire block from "hostname" = { to }
|
||||
replace_pattern = rf'^\s+"{re.escape(hostname)}" = \{{.*?^\s+\}}\n'
|
||||
new_content, count = re.subn(replace_pattern, "", content, flags=re.MULTILINE | re.DOTALL)
|
||||
|
||||
if count == 0:
|
||||
return False
|
||||
|
||||
terraform_path.write_text(new_content)
|
||||
return True
|
||||
|
||||
|
||||
def remove_from_vault_terraform(hostname: str, repo_root: Path) -> bool:
|
||||
"""
|
||||
Remove host policy from terraform/vault/hosts-generated.tf.
|
||||
|
||||
Args:
|
||||
hostname: Hostname to remove
|
||||
repo_root: Path to repository root
|
||||
|
||||
Returns:
|
||||
True if found and removed, False if not found
|
||||
"""
|
||||
vault_tf_path = repo_root / "terraform" / "vault" / "hosts-generated.tf"
|
||||
|
||||
if not vault_tf_path.exists():
|
||||
return False
|
||||
|
||||
content = vault_tf_path.read_text()
|
||||
|
||||
# Check if hostname exists in the policies
|
||||
if f'"{hostname}"' not in content:
|
||||
return False
|
||||
|
||||
# Match the host entry block within generated_host_policies
|
||||
# Pattern matches: "hostname" = { ... } with possible trailing newlines
|
||||
replace_pattern = rf'\s*"{re.escape(hostname)}" = \{{\s*paths = \[.*?\]\s*\}}\n?'
|
||||
new_content, count = re.subn(replace_pattern, "", content, flags=re.DOTALL)
|
||||
|
||||
if count == 0:
|
||||
return False
|
||||
|
||||
vault_tf_path.write_text(new_content)
|
||||
return True
|
||||
|
||||
|
||||
def check_entries_exist(hostname: str, repo_root: Path) -> Tuple[bool, bool, bool]:
|
||||
"""
|
||||
Check which entries exist for a hostname.
|
||||
|
||||
Args:
|
||||
hostname: Hostname to check
|
||||
repo_root: Path to repository root
|
||||
|
||||
Returns:
|
||||
Tuple of (flake_exists, terraform_vms_exists, vault_exists)
|
||||
"""
|
||||
# Check flake.nix
|
||||
flake_path = repo_root / "flake.nix"
|
||||
flake_content = flake_path.read_text()
|
||||
flake_pattern = rf"^ {re.escape(hostname)} = nixpkgs\.lib\.nixosSystem"
|
||||
flake_exists = bool(re.search(flake_pattern, flake_content, re.MULTILINE))
|
||||
|
||||
# Check terraform/vms.tf
|
||||
terraform_path = repo_root / "terraform" / "vms.tf"
|
||||
terraform_content = terraform_path.read_text()
|
||||
terraform_pattern = rf'^\s+"{re.escape(hostname)}" = \{{'
|
||||
terraform_exists = bool(re.search(terraform_pattern, terraform_content, re.MULTILINE))
|
||||
|
||||
# Check terraform/vault/hosts-generated.tf
|
||||
vault_tf_path = repo_root / "terraform" / "vault" / "hosts-generated.tf"
|
||||
vault_exists = False
|
||||
if vault_tf_path.exists():
|
||||
vault_content = vault_tf_path.read_text()
|
||||
vault_exists = f'"{hostname}"' in vault_content
|
||||
|
||||
return (flake_exists, terraform_exists, vault_exists)
|
||||
|
||||
|
||||
def update_flake_nix(config: HostConfig, repo_root: Path, force: bool = False) -> None:
|
||||
"""
|
||||
Add or update host entry in flake.nix nixosConfigurations.
|
||||
|
||||
Reference in New Issue
Block a user