generated from Luis/nextjs-python-web-template
factor out deployment address parsing into a function
This commit is contained in:
@@ -2,13 +2,12 @@ import argparse
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from ..dirs import get_clan_flake_toplevel
|
from ..dirs import get_clan_flake_toplevel
|
||||||
from ..nix import nix_command, nix_eval
|
from ..nix import nix_command, nix_eval
|
||||||
from ..secrets.generate import generate_secrets
|
from ..secrets.generate import generate_secrets
|
||||||
from ..secrets.upload import upload_secrets
|
from ..secrets.upload import upload_secrets
|
||||||
from ..ssh import Host, HostGroup, HostKeyCheck
|
from ..ssh import Host, HostGroup, HostKeyCheck, parse_deployment_address
|
||||||
|
|
||||||
|
|
||||||
def deploy_nixos(hosts: HostGroup) -> None:
|
def deploy_nixos(hosts: HostGroup) -> None:
|
||||||
@@ -78,11 +77,12 @@ def deploy_nixos(hosts: HostGroup) -> None:
|
|||||||
# FIXME: we want some kind of inventory here.
|
# FIXME: we want some kind of inventory here.
|
||||||
def update(args: argparse.Namespace) -> None:
|
def update(args: argparse.Namespace) -> None:
|
||||||
clan_dir = get_clan_flake_toplevel().as_posix()
|
clan_dir = get_clan_flake_toplevel().as_posix()
|
||||||
host = json.loads(
|
machine = args.machine
|
||||||
|
address = json.loads(
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
nix_eval(
|
nix_eval(
|
||||||
[
|
[
|
||||||
f'{clan_dir}#nixosConfigurations."{args.machine}".config.clan.networking.deploymentAddress'
|
f'{clan_dir}#nixosConfigurations."{machine}".config.clan.networking.deploymentAddress'
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
@@ -90,31 +90,9 @@ def update(args: argparse.Namespace) -> None:
|
|||||||
text=True,
|
text=True,
|
||||||
).stdout
|
).stdout
|
||||||
)
|
)
|
||||||
parts = host.split("@")
|
host = parse_deployment_address(machine, address)
|
||||||
user: Optional[str] = None
|
print(f"deploying {machine}")
|
||||||
if len(parts) > 1:
|
deploy_nixos(HostGroup([host]))
|
||||||
user = parts[0]
|
|
||||||
hostname = parts[1]
|
|
||||||
else:
|
|
||||||
hostname = parts[0]
|
|
||||||
maybe_port = hostname.split(":")
|
|
||||||
port = None
|
|
||||||
if len(maybe_port) > 1:
|
|
||||||
hostname = maybe_port[0]
|
|
||||||
port = int(maybe_port[1])
|
|
||||||
print(f"deploying {host}")
|
|
||||||
deploy_nixos(
|
|
||||||
HostGroup(
|
|
||||||
[
|
|
||||||
Host(
|
|
||||||
host=hostname,
|
|
||||||
port=port,
|
|
||||||
user=user,
|
|
||||||
meta=dict(flake_attr=args.machine),
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def register_update_parser(parser: argparse.ArgumentParser) -> None:
|
def register_update_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
|||||||
@@ -756,6 +756,36 @@ class HostGroup:
|
|||||||
return HostGroup(list(filter(pred, self.hosts)))
|
return HostGroup(list(filter(pred, self.hosts)))
|
||||||
|
|
||||||
|
|
||||||
|
def parse_deployment_address(machine_name: str, host: str) -> Host:
|
||||||
|
parts = host.split("@")
|
||||||
|
user: Optional[str] = None
|
||||||
|
if len(parts) > 1:
|
||||||
|
user = parts[0]
|
||||||
|
hostname = parts[1]
|
||||||
|
else:
|
||||||
|
hostname = parts[0]
|
||||||
|
maybe_options = hostname.split("?")
|
||||||
|
options: Dict[str, str] = {}
|
||||||
|
if len(maybe_options) > 1:
|
||||||
|
hostname = maybe_options[0]
|
||||||
|
for option in maybe_options[1].split("&"):
|
||||||
|
k, v = option.split("=")
|
||||||
|
options[k] = v
|
||||||
|
maybe_port = hostname.split(":")
|
||||||
|
port = None
|
||||||
|
if len(maybe_port) > 1:
|
||||||
|
hostname = maybe_port[0]
|
||||||
|
port = int(maybe_port[1])
|
||||||
|
return Host(
|
||||||
|
hostname,
|
||||||
|
user=user,
|
||||||
|
port=port,
|
||||||
|
command_prefix=machine_name,
|
||||||
|
meta=dict(flake_attr=machine_name),
|
||||||
|
ssh_options=options,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def run(
|
def run(
|
||||||
cmd: Union[List[str], str],
|
cmd: Union[List[str], str],
|
||||||
|
|||||||
Reference in New Issue
Block a user