clan-core/pkgs/clan-cli/clan_cli/backups/restore.py

110 lines
3.6 KiB
Python
Raw Normal View History

2023-11-28 12:23:48 +00:00
import argparse
import json
import subprocess
2023-11-28 12:23:48 +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
from ..machines.machines import Machine
2023-11-28 12:23:48 +00:00
def restore_service(machine: Machine, name: str, provider: str, service: str) -> None:
2024-06-17 10:42:28 +00:00
backup_metadata = json.loads(machine.eval_nix("config.clan.core.backups"))
backup_folders = json.loads(machine.eval_nix("config.clan.core.state"))
2024-06-05 16:37:31 +00:00
if service not in backup_folders:
msg = f"Service {service} not found in configuration. Available services are: {', '.join(backup_folders.keys())}"
raise ClanError(msg)
2023-12-08 17:40:18 +00:00
folders = backup_folders[service]["folders"]
env = {}
env["NAME"] = name
2024-06-05 16:37:31 +00:00
# FIXME: If we have too many folder this might overflow the stack.
2024-05-31 14:36:37 +00:00
env["FOLDERS"] = ":".join(set(folders))
2023-12-08 17:40:18 +00:00
if pre_restore := backup_folders[service]["preRestoreCommand"]:
proc = machine.target_host.run(
[pre_restore],
stdout=subprocess.PIPE,
extra_env=env,
2023-12-08 17:40:18 +00:00
)
if proc.returncode != 0:
raise ClanError(
f"failed to run preRestoreCommand: {pre_restore}, error was: {proc.stdout}"
)
2023-12-08 17:40:18 +00:00
proc = machine.target_host.run(
[backup_metadata["providers"][provider]["restore"]],
2023-12-08 17:40:18 +00:00
stdout=subprocess.PIPE,
extra_env=env,
)
if proc.returncode != 0:
raise ClanError(
f"failed to restore backup: {backup_metadata['providers'][provider]['restore']}"
)
if post_restore := backup_folders[service]["postRestoreCommand"]:
proc = machine.target_host.run(
[post_restore],
stdout=subprocess.PIPE,
extra_env=env,
2023-12-08 17:40:18 +00:00
)
if proc.returncode != 0:
raise ClanError(
f"failed to run postRestoreCommand: {post_restore}, error was: {proc.stdout}"
)
2023-12-08 17:40:18 +00:00
def restore_backup(
machine: Machine,
provider: str,
2024-03-19 16:25:31 +00:00
name: str,
2023-12-08 17:40:18 +00:00
service: str | None = None,
) -> None:
2024-05-31 14:36:37 +00:00
errors = []
2023-11-28 12:23:48 +00:00
if service is None:
2024-06-17 10:42:28 +00:00
backup_folders = json.loads(machine.eval_nix("config.clan.core.state"))
for _service in backup_folders:
2024-05-31 14:36:37 +00:00
try:
restore_service(machine, name, provider, _service)
except ClanError as e:
errors.append(f"{_service}: {e}")
2023-11-28 12:23:48 +00:00
else:
2024-05-31 14:36:37 +00:00
try:
restore_service(machine, name, provider, service)
except ClanError as e:
errors.append(f"{service}: {e}")
if errors:
raise ClanError(
"Restore failed for the following services:\n" + "\n".join(errors)
)
2023-11-28 12:23:48 +00:00
def restore_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")
machine = Machine(name=args.machine, flake=args.flake)
2023-12-04 16:05:37 +00:00
restore_backup(
machine=machine,
2023-12-04 16:05:37 +00:00
provider=args.provider,
2024-03-19 16:25:31 +00:00
name=args.name,
2023-12-04 16:05:37 +00:00
service=args.service,
)
2023-11-28 12:23:48 +00:00
def register_restore_parser(parser: argparse.ArgumentParser) -> None:
machine_action = parser.add_argument(
2023-12-04 16:05:37 +00:00
"machine", type=str, help="machine in the flake to create backups of"
)
add_dynamic_completer(machine_action, complete_machines)
provider_action = parser.add_argument(
"provider", type=str, help="backup provider to use"
)
add_dynamic_completer(provider_action, complete_backup_providers_for_machine)
2024-03-19 16:25:31 +00:00
parser.add_argument("name", type=str, help="Name of the backup to restore")
2023-11-28 12:23:48 +00:00
parser.add_argument("--service", type=str, help="name of the service to restore")
parser.set_defaults(func=restore_command)