Skip to content

Commit 821e75c

Browse files
authored
Merge pull request #468 from xcp-ng/ohu/update-tools/snapshot-3
tools/update - create snapshot for nested hosts
2 parents adf9f6f + 31e53f1 commit 821e75c

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

lib/tools/tasks/snapshot.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Snapshot tasks.
2+
"""
3+
from concurrent.futures import ThreadPoolExecutor
4+
from datetime import date
5+
6+
from lib.host import Host
7+
from lib.vm import VM
8+
9+
from .. import logger
10+
11+
def create_snapshots(host: Host, vm_uuids: list[str]):
12+
"""Create a snapshot for a list of VMs in host.
13+
14+
:param host:
15+
The target hosting pool which hosts the VMs.
16+
:param vm_uuids:
17+
uuids of target VMs.
18+
"""
19+
# init VMs list
20+
vms = [VM(uuid, host) for uuid in vm_uuids]
21+
22+
snapshot_name = f"utd-{date.today().strftime('%Y%m%d')}"
23+
24+
logger.debug(f"[{host}] Create snapshot '{snapshot_name}' for VMs: {vm_uuids}")
25+
26+
with ThreadPoolExecutor() as executor:
27+
for vm in vms:
28+
executor.submit(vm.snapshot, None, snapshot_name)

lib/tools/tasks/update.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
from concurrent.futures import ThreadPoolExecutor
88

9+
from lib.host import Host
910
from lib.pool import NotAMasterHostError, Pool
1011
from lib.tools.inventory import Inventory
12+
from lib.tools.tasks.snapshot import create_snapshots
1113

1214
from .. import logger
1315

@@ -27,10 +29,18 @@ def update_pools(inventory: Inventory) -> None:
2729
inventory_hosts = inventory["hosts"]
2830
# init related pools
2931
pools: list[Pool] = []
32+
nested_hosts: dict[str, list[Host]] = {}
3033
for host in inventory_hosts:
3134
try:
3235
p = Pool(host)
3336
pools.append(p)
37+
hosting_pool = inventory_hosts[host]["hosting_pool"]
38+
if hosting_pool is not None:
39+
# we assume all hosts are nested, not only master
40+
if nested_hosts.get(hosting_pool) is not None:
41+
nested_hosts[hosting_pool].extend(p.hosts)
42+
else:
43+
nested_hosts[hosting_pool] = p.hosts
3444
except NotAMasterHostError:
3545
logger.warning(f"[{host}] Skipping: not a master host")
3646

@@ -47,3 +57,9 @@ def update_pools(inventory: Inventory) -> None:
4757
# repos are the same as for the master host
4858
repos = inventory_hosts[p.master.hostname_or_ip]["repositories"]
4959
executor.submit(other_host.update, repos)
60+
61+
# Snapshot creation
62+
for hosting_pool, nested in nested_hosts.items():
63+
pool = Pool(hosting_pool) # mandatory for getting an host instance
64+
vm_uuids = [h.get_system_uuid() for h in nested]
65+
create_snapshots(pool.master, vm_uuids)

0 commit comments

Comments
 (0)