clan-cli Machine: make deployment info lazy
This commit is contained in:
@@ -1,17 +1,20 @@
|
|||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from ..cmd import run
|
from ..cmd import run
|
||||||
from ..nix import nix_build, nix_config, nix_eval
|
from ..nix import nix_build, nix_config, nix_eval
|
||||||
from ..ssh import Host, parse_deployment_address
|
from ..ssh import Host, parse_deployment_address
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Machine:
|
class Machine:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
flake: Path | str,
|
flake: Path | str,
|
||||||
machine_data: dict | None = None,
|
deployment_info: dict | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Creates a Machine
|
Creates a Machine
|
||||||
@@ -25,21 +28,45 @@ class Machine:
|
|||||||
self.eval_cache: dict[str, str] = {}
|
self.eval_cache: dict[str, str] = {}
|
||||||
self.build_cache: dict[str, Path] = {}
|
self.build_cache: dict[str, Path] = {}
|
||||||
|
|
||||||
# TODO do this lazily
|
if deployment_info is not None:
|
||||||
if machine_data is None:
|
self.deployment_info = deployment_info
|
||||||
self.machine_data = json.loads(
|
|
||||||
self.build_nix("config.system.clan.deployment.file").read_text()
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self.machine_data = machine_data
|
|
||||||
|
|
||||||
self.deployment_address = self.machine_data["deploymentAddress"]
|
def get_deployment_info(self) -> None:
|
||||||
self.secrets_module = self.machine_data.get("secretsModule", None)
|
self.deployment_info = json.loads(
|
||||||
if "secretsData" in self.machine_data:
|
self.build_nix("config.system.clan.deployment.file").read_text()
|
||||||
self.secrets_data = json.loads(
|
)
|
||||||
Path(self.machine_data["secretsData"]).read_text()
|
|
||||||
)
|
@property
|
||||||
self.secrets_upload_directory = self.machine_data["secretsUploadDirectory"]
|
def deployment_address(self) -> str:
|
||||||
|
if not hasattr(self, "deployment_info"):
|
||||||
|
self.get_deployment_info()
|
||||||
|
return self.deployment_info["deploymentAddress"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def secrets_module(self) -> str:
|
||||||
|
if not hasattr(self, "deployment_info"):
|
||||||
|
self.get_deployment_info()
|
||||||
|
return self.deployment_info["secretsModule"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def secrets_data(self) -> dict:
|
||||||
|
if not hasattr(self, "deployment_info"):
|
||||||
|
self.get_deployment_info()
|
||||||
|
if self.deployment_info["secretsData"]:
|
||||||
|
try:
|
||||||
|
return json.loads(Path(self.deployment_info["secretsData"]).read_text())
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
log.error(
|
||||||
|
f"Failed to parse secretsData for machine {self.name} as json"
|
||||||
|
)
|
||||||
|
return {}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def secrets_upload_directory(self) -> str:
|
||||||
|
if not hasattr(self, "deployment_info"):
|
||||||
|
self.get_deployment_info()
|
||||||
|
return self.deployment_info["secretsUploadDirectory"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def flake_dir(self) -> Path:
|
def flake_dir(self) -> Path:
|
||||||
@@ -64,28 +91,24 @@ class Machine:
|
|||||||
eval a nix attribute of the machine
|
eval a nix attribute of the machine
|
||||||
@attr: the attribute to get
|
@attr: the attribute to get
|
||||||
"""
|
"""
|
||||||
if attr in self.eval_cache and not refresh:
|
|
||||||
return self.eval_cache[attr]
|
|
||||||
|
|
||||||
config = nix_config()
|
config = nix_config()
|
||||||
system = config["system"]
|
system = config["system"]
|
||||||
|
|
||||||
|
attr = f'clanInternals.machines."{system}".{self.name}.{attr}'
|
||||||
|
|
||||||
|
if attr in self.eval_cache and not refresh:
|
||||||
|
return self.eval_cache[attr]
|
||||||
|
|
||||||
if isinstance(self.flake, Path):
|
if isinstance(self.flake, Path):
|
||||||
output = run(
|
if (self.flake / ".git").exists():
|
||||||
nix_eval(
|
flake = f"git+file://{self.flake}"
|
||||||
[
|
else:
|
||||||
f'path:{self.flake}#clanInternals.machines."{system}"."{self.name}".{attr}'
|
flake = f"path:{self.flake}"
|
||||||
]
|
|
||||||
),
|
|
||||||
).stdout.strip()
|
|
||||||
else:
|
else:
|
||||||
output = run(
|
flake = self.flake
|
||||||
nix_eval(
|
|
||||||
[
|
log.info(f"evaluating {flake}#{attr}")
|
||||||
f'{self.flake}#clanInternals.machines."{system}"."{self.name}".{attr}'
|
output = run(nix_eval([f"{flake}#{attr}"])).stdout.strip()
|
||||||
]
|
|
||||||
),
|
|
||||||
).stdout.strip()
|
|
||||||
self.eval_cache[attr] = output
|
self.eval_cache[attr] = output
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -94,27 +117,21 @@ class Machine:
|
|||||||
build a nix attribute of the machine
|
build a nix attribute of the machine
|
||||||
@attr: the attribute to get
|
@attr: the attribute to get
|
||||||
"""
|
"""
|
||||||
if attr in self.build_cache and not refresh:
|
|
||||||
return self.build_cache[attr]
|
|
||||||
|
|
||||||
config = nix_config()
|
config = nix_config()
|
||||||
system = config["system"]
|
system = config["system"]
|
||||||
|
|
||||||
|
attr = f'clanInternals.machines."{system}".{self.name}.{attr}'
|
||||||
|
|
||||||
|
if attr in self.build_cache and not refresh:
|
||||||
|
return self.build_cache[attr]
|
||||||
|
|
||||||
if isinstance(self.flake, Path):
|
if isinstance(self.flake, Path):
|
||||||
outpath = run(
|
flake = f"path:{self.flake}"
|
||||||
nix_build(
|
|
||||||
[
|
|
||||||
f'path:{self.flake}#clanInternals.machines."{system}"."{self.name}".{attr}'
|
|
||||||
]
|
|
||||||
),
|
|
||||||
).stdout.strip()
|
|
||||||
else:
|
else:
|
||||||
outpath = run(
|
flake = self.flake
|
||||||
nix_build(
|
|
||||||
[
|
log.info(f"building {flake}#{attr}")
|
||||||
f'{self.flake}#clanInternals.machines."{system}"."{self.name}".{attr}'
|
outpath = run(nix_build([f"{flake}#{attr}"])).stdout.strip()
|
||||||
]
|
|
||||||
),
|
|
||||||
).stdout.strip()
|
|
||||||
self.build_cache[attr] = Path(outpath)
|
self.build_cache[attr] = Path(outpath)
|
||||||
return Path(outpath)
|
return Path(outpath)
|
||||||
|
|||||||
Reference in New Issue
Block a user