From 9ea7f5a9b06d70d657723be8f7d060d193898a82 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Mon, 17 Jan 2022 18:57:53 +0100 Subject: [PATCH] copr-backend: cleanup all orphaned subscriptions Not just the first 100 numbers (the list query is limited to 100 items). --- .../cleanup-unused-redhat-subscriptions | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions b/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions index c5b6a52d48..2b3b3a9ad0 100644 --- a/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions +++ b/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions @@ -7,6 +7,7 @@ the 'copr-team' RHN account. import logging import os +import random import subprocess import sys @@ -14,7 +15,7 @@ import requests URL_TOKEN = "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" -URL_SYSTEMS = "https://api.access.redhat.com/management/v1/systems?limit=1000" +URL_SYSTEMS = "https://api.access.redhat.com/management/v1/systems" URL_DELETE = "https://api.access.redhat.com/management/v1/systems/{UUID}" OFFLINE_TOKEN_FILE = "{{ rhn_offline_token_file }}" @@ -74,7 +75,35 @@ def get_systems(opts): """ Get the list of tracked systems in RHSM (list of dicts) """ - return get_auth(URL_SYSTEMS, opts).json()["body"] + page = 1 + all_systems = [] + found_systems = set() + while True: + limit = 100 + offset = (page-1) * limit + url = URL_SYSTEMS + "?limit={}&offset={}".format(limit, offset) + LOG.debug("Getting %s", url) + page += 1 + systems = get_auth(url, opts).json()["body"] + if not systems: + break + found = 0 + for new_system in systems: + name = new_system["name"] + if name in found_systems: + continue + found += 1 + found_systems.add(name) + all_systems.append(new_system) + + if not found: + break + + LOG.debug("Found %s systems (%s in total)", found, len(found)) + + # randomize the order, so we can parallelize this + random.shuffle(all_systems) + return all_systems def filter_out_systems(systems):