Skip to content

Commit b40781e

Browse files
committed
Add disablerepos to update tool
Add a way to exclude repositories from the update via a disablerepos field in the inventory (or a --disablerepo CLI flag), and restore the previous yum_update() behavior where --enablerepo only enables extra repositories instead of disabling all others. Signed-off-by: Gaëtan Lehmann <gaetan.lehmann@vates.tech>
1 parent 6f680f1 commit b40781e

5 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ For each pool target :
654654

655655
1. Update master host of the pool:
656656
* Clean cached metadata
657-
* Update with repository manager (yum): Optionally enables repositories
657+
* Update with repository manager (yum): Optionally enables or disables repositories
658658
* Reboot
659659
2. Get other hosts of the pool
660660
* Repeat step `1.` for each host
@@ -679,6 +679,7 @@ Take a look at an example inventory file:
679679

680680
[default]
681681
repositories = ["xcp-ng-base"]
682+
disabled_repositories = ["epel"]
682683
hosting_pool = "A"
683684

684685
[hosts]
@@ -696,6 +697,9 @@ hosting_pool = "B"
696697
> * Config values under `hosts` override values under `default`. For instance, the above inventory would produce
697698
> the following python dict:
698699
>
699-
> `{'ip_or_hostname-1': {'repositories': ['xcp-ng-base'], 'hosting_pool': 'A'}, 'ip_or_hostname-2': {'repositories': ['xcp-ng-updates'], 'hosting_pool': 'B'}}`
700+
> `{'ip_or_hostname-1': {'repositories': ['xcp-ng-base'], 'disabled_repositories': ['epel'], 'hosting_pool': 'A'}, 'ip_or_hostname-2': {'repositories': ['xcp-ng-updates'], 'hosting_pool': 'B'}}`
701+
>
702+
> * `disabled_repositories` disables one or more repositories during the update. It can be set under `default` or overridden per host, and can also be passed with the `-x/--disablerepo` flag.
703+
> * `*` as a repository value disables **all** repositories (e.g. `disabled_repositories = ["*"]`).
700704
>
701705
> * When `--inventory` flag is present, repos passed to `-e` flag won't be considered.

lib/host.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,27 +511,33 @@ def yum_clean_metadata(self) -> str:
511511
logging.info(f"[{self}] Removing cache metadata...")
512512
return self.ssh("yum clean metadata -q")
513513

514-
def yum_update(self, enablerepos: list[str] = []) -> str:
514+
def yum_update(self, enablerepos: list[str] = [], disablerepos: list[str] = []) -> str:
515515
"""Updates packages on target.
516516
517517
Performs the following shell command::
518518
519519
yum update -y
520520
# with enablerepos
521-
yum update -y --disablerepo='*' --enablerepo=extra1 --enablerepos=extra2
521+
yum update -y --enablerepo=extra1 --enablerepos=extra2
522+
# with disablerepos
523+
yum update -y --disablerepo=extra1 --disablerepos=extra2
522524
523-
:param enablerepos: Enable one or more repositories (default: []) if present disable other repos
525+
:param enablerepos: Enable one or more repositories (default: [])
526+
:param disablerepos: Disable one or more repositories (default: [])
524527
"""
525528
base_command = "yum update -y"
526529

527530
logging.info(f"[{self}] Updating packages...")
531+
if disablerepos:
532+
extra = " ".join(f"--disablerepo={r}" for r in disablerepos)
533+
base_command = f"{base_command} {extra}"
528534
if enablerepos:
529535
extra = " ".join(f"--enablerepo={r}" for r in enablerepos)
530-
base_command = f"{base_command} --disablerepo='*' {extra}"
536+
base_command = f"{base_command} {extra}"
531537

532538
return self.ssh(base_command)
533539

534-
def update(self, enablerepos: list[str] = [], reboot: bool = True) -> None:
540+
def update(self, enablerepos: list[str] = [], disablerepos: list[str] = [], reboot: bool = True) -> None:
535541
"""Updates current host.
536542
537543
An helper function that wraps update tasks on current host.
@@ -541,13 +547,15 @@ def update(self, enablerepos: list[str] = [], reboot: bool = True) -> None:
541547
542548
:param list[str] enablerepos:
543549
Repositories to enable when updating.
550+
:param list[str] disablerepos:
551+
Repositories to disable when updating.
544552
:param bool reboot:
545553
Choose to reboot or not after update (default: True).
546554
"""
547555
logging.info(f"[{self}] Updating...")
548556

549557
self.yum_clean_metadata()
550-
output = self.yum_update(enablerepos=enablerepos)
558+
output = self.yum_update(enablerepos=enablerepos, disablerepos=disablerepos)
551559
if reboot and "No packages marked for update" not in output:
552560
# Everything's ok, just reboot
553561
self.reboot(verify=True)

lib/tools/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _command_update(args: argparse.Namespace) -> None:
1818
if args.inventory:
1919
inventory = load_inventory(args.inventory)
2020
else:
21-
inventory = into_inventory(args.hosts, args.repos, args.hosting_pool)
21+
inventory = into_inventory(args.hosts, args.repos, args.hosting_pool, disabled_repositories=args.disablerepos)
2222

2323
update_pools(inventory)
2424

@@ -63,6 +63,13 @@ def cli() -> None:
6363
dest="repos",
6464
help="repositories to enable when updating",
6565
)
66+
subparser_cmd_update.add_argument(
67+
"-x", "--disablerepo",
68+
metavar="REPO",
69+
action="append",
70+
dest="disablerepos",
71+
help="repositories to disable when updating",
72+
)
6673
subparser_cmd_update.add_argument(
6774
"-P",
6875
"--hosting-pool",

lib/tools/inventory.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
class HostConfig(TypedDict):
1313
repositories: list[str]
14+
disabled_repositories: list[str]
1415
hosting_pool: HostAddress | None
1516

1617

@@ -30,11 +31,13 @@ def load_inventory(inventory_path: Path) -> Inventory:
3031
inventory_hosts: HostConfigs = {}
3132
for h, config in hosts.items():
3233
repos = config.get("repositories", [])
34+
disabled_repositories = config.get("disabled_repositories", [])
3335
hosting_pool = config.get("hosting_pool", None)
3436
if hosting_pool is None:
3537
hosting_pool = default.get("hosting_pool", None)
3638
host: HostConfig = {
3739
"repositories": repos or default.get("repositories", []),
40+
"disabled_repositories": disabled_repositories or default.get("disabled_repositories", []),
3841
"hosting_pool": hosting_pool,
3942
}
4043
inventory_hosts[h] = host
@@ -44,7 +47,12 @@ def load_inventory(inventory_path: Path) -> Inventory:
4447
}
4548

4649

47-
def into_inventory(hosts: list[HostAddress], repositories: list[str], hosting_pool: HostAddress) -> Inventory:
50+
def into_inventory(
51+
hosts: list[HostAddress],
52+
repositories: list[str],
53+
hosting_pool: HostAddress,
54+
disabled_repositories: list[str] = [],
55+
) -> Inventory:
4856
"""Create an inventory object from arguments.
4957
5058
Basically, it is used as compatibility when we don't want inventory from file.
@@ -53,6 +61,7 @@ def into_inventory(hosts: list[HostAddress], repositories: list[str], hosting_po
5361
for h in hosts:
5462
host: HostConfig = {
5563
"repositories": repositories or [],
64+
"disabled_repositories": disabled_repositories or [],
5665
"hosting_pool": hosting_pool or None,
5766
}
5867
inventory_hosts[h] = host

lib/tools/tasks/update.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def update_pools(inventory: Inventory) -> None:
9696
# update master hosts
9797
with ThreadPoolExecutor() as executor:
9898
future_masters = {executor.submit(
99-
p.master.update, inventory_hosts[p.master.hostname_or_ip]["repositories"]): p.master for p in pools}
99+
p.master.update,
100+
inventory_hosts[p.master.hostname_or_ip]["repositories"],
101+
disablerepos=inventory_hosts[p.master.hostname_or_ip]["disabled_repositories"],
102+
): p.master for p in pools}
100103
for future in as_completed(future_masters):
101104
future_master = future_masters[future]
102105
try:
@@ -117,7 +120,8 @@ def update_pools(inventory: Inventory) -> None:
117120
for h in p.hosts[1:]:
118121
# repos are the same as for the master host
119122
repos = inventory_hosts[p.master.hostname_or_ip]["repositories"]
120-
future_other_hosts[executor.submit(h.update, repos)] = h
123+
disablerepos = inventory_hosts[p.master.hostname_or_ip]["disabled_repositories"]
124+
future_other_hosts[executor.submit(h.update, repos, disablerepos=disablerepos)] = h
121125
for future in as_completed(future_other_hosts):
122126
other_host = future_other_hosts[future]
123127
try:

0 commit comments

Comments
 (0)