Skip to content

Commit

Permalink
Use session-scoped inventory by default
Browse files Browse the repository at this point in the history
Currently, Manifester syncs its inventory multiple times while
generating a manifest to ensure that the inventory data matches with the
corresponding data in RHSM. For some use cases, though, this behavior is
not desirable. Notably, in a CI scenario in which multiple workers share
the same `username_prefix` setting, syncing the inventory with RHSM
prevents those workers from running `manifester delete --all` at the end
of each pipeline, as this could delete allocations that are still being
used by other workers.

This PR addresses this issue by converting the default inventory
behavior to not sync from RHSM unless a sync is explicitly invoked.
  • Loading branch information
synkd committed Nov 22, 2024
1 parent 5821185 commit 6352bbe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
3 changes: 2 additions & 1 deletion manifester/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def inventory(details, sync, offline_token):
border = "-" * 38
if sync:
helpers.update_inventory(
Manifester(minimal_init=True, offline_token=offline_token).subscription_allocations
Manifester(minimal_init=True, offline_token=offline_token).subscription_allocations,
sync=True,
)
inv = helpers.load_inventory_file(Path(settings.inventory_path))
if not details:
Expand Down
42 changes: 35 additions & 7 deletions manifester/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,43 @@ def load_inventory_file(file):
return yaml.load(f, Loader=yaml.FullLoader) or []


def update_inventory(inventory_data):
def _dump_inventory_file(inventory_path, inventory):
"""Write inventory data to local inventory file."""
with inventory_path.open("w") as inventory_file:
yaml.dump(inventory, inventory_file, allow_unicode=True)


def _update_inventory(inventory_path, inventory, allocation):
"""Add new allocation, remove and recreate inventory file."""
inventory.append(allocation)
inventory_path.unlink()
inventory_path.touch()
_dump_inventory_file(inventory_path, inventory)


def update_inventory(inventory_data, sync=False, remove=False, uuid=None):
"""Replace the existing inventory file with current subscription allocations."""
inventory_path = Path(settings.inventory_path)
if load_inventory_file(inventory_path):
inventory_path.unlink()
inventory_path.touch()
if inventory_data != []:
with inventory_path.open("w") as inventory_file:
yaml.dump(inventory_data, inventory_file, allow_unicode=True)
if sync:
if load_inventory_file(inventory_path):
inventory_path.unlink()
inventory_path.touch()
if inventory_data != []:
_dump_inventory_file(inventory_path, inventory_data)
elif remove:
inv = load_inventory_file(inventory_path)
_dump_inventory_file(inventory_path, [alloc for alloc in inv if uuid not in alloc["uuid"]])
else:
current_allocation = next(
iter([alloc for alloc in inventory_data if alloc["uuid"] == uuid])
)
inv = load_inventory_file(inventory_path)
uuids = [alloc["uuid"] for alloc in inv]
if current_allocation["uuid"] not in uuids:
_update_inventory(inventory_path, inv, current_allocation)
else:
inv = [alloc for alloc in inv if uuid not in alloc["uuid"]]
_update_inventory(inventory_path, inv, current_allocation)


def fake_http_response_code(good_codes=None, bad_codes=None, fail_rate=0):
Expand Down
13 changes: 7 additions & 6 deletions manifester/manifester.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def create_subscription_allocation(self):
f"Subscription allocation created with name {self.allocation_name} "
f"and UUID {self.allocation_uuid}"
)
update_inventory(self.subscription_allocations)
update_inventory(self.subscription_allocations, uuid=self.allocation_uuid)
return self.allocation_uuid

def delete_subscription_allocation(self, uuid=None):
Expand All @@ -203,7 +203,9 @@ def delete_subscription_allocation(self, uuid=None):
cmd_args=[f"{self.allocations_url}/{uuid if uuid else self.allocation_uuid}"],
cmd_kwargs=data,
)
update_inventory(self.subscription_allocations)
update_inventory(
self.subscription_allocations, remove=True, uuid=uuid if uuid else self.allocation_uuid
)
return response

def add_entitlements_to_allocation(self, pool_id, entitlement_quantity):
Expand Down Expand Up @@ -322,15 +324,15 @@ def process_subscription_pools(self, subscription_pools, subscription_data):
f"{subscription_data['name']} to the allocation."
)
self._active_pools.append(match)
update_inventory(self.subscription_allocations)
update_inventory(self.subscription_allocations, uuid=self.allocation_uuid)
break
elif add_entitlements.status_code == SUCCESS_CODE:
logger.debug(
f"Successfully added {subscription_data['quantity']} entitlements of "
f"{subscription_data['name']} to the allocation."
)
self._active_pools.append(match)
update_inventory(self.subscription_allocations)
update_inventory(self.subscription_allocations, uuid=self.allocation_uuid)
break
else:
raise RuntimeError(
Expand Down Expand Up @@ -409,7 +411,7 @@ def trigger_manifest_export(self):
manifest.uuid = self.allocation_uuid.uuid
else:
manifest.uuid = self.allocation_uuid
update_inventory(self.subscription_allocations)
update_inventory(self.subscription_allocations, uuid=self.allocation_uuid)
return manifest

def get_manifest(self):
Expand Down Expand Up @@ -437,4 +439,3 @@ def __enter__(self):
def __exit__(self, *tb_args):
"""Deletes subscription allocation on teardown unless using CLI."""
self.delete_subscription_allocation()
update_inventory(self.subscription_allocations)

0 comments on commit 6352bbe

Please sign in to comment.