clan-cli: Rename Host -> Remote move to clan_lib and mark as frozen
This commit is contained in:
@@ -6,13 +6,14 @@ from clan_lib.errors import ClanError
|
||||
def create_backup(machine: Machine, provider: str | None = None) -> None:
|
||||
machine.info(f"creating backup for {machine.name}")
|
||||
backup_scripts = machine.eval_nix("config.clan.core.backups")
|
||||
host = machine.target_host()
|
||||
if provider is None:
|
||||
if not backup_scripts["providers"]:
|
||||
msg = "No providers specified"
|
||||
raise ClanError(msg)
|
||||
with machine.target_host() as host:
|
||||
with host.ssh_control_master() as ssh:
|
||||
for provider in backup_scripts["providers"]:
|
||||
proc = host.run(
|
||||
proc = ssh.run(
|
||||
[backup_scripts["providers"][provider]["create"]],
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
@@ -23,8 +24,8 @@ def create_backup(machine: Machine, provider: str | None = None) -> None:
|
||||
if provider not in backup_scripts["providers"]:
|
||||
msg = f"provider {provider} not found"
|
||||
raise ClanError(msg)
|
||||
with machine.target_host() as host:
|
||||
proc = host.run(
|
||||
with host.ssh_control_master() as ssh:
|
||||
proc = ssh.run(
|
||||
[backup_scripts["providers"][provider]["create"]],
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
|
||||
@@ -2,10 +2,10 @@ import json
|
||||
from dataclasses import dataclass
|
||||
|
||||
from clan_cli.machines.machines import Machine
|
||||
from clan_cli.ssh.host import Host
|
||||
|
||||
from clan_lib.cmd import Log, RunOpts
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.ssh.remote import Remote
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -14,14 +14,15 @@ class Backup:
|
||||
job_name: str | None = None
|
||||
|
||||
|
||||
def list_provider(machine: Machine, host: Host, provider: str) -> list[Backup]:
|
||||
def list_provider(machine: Machine, host: Remote, provider: str) -> list[Backup]:
|
||||
results = []
|
||||
backup_metadata = machine.eval_nix("config.clan.core.backups")
|
||||
list_command = backup_metadata["providers"][provider]["list"]
|
||||
proc = host.run(
|
||||
[list_command],
|
||||
RunOpts(log=Log.NONE, check=False),
|
||||
)
|
||||
with host.ssh_control_master() as ssh:
|
||||
proc = ssh.run(
|
||||
[list_command],
|
||||
RunOpts(log=Log.NONE, check=False),
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
# TODO this should be a warning, only raise exception if no providers succeed
|
||||
msg = f"Failed to list backups for provider {provider}:"
|
||||
@@ -44,12 +45,12 @@ def list_provider(machine: Machine, host: Host, provider: str) -> list[Backup]:
|
||||
def list_backups(machine: Machine, provider: str | None = None) -> list[Backup]:
|
||||
backup_metadata = machine.eval_nix("config.clan.core.backups")
|
||||
results = []
|
||||
with machine.target_host() as host:
|
||||
if provider is None:
|
||||
for _provider in backup_metadata["providers"]:
|
||||
results += list_provider(machine, host, _provider)
|
||||
host = machine.target_host()
|
||||
if provider is None:
|
||||
for _provider in backup_metadata["providers"]:
|
||||
results += list_provider(machine, host, _provider)
|
||||
|
||||
else:
|
||||
results += list_provider(machine, host, provider)
|
||||
else:
|
||||
results += list_provider(machine, host, provider)
|
||||
|
||||
return results
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from clan_cli.machines.machines import Machine
|
||||
from clan_cli.ssh.host import Host
|
||||
|
||||
from clan_lib.cmd import Log, RunOpts
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.ssh.remote import Remote
|
||||
|
||||
|
||||
def restore_service(
|
||||
machine: Machine, host: Host, name: str, provider: str, service: str
|
||||
machine: Machine, host: Remote, name: str, provider: str, service: str
|
||||
) -> None:
|
||||
backup_metadata = machine.eval_nix("config.clan.core.backups")
|
||||
backup_folders = machine.eval_nix("config.clan.core.state")
|
||||
@@ -21,34 +21,35 @@ def restore_service(
|
||||
# FIXME: If we have too many folder this might overflow the stack.
|
||||
env["FOLDERS"] = ":".join(set(folders))
|
||||
|
||||
if pre_restore := backup_folders[service]["preRestoreCommand"]:
|
||||
proc = host.run(
|
||||
[pre_restore],
|
||||
with host.ssh_control_master() as ssh:
|
||||
if pre_restore := backup_folders[service]["preRestoreCommand"]:
|
||||
proc = ssh.run(
|
||||
[pre_restore],
|
||||
RunOpts(log=Log.STDERR),
|
||||
extra_env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
msg = f"failed to run preRestoreCommand: {pre_restore}, error was: {proc.stdout}"
|
||||
raise ClanError(msg)
|
||||
|
||||
proc = ssh.run(
|
||||
[backup_metadata["providers"][provider]["restore"]],
|
||||
RunOpts(log=Log.STDERR),
|
||||
extra_env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
msg = f"failed to run preRestoreCommand: {pre_restore}, error was: {proc.stdout}"
|
||||
msg = f"failed to restore backup: {backup_metadata['providers'][provider]['restore']}"
|
||||
raise ClanError(msg)
|
||||
|
||||
proc = host.run(
|
||||
[backup_metadata["providers"][provider]["restore"]],
|
||||
RunOpts(log=Log.STDERR),
|
||||
extra_env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
msg = f"failed to restore backup: {backup_metadata['providers'][provider]['restore']}"
|
||||
raise ClanError(msg)
|
||||
|
||||
if post_restore := backup_folders[service]["postRestoreCommand"]:
|
||||
proc = host.run(
|
||||
[post_restore],
|
||||
RunOpts(log=Log.STDERR),
|
||||
extra_env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
msg = f"failed to run postRestoreCommand: {post_restore}, error was: {proc.stdout}"
|
||||
raise ClanError(msg)
|
||||
if post_restore := backup_folders[service]["postRestoreCommand"]:
|
||||
proc = ssh.run(
|
||||
[post_restore],
|
||||
RunOpts(log=Log.STDERR),
|
||||
extra_env=env,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
msg = f"failed to run postRestoreCommand: {post_restore}, error was: {proc.stdout}"
|
||||
raise ClanError(msg)
|
||||
|
||||
|
||||
def restore_backup(
|
||||
@@ -58,7 +59,8 @@ def restore_backup(
|
||||
service: str | None = None,
|
||||
) -> None:
|
||||
errors = []
|
||||
with machine.target_host() as host:
|
||||
host = machine.target_host()
|
||||
with host.ssh_control_master():
|
||||
if service is None:
|
||||
backup_folders = machine.eval_nix("config.clan.core.state")
|
||||
for _service in backup_folders:
|
||||
|
||||
Reference in New Issue
Block a user