-
Notifications
You must be signed in to change notification settings - Fork 20
/
clusterSnapshotter.py
145 lines (123 loc) · 5.55 KB
/
clusterSnapshotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from assistedInstaller import AssistedClientAutomation as ACA
from assistedInstallerService import AssistedInstallerService as AIS
from clustersConfig import ClustersConfig, NodeConfig
from clusterDeployer import ClusterDeployer
from clusterNode import VmClusterNode
import host
import os
from logger import logger
import coreosBuilder
from concurrent.futures import ThreadPoolExecutor
from nfs import NFS
def get_part_table(h: host.Host, drive: str) -> list[str]:
fdisk_output = h.run(f"sudo fdisk -l {drive}").out.strip().split()
parts = [x.split()[0] for x in fdisk_output if x.startswith(drive)]
parts = [x for x in parts if ":" not in x]
return parts
def fsarchiver() -> str:
url = "quay.io/bnemeth/fsarchiver"
return f"podman run --user 0 --privileged -v /mnt:/mnt -v /dev:/dev {url}"
class ClusterSnapshotter:
def __init__(self, cc: ClustersConfig, ais: AIS, ai: ACA, name: str):
self._ais = ais
self._ai = ai
self._cc = cc
self._name = name
def export_cluster(self) -> None:
self._cc.prepare_external_port()
lh = host.LocalHost()
lh.run(f"mkdir -p {self._snapshot_dir()}")
self._ais.export_snapshot(self._snapshot_dir())
def save_phys(node: str) -> None:
coreosBuilder.ensure_fcos_exists()
rh = host.RemoteHost(node)
nfs = NFS(host.LocalHost(), self._cc.external_port)
file = nfs.host_file("/root/iso/fedora-coreos.iso")
rh.boot_iso_redfish(file)
logger.info(f"Backing up node {node}")
rh.ssh_connect("core")
rh.run("sudo mount /dev/sdb1 /mnt/")
for e in get_part_table(rh, "/dev/sda"):
name = os.path.basename(e)
dest = f"/mnt/fsarchiver_backup.{name}.fsa"
dest_dd = f"/mnt/{name}.dd"
rh.run(f"sudo rm -f {dest} {dest_dd}")
ret = rh.run(f"sudo {fsarchiver()} savefs {dest} {e}")
logger.info(ret)
if ret.returncode != 0:
rh.run(f"sudo dd if={e} of={dest_dd}")
logger.info(f"Finished backing up node {node}")
rh.run("sudo systemctl reboot")
def save_vms() -> None:
lh = host.LocalHost()
vms = lh.run("virsh list --all --name").out.strip().split()
for e in self._cc.all_vms():
if e.name in vms:
self._export_vm(e)
not_vms = [x for x in self._cc.all_nodes() if x.kind == "physical"]
executor = ThreadPoolExecutor(max_workers=len(not_vms) + 1)
futures = []
for e in not_vms:
futures.append(executor.submit(save_phys, e.node))
futures.append(executor.submit(save_vms))
for x in futures:
x.result()
def import_cluster(self) -> None:
self._cc.prepare_external_port()
self._ais.import_snapshot(self._snapshot_dir())
ai_nodes = [h["requested_hostname"] for h in self._ai.list_hosts()]
active_vms = [x for x in self._cc.all_vms() if x.name in ai_nodes]
def load_vms() -> None:
for e in active_vms:
self._import_vm(e)
lh = host.LocalHost()
for e in active_vms:
lh.run(f'virsh destroy {e.name}')
for e in active_vms:
lh.run(f'virsh start {e.name}')
def load_phys(node: str) -> None:
coreosBuilder.ensure_fcos_exists()
rh = host.RemoteHost(node)
nfs = NFS(host.LocalHost(), self._cc.external_port)
file = nfs.host_file("/root/iso/fedora-coreos.iso")
rh.boot_iso_redfish(file)
logger.info(f"Restoring node {node}")
rh.ssh_connect("core")
rh.run("sudo mount /dev/sdb1 /mnt/")
rh.run("sudo sfdisk /dev/sda < /mnt/sda")
for e in get_part_table(rh, "/dev/sda"):
name = os.path.basename(e)
backup = f"/mnt/fsarchiver_backup.{name}.fsa"
backup_dd = f"/mnt/{name}.dd"
ret = rh.run(f"sudo {fsarchiver()} restfs {backup} id=0,dest=/{e}")
logger.info(ret)
if ret.returncode != 0:
rh.run(f"sudo dd if={backup_dd} of={e}")
logger.info(f"Finished restorting node {node}")
rh.run("sudo systemctl reboot")
not_vms = [x for x in self._cc.all_nodes() if x.kind == "physical"]
executor = ThreadPoolExecutor(max_workers=len(not_vms) + 1)
futures = []
for e in not_vms:
futures.append(executor.submit(load_phys, e.node))
futures.append(executor.submit(load_vms))
for x in futures:
x.result()
self._ai.download_kubeconfig_and_secrets(self._cc.name, self._cc.kubeconfig)
def _export_vm(self, config: NodeConfig) -> None:
lh = host.LocalHost()
src = config.image_path
dst = os.path.join(self._snapshot_dir(), os.path.basename(src))
logger.info(f"Copying {src} to {dst}")
lh.copy_to(src, dst)
def _import_vm(self, config: NodeConfig) -> None:
lh = host.LocalHost()
src = config.image_path
os.makedirs(os.path.dirname(src), exist_ok=True)
dst = os.path.join(self._snapshot_dir(), os.path.basename(src))
logger.info(f"Copying {dst} to {src}")
lh.copy_to(dst, src)
VmClusterNode(lh, config).setup_vm(config.image_path)
ClusterDeployer(self._cc, self._ai, [], "").update_etc_hosts()
def _snapshot_dir(self) -> str:
return os.path.join("/root/snapshots", self._name)