Skip to content

Commit 73d7fb4

Browse files
committed
tools/cli: add new arg for parent host
Parent is the machine where VMs (nested hosts) are living. So, in the inventory we need this information to perform snapshots on it while targetting guest hosts. Signed-off-by: Olivier Hoareau <olivier.hoareau@vates.tech>
1 parent 82aeb0e commit 73d7fb4

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ Take a look at an example inventory file:
673673

674674
[all]
675675
repositories = ["xcp-ng-base"]
676+
hosting_pool = "A"
676677

677678
[hosts]
678679

@@ -681,12 +682,13 @@ repositories = ["xcp-ng-base"]
681682
[hosts."ip_or_hostname-2"]
682683

683684
repositories = ["xcp-ng-updates"]
685+
hosting_pool = "B"
684686
```
685687

686688
> [!IMPORTANT]
687-
> Config values under `servers` override values under `all`. For instance, the above inventory would produce
689+
> Config values under `hosts` override values under `all`. For instance, the above inventory would produce
688690
> the following python dict:
689691
>
690-
> `{'ip_or_hostname-1': {'repositories': ['xcp-ng-base']}, 'ip_or_hostname-2': {'repositories': ['xcp-ng-updates']}}`
692+
> `{'ip_or_hostname-1': {'repositories': ['xcp-ng-base'], 'hosting_pool': 'A'}, 'ip_or_hostname-2': {'repositories': ['xcp-ng-updates'], 'hosting_pool': 'B'}}`
691693
>
692694
> Using *enablerepo flag* `-e` with inventory is still possible, it won't be used though.

lib/tools/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _command_update(args: argparse.Namespace) -> None:
1717
if args.inventory:
1818
inventory = load_inventory(args.inventory)
1919
else:
20-
inventory = into_inventory(args.hosts, args.repos)
20+
inventory = into_inventory(args.hosts, args.repos, args.hosting_pool)
2121

2222
update_pools(inventory)
2323

@@ -53,6 +53,12 @@ def cli() -> None:
5353
dest="repos",
5454
help="repositories to enable when updating",
5555
)
56+
subparser_cmd_update.add_argument(
57+
"-P",
58+
"--hosting-pool",
59+
type=HostAddress,
60+
help="Address (hostname|ip) of hosting pool's master host (nested context)",
61+
)
5662
subparser_cmd_update.set_defaults(func=_command_update)
5763

5864
args = parser.parse_args()

lib/tools/inventory.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

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

1516

1617
HostConfigs: TypeAlias = dict[HostAddress, HostConfig]
1718

1819
class Inventory(TypedDict):
1920
hosts: HostConfigs
2021

21-
2222
def load_inventory(inventory_path: Path) -> Inventory:
2323
"""Create an inventory object from loaded inventory file."""
2424
with open(inventory_path, "rb") as f:
@@ -30,22 +30,31 @@ def load_inventory(inventory_path: Path) -> Inventory:
3030
inventory_hosts: HostConfigs = {}
3131
for h, config in hosts.items():
3232
repos = config.get("repositories", [])
33-
host: HostConfig = {"repositories": repos or all.get("repositories", [])}
33+
hosting_pool = config.get("hosting_pool", None)
34+
if hosting_pool is None:
35+
hosting_pool = all.get("hosting_pool", None)
36+
host: HostConfig = {
37+
"repositories": repos or all.get("repositories", []),
38+
"hosting_pool": hosting_pool,
39+
}
3440
inventory_hosts[h] = host
3541

3642
return {
3743
"hosts": inventory_hosts,
3844
}
3945

4046

41-
def into_inventory(hosts: list[HostAddress], repositories: list[str]) -> Inventory:
47+
def into_inventory(hosts: list[HostAddress], repositories: list[str], hosting_pool: HostAddress) -> Inventory:
4248
"""Create an inventory object from arguments.
4349
4450
Basically, it is used as compatibility when we don't want inventory from file.
4551
"""
4652
inventory_hosts: HostConfigs = {}
4753
for h in hosts:
48-
host: HostConfig = {"repositories": repositories or []}
54+
host: HostConfig = {
55+
"repositories": repositories or [],
56+
"hosting_pool": hosting_pool or None,
57+
}
4958
inventory_hosts[h] = host
5059

5160
return {

0 commit comments

Comments
 (0)