|
| 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