Skip to content

Commit fbc327d

Browse files
authored
Merge pull request #415 from xcp-ng/ohu/xcpng-539/001
ohu/xcpng/001 - Tools - adds update scenario
2 parents 91b495b + da6256d commit fbc327d

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/tools/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Tools for automation.
2+
3+
This sub package is intended for scripting and automation tasks.
4+
Code is sometimes too long to be written as a standalone script
5+
(e.g. like ``scripts`` directory in root project).
6+
"""
7+
8+
import logging
9+
10+
logger = logging.getLogger()
11+
logging.basicConfig(
12+
format="%(asctime)s.%(msecs)03d %(levelname)s %(message)s", datefmt="%b %d %H:%M:%S", level=logging.INFO
13+
)

lib/tools/tasks/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Tasks
2+
3+
Tasks are actions performed on a remote target. Perhaps, it could be done on local targets too.
4+
Tasks can be used before and after running xcp-ng tests suite.
5+
"""

lib/tools/tasks/update.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Update tasks.
2+
3+
This module is intended for performing update actions on existing remote targets.
4+
"""
5+
from concurrent.futures import ThreadPoolExecutor
6+
7+
from lib.common import HostAddress
8+
from lib.host import Host
9+
from lib.pool import Pool
10+
11+
from .. import logger
12+
13+
def update_all(master_hosts: list[HostAddress], enablerepos: list[str]) -> None:
14+
"""Updates all master (primary) hosts.
15+
16+
.. note:: Host must be a master
17+
18+
Throws error if hosts are not master (primary).
19+
20+
:param :py:class:`list[lib.common.HostAddress]` master_hosts:
21+
A list of hosts to update.
22+
:param list[str] enablerepos:
23+
Repositories to enable when updating.
24+
"""
25+
logger.debug(f"[{master_hosts}] enablerepos: {enablerepos}")
26+
# init related pools
27+
pools = [Pool(h) for h in master_hosts]
28+
29+
with ThreadPoolExecutor() as executor:
30+
for p in pools:
31+
executor.submit(update_host, p.master, enablerepos)
32+
33+
34+
def update_host(host: Host, enablerepos: list[str] = []):
35+
"""Updates the target host.
36+
37+
An helper function that wraps update tasks on specific host.
38+
39+
:param :py:class:`lib.host.Host` host:
40+
Target host to update.
41+
:param list[str] enablerepos:
42+
Repositories to enable when updating.
43+
"""
44+
logger.info(f"[{host}] Updating...")
45+
46+
host.yum_clean_metadata()
47+
host.yum_update(enablerepos=enablerepos)
48+
# Everything's ok, just reboot
49+
host.reboot(verify=True)
50+
51+
logger.info(f"[{host}] Updated successfully!")

0 commit comments

Comments
 (0)