2023-09-14 11:49:20 +00:00
|
|
|
import argparse
|
2024-01-17 17:00:30 +00:00
|
|
|
import importlib
|
2023-10-03 10:50:47 +00:00
|
|
|
import logging
|
2023-09-28 12:13:23 +00:00
|
|
|
from pathlib import Path
|
2023-09-29 16:30:11 +00:00
|
|
|
from tempfile import TemporaryDirectory
|
2023-09-14 11:49:20 +00:00
|
|
|
|
2024-01-12 16:01:46 +00:00
|
|
|
from ..cmd import Log, run
|
2024-05-30 17:51:53 +00:00
|
|
|
from ..completions import add_dynamic_completer, complete_machines
|
2023-10-04 13:32:04 +00:00
|
|
|
from ..machines.machines import Machine
|
2023-10-04 19:29:19 +00:00
|
|
|
from ..nix import nix_shell
|
2023-09-14 11:49:20 +00:00
|
|
|
|
2023-10-03 10:50:47 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2023-09-14 11:49:20 +00:00
|
|
|
|
2023-10-04 13:32:04 +00:00
|
|
|
def upload_secrets(machine: Machine) -> None:
|
2024-03-23 04:05:31 +00:00
|
|
|
secret_facts_module = importlib.import_module(machine.secret_facts_module)
|
|
|
|
secret_facts_store = secret_facts_module.SecretStore(machine=machine)
|
2024-01-15 18:34:04 +00:00
|
|
|
|
2024-03-23 04:05:31 +00:00
|
|
|
if secret_facts_store.update_check():
|
2024-02-16 13:57:01 +00:00
|
|
|
log.info("Secrets already up to date")
|
|
|
|
return
|
2024-01-15 18:34:04 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2024-03-23 04:05:31 +00:00
|
|
|
secret_facts_store.upload(Path(tempdir))
|
2024-02-06 14:55:34 +00:00
|
|
|
host = machine.target_host
|
2024-01-15 18:34:04 +00:00
|
|
|
|
|
|
|
ssh_cmd = host.ssh_cmd()
|
|
|
|
run(
|
|
|
|
nix_shell(
|
|
|
|
["nixpkgs#rsync"],
|
|
|
|
[
|
|
|
|
"rsync",
|
|
|
|
"-e",
|
|
|
|
" ".join(["ssh"] + ssh_cmd[2:]),
|
|
|
|
"-az",
|
|
|
|
"--delete",
|
2024-06-02 17:10:48 +00:00
|
|
|
"--chown=root:root",
|
|
|
|
"--chmod=D700,F600",
|
2024-01-15 18:34:04 +00:00
|
|
|
f"{tempdir!s}/",
|
|
|
|
f"{host.user}@{host.host}:{machine.secrets_upload_directory}/",
|
|
|
|
],
|
|
|
|
),
|
|
|
|
log=Log.BOTH,
|
|
|
|
)
|
2023-09-14 11:49:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upload_command(args: argparse.Namespace) -> None:
|
2024-01-19 13:37:30 +00:00
|
|
|
machine = Machine(name=args.machine, flake=args.flake)
|
2023-10-04 13:32:04 +00:00
|
|
|
upload_secrets(machine)
|
2023-09-14 11:49:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register_upload_parser(parser: argparse.ArgumentParser) -> None:
|
2024-05-30 17:51:53 +00:00
|
|
|
machines_parser = parser.add_argument(
|
2023-09-14 11:49:20 +00:00
|
|
|
"machine",
|
|
|
|
help="The machine to upload secrets to",
|
|
|
|
)
|
2024-05-30 17:51:53 +00:00
|
|
|
add_dynamic_completer(machines_parser, complete_machines)
|
|
|
|
|
2023-09-14 11:49:20 +00:00
|
|
|
parser.set_defaults(func=upload_command)
|