add dummy backups cli

This commit is contained in:
lassulus
2023-11-28 13:23:48 +01:00
parent 640430075a
commit 4ace326aeb
5 changed files with 148 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ from pathlib import Path
from types import ModuleType
from typing import Any, Optional, Sequence
from . import config, flakes, machines, secrets, vms, webui
from . import config, flakes, machines, secrets, vms, webui, backups
from .custom_logger import setup_logging
from .dirs import get_clan_flake_toplevel
from .ssh import cli as ssh_cli
@@ -64,6 +64,11 @@ def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser:
subparsers = parser.add_subparsers()
parser_backups = subparsers.add_parser(
"backups", help="manage backups of clan machines"
)
backups.register_parser(parser_backups)
parser_flake = subparsers.add_parser(
"flakes", help="create a clan flake inside the current directory"
)

View File

@@ -0,0 +1,25 @@
# !/usr/bin/env python3
import argparse
from .list import register_list_parser
from .create import register_create_parser
from .restore import register_restore_parser
# takes a (sub)parser and configures it
def register_parser(parser: argparse.ArgumentParser) -> None:
subparser = parser.add_subparsers(
title="command",
description="the command to run",
help="the command to run",
required=True,
)
list_parser = subparser.add_parser("list", help="list backups")
register_list_parser(list_parser)
create_parser = subparser.add_parser("create", help="create backups")
register_create_parser(create_parser)
restore_parser = subparser.add_parser("restore", help="restore backups")
register_restore_parser(restore_parser)

View File

@@ -0,0 +1,34 @@
import argparse
import pprint
from pathlib import Path
from typing import Optional
from ..errors import ClanError
def create_backup(flake_dir: Path, machine: Optional[str] = None, provider: Optional[str] = None) -> None:
if machine is None:
# TODO get all machines here
machines = [ "machine1", "machine2" ]
else:
machines = [ machine ]
if provider is None:
# TODO get all providers here
providers = [ "provider1", "provider2" ]
else:
providers = [ provider ]
print("would create backups for machines: ", machines, " with providers: ", providers)
def create_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
create_backup(Path(args.flake), machine=args.machine, provider=args.provider)
def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--machine", type=str, help="machine in the flake to create backups of")
parser.add_argument("--provider", type=str, help="backup provider to use")
parser.set_defaults(func=create_command)

View File

@@ -0,0 +1,56 @@
import argparse
import pprint
from pathlib import Path
from typing import Optional
from ..errors import ClanError
def list_backups(flake_dir: Path, machine: Optional[str] = None, provider: Optional[str] = None) -> dict[str, dict[str, list[str]]]:
dummy_data = {
"testhostname": {
"borgbackup": [
"2021-01-01T00:00:00Z",
"2022-01-01T00:00:00Z",
"2023-01-01T00:00:00Z",
],
"restic" : [
"2021-01-01T00:00:00Z",
"2022-01-01T00:00:00Z",
"2023-01-01T00:00:00Z",
],
},
"another host": {
"borgbackup": [
"2021-01-01T00:00:00Z",
"2022-01-01T00:00:00Z",
"2023-01-01T00:00:00Z",
],
},
}
if provider is not None:
new_data = {}
for machine_ in dummy_data:
if provider in dummy_data[machine_]:
new_data[machine_] = {provider: dummy_data[machine_][provider]}
dummy_data = new_data
if machine is None:
return dummy_data
else:
return {machine: dummy_data[machine]}
def list_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
backups = list_backups(Path(args.flake), machine=args.machine, provider=args.provider)
if len(backups) > 0:
pp = pprint.PrettyPrinter(depth=4)
pp.pprint(backups)
def register_list_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--machine", type=str, help="machine in the flake to show backups of")
parser.add_argument("--provider", type=str, help="backup provider to filter by")
parser.set_defaults(func=list_command)

View File

@@ -0,0 +1,27 @@
import argparse
import pprint
from pathlib import Path
from typing import Optional
from ..errors import ClanError
def restore_backup(flake_dir: Path, machine: str, provider: str, backup_id: str, service: Optional[str] = None) -> None:
if service is None:
print("would restore backup", machine, provider, backup_id)
else:
print("would restore backup", machine, provider, backup_id, "of service:", service)
def restore_command(args: argparse.Namespace) -> None:
if args.flake is None:
raise ClanError("Could not find clan flake toplevel directory")
restore_backup(Path(args.flake), machine=args.machine, provider=args.provider, backup_id=args.backup_id, service=args.service)
def register_restore_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("machine", type=str, help="machine in the flake to create backups of")
parser.add_argument("provider", type=str, help="backup provider to use")
parser.add_argument("backup_id", type=str, help="id of the backup to restore")
parser.add_argument("--service", type=str, help="name of the service to restore")
parser.set_defaults(func=restore_command)