2023-11-28 12:23:48 +00:00
|
|
|
import argparse
|
2023-12-04 16:05:37 +00:00
|
|
|
import json
|
2024-01-23 14:04:10 +00:00
|
|
|
import logging
|
2023-11-28 12:23:48 +00:00
|
|
|
|
2024-06-05 09:17:58 +00:00
|
|
|
from ..completions import (
|
|
|
|
add_dynamic_completer,
|
|
|
|
complete_backup_providers_for_machine,
|
|
|
|
complete_machines,
|
|
|
|
)
|
2023-11-28 12:23:48 +00:00
|
|
|
from ..errors import ClanError
|
2023-12-04 16:05:37 +00:00
|
|
|
from ..machines.machines import Machine
|
2023-11-28 12:23:48 +00:00
|
|
|
|
2024-01-23 14:04:10 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2023-11-28 12:23:48 +00:00
|
|
|
|
2023-12-06 16:13:32 +00:00
|
|
|
def create_backup(machine: Machine, provider: str | None = None) -> None:
|
2024-01-23 14:04:10 +00:00
|
|
|
log.info(f"creating backup for {machine.name}")
|
2024-06-17 10:42:28 +00:00
|
|
|
backup_scripts = json.loads(machine.eval_nix("config.clan.core.backups"))
|
2023-11-28 12:23:48 +00:00
|
|
|
if provider is None:
|
2023-12-04 16:05:37 +00:00
|
|
|
for provider in backup_scripts["providers"]:
|
2024-02-06 14:55:34 +00:00
|
|
|
proc = machine.target_host.run(
|
2024-03-20 06:28:02 +00:00
|
|
|
[backup_scripts["providers"][provider]["create"]],
|
2023-12-04 16:05:37 +00:00
|
|
|
)
|
|
|
|
if proc.returncode != 0:
|
|
|
|
raise ClanError("failed to start backup")
|
|
|
|
else:
|
|
|
|
print("successfully started backup")
|
2023-11-28 12:23:48 +00:00
|
|
|
else:
|
2023-12-04 16:05:37 +00:00
|
|
|
if provider not in backup_scripts["providers"]:
|
|
|
|
raise ClanError(f"provider {provider} not found")
|
2024-02-06 14:55:34 +00:00
|
|
|
proc = machine.target_host.run(
|
2024-03-20 06:28:02 +00:00
|
|
|
[backup_scripts["providers"][provider]["create"]],
|
2023-12-04 16:05:37 +00:00
|
|
|
)
|
|
|
|
if proc.returncode != 0:
|
|
|
|
raise ClanError("failed to start backup")
|
|
|
|
else:
|
|
|
|
print("successfully started backup")
|
2023-11-28 12:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_command(args: argparse.Namespace) -> None:
|
2024-05-29 07:46:23 +00:00
|
|
|
if args.flake is None:
|
|
|
|
raise ClanError("Could not find clan flake toplevel directory")
|
2024-01-19 13:37:30 +00:00
|
|
|
machine = Machine(name=args.machine, flake=args.flake)
|
2023-12-04 16:05:37 +00:00
|
|
|
create_backup(machine=machine, provider=args.provider)
|
2023-11-28 12:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
2024-05-30 17:51:53 +00:00
|
|
|
machines_parser = parser.add_argument(
|
2023-12-04 16:05:37 +00:00
|
|
|
"machine", type=str, help="machine in the flake to create backups of"
|
|
|
|
)
|
2024-05-30 17:51:53 +00:00
|
|
|
add_dynamic_completer(machines_parser, complete_machines)
|
|
|
|
|
2024-06-05 09:17:58 +00:00
|
|
|
provider_action = parser.add_argument(
|
|
|
|
"--provider", type=str, help="backup provider to use"
|
|
|
|
)
|
|
|
|
add_dynamic_completer(provider_action, complete_backup_providers_for_machine)
|
2023-11-28 12:23:48 +00:00
|
|
|
parser.set_defaults(func=create_command)
|