add machine subcommand
This commit is contained in:
parent
be78e65b11
commit
c5b16124ef
@ -1,6 +1,9 @@
|
|||||||
# !/usr/bin/env python3
|
# !/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
from .create import register_create_parser
|
||||||
|
from .delete import register_delete_parser
|
||||||
|
from .list import register_list_parser
|
||||||
from .update import register_update_parser
|
from .update import register_update_parser
|
||||||
|
|
||||||
|
|
||||||
@ -13,5 +16,14 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
required=True,
|
required=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
groups_parser = subparser.add_parser("update", help="Update a machine")
|
update_parser = subparser.add_parser("update", help="Update a machine")
|
||||||
register_update_parser(groups_parser)
|
register_update_parser(update_parser)
|
||||||
|
|
||||||
|
create_parser = subparser.add_parser("create", help="Create a machine")
|
||||||
|
register_create_parser(create_parser)
|
||||||
|
|
||||||
|
delete_parser = subparser.add_parser("delete", help="Delete a machine")
|
||||||
|
register_delete_parser(delete_parser)
|
||||||
|
|
||||||
|
list_parser = subparser.add_parser("list", help="List machines")
|
||||||
|
register_list_parser(list_parser)
|
||||||
|
13
pkgs/clan-cli/clan_cli/machines/create.py
Normal file
13
pkgs/clan-cli/clan_cli/machines/create.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import argparse
|
||||||
|
|
||||||
|
from .folders import machine_folder
|
||||||
|
|
||||||
|
|
||||||
|
def create_command(args: argparse.Namespace) -> None:
|
||||||
|
folder = machine_folder(args.host)
|
||||||
|
folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
parser.add_argument("host", type=str)
|
||||||
|
parser.set_defaults(func=create_command)
|
17
pkgs/clan-cli/clan_cli/machines/delete.py
Normal file
17
pkgs/clan-cli/clan_cli/machines/delete.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import argparse
|
||||||
|
|
||||||
|
from ..errors import ClanError
|
||||||
|
from .folders import machine_folder
|
||||||
|
|
||||||
|
|
||||||
|
def delete_command(args: argparse.Namespace) -> None:
|
||||||
|
folder = machine_folder(args.host)
|
||||||
|
if folder.exists():
|
||||||
|
folder.rmdir()
|
||||||
|
else:
|
||||||
|
raise ClanError(f"Machine {args.host} does not exist")
|
||||||
|
|
||||||
|
|
||||||
|
def register_delete_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
parser.add_argument("host", type=str)
|
||||||
|
parser.set_defaults(func=delete_command)
|
11
pkgs/clan-cli/clan_cli/machines/folders.py
Normal file
11
pkgs/clan-cli/clan_cli/machines/folders.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from ..dirs import get_clan_flake_toplevel
|
||||||
|
|
||||||
|
|
||||||
|
def machines_folder() -> Path:
|
||||||
|
return get_clan_flake_toplevel() / "machines"
|
||||||
|
|
||||||
|
|
||||||
|
def machine_folder(machine: str) -> Path:
|
||||||
|
return machines_folder() / machine
|
25
pkgs/clan-cli/clan_cli/machines/list.py
Normal file
25
pkgs/clan-cli/clan_cli/machines/list.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
from .folders import machines_folder
|
||||||
|
from .types import validate_hostname
|
||||||
|
|
||||||
|
|
||||||
|
def list_machines() -> list[str]:
|
||||||
|
path = machines_folder()
|
||||||
|
if not path.exists():
|
||||||
|
return []
|
||||||
|
objs: list[str] = []
|
||||||
|
for f in os.listdir(path):
|
||||||
|
if validate_hostname(f):
|
||||||
|
objs.append(f)
|
||||||
|
return objs
|
||||||
|
|
||||||
|
|
||||||
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
|
for machine in list_machines():
|
||||||
|
print(machine)
|
||||||
|
|
||||||
|
|
||||||
|
def register_list_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
parser.set_defaults(func=list_command)
|
22
pkgs/clan-cli/clan_cli/machines/types.py
Normal file
22
pkgs/clan-cli/clan_cli/machines/types.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import argparse
|
||||||
|
import re
|
||||||
|
|
||||||
|
VALID_HOSTNAME = re.compile(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", re.IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_hostname(hostname: str) -> bool:
|
||||||
|
if len(hostname) > 63:
|
||||||
|
return False
|
||||||
|
return VALID_HOSTNAME.match(hostname) is not None
|
||||||
|
|
||||||
|
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
return arg_value
|
@ -3,15 +3,14 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
|
from ..machines.types import machine_name_type, validate_hostname
|
||||||
from . import secrets
|
from . import secrets
|
||||||
from .folders import sops_groups_folder, sops_machines_folder, sops_users_folder
|
from .folders import sops_groups_folder, sops_machines_folder, sops_users_folder
|
||||||
from .types import (
|
from .types import (
|
||||||
VALID_USER_NAME,
|
VALID_USER_NAME,
|
||||||
group_name_type,
|
group_name_type,
|
||||||
machine_name_type,
|
|
||||||
secret_name_type,
|
secret_name_type,
|
||||||
user_name_type,
|
user_name_type,
|
||||||
validate_hostname,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
from ..machines.types import machine_name_type, validate_hostname
|
||||||
from . import secrets
|
from . import secrets
|
||||||
from .folders import list_objects, remove_object, sops_machines_folder
|
from .folders import list_objects, remove_object, sops_machines_folder
|
||||||
from .sops import write_key
|
from .sops import write_key
|
||||||
from .types import (
|
from .types import (
|
||||||
machine_name_type,
|
|
||||||
public_or_private_age_key_type,
|
public_or_private_age_key_type,
|
||||||
secret_name_type,
|
secret_name_type,
|
||||||
validate_hostname,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,13 +9,6 @@ from .sops import get_public_key
|
|||||||
|
|
||||||
VALID_SECRET_NAME = re.compile(r"^[a-zA-Z0-9._-]+$")
|
VALID_SECRET_NAME = re.compile(r"^[a-zA-Z0-9._-]+$")
|
||||||
VALID_USER_NAME = re.compile(r"^[a-z_]([a-z0-9_-]{0,31})?$")
|
VALID_USER_NAME = re.compile(r"^[a-z_]([a-z0-9_-]{0,31})?$")
|
||||||
VALID_HOSTNAME = re.compile(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", re.IGNORECASE)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_hostname(hostname: str) -> bool:
|
|
||||||
if len(hostname) > 63:
|
|
||||||
return False
|
|
||||||
return VALID_HOSTNAME.match(hostname) is not None
|
|
||||||
|
|
||||||
|
|
||||||
def secret_name_type(arg_value: str) -> str:
|
def secret_name_type(arg_value: str) -> str:
|
||||||
@ -26,18 +19,6 @@ def secret_name_type(arg_value: str) -> str:
|
|||||||
return arg_value
|
return arg_value
|
||||||
|
|
||||||
|
|
||||||
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"
|
|
||||||
)
|
|
||||||
if not VALID_SECRET_NAME.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"
|
|
||||||
)
|
|
||||||
return arg_value
|
|
||||||
|
|
||||||
|
|
||||||
def public_or_private_age_key_type(arg_value: str) -> str:
|
def public_or_private_age_key_type(arg_value: str) -> str:
|
||||||
if os.path.isfile(arg_value):
|
if os.path.isfile(arg_value):
|
||||||
arg_value = Path(arg_value).read_text().strip()
|
arg_value = Path(arg_value).read_text().strip()
|
||||||
|
Loading…
Reference in New Issue
Block a user