-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupdate.py
More file actions
64 lines (52 loc) · 2.31 KB
/
Copy pathupdate.py
File metadata and controls
64 lines (52 loc) · 2.31 KB
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
"""Update tasks.
This module is intended for performing update actions on existing remote targets.
"""
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor
from lib.host import Host
from lib.pool import NotAMasterHostError, Pool
from lib.tools.inventory import Inventory
from lib.tools.tasks.snapshot import create_snapshots
from .. import logger
def update_pools(inventory: Inventory) -> None:
"""Updates hosts in pool(s).
.. note::
Every non-master hosts in inventory will be ignored
*Update each pool's master host declared in inventory first, then, update other hosts for each pool.*
:param dict inventory:
Each host (key) holds its own config data (values, eg: `enablerepos`).
"""
logger.debug(f"Inventory: {inventory}")
inventory_hosts = inventory["hosts"]
# init related pools
pools: list[Pool] = []
nested_hosts: dict[str, list[Host]] = {}
for host in inventory_hosts:
try:
p = Pool(host)
pools.append(p)
hosting_pool = inventory_hosts[host]["hosting_pool"]
if hosting_pool is not None:
if nested_hosts.get(hosting_pool) is not None:
nested_hosts[hosting_pool].extend([h for h in p.hosts if h.is_nested])
else:
nested_hosts[hosting_pool] = [h for h in p.hosts if h.is_nested]
except NotAMasterHostError:
logger.warning(f"[{host}] Skipping: not a master host")
# update master hosts
with ThreadPoolExecutor() as executor:
for p in pools:
executor.submit(p.master.update, inventory_hosts[p.master.hostname_or_ip]["repositories"])
# update other hosts
with ThreadPoolExecutor() as executor:
for p in pools:
# omit first item because it is the pool's master
for other_host in p.hosts[1:]:
# repos are the same as for the master host
repos = inventory_hosts[p.master.hostname_or_ip]["repositories"]
executor.submit(other_host.update, repos)
# Snapshot creation
for hosting_pool, nested in nested_hosts.items():
pool = Pool(hosting_pool) # mandatory for getting an host instance
vm_uuids = [h.get_system_uuid() for h in nested]
create_snapshots(pool.master, vm_uuids)