From bbda47d9a9ca3c3634cbc88477f2cdf53750d8c4 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 18 Jan 2022 12:57:07 +0100 Subject: [PATCH] copr-backend: cleanup-subscriptions: take a new access_token when needed --- .../cleanup-unused-redhat-subscriptions | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions b/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions index b172ef3f2c..1a7ab18ac0 100644 --- a/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions +++ b/roles/copr/backend/templates/cleanup-unused-redhat-subscriptions @@ -5,6 +5,7 @@ Periodically remove unused (staled, forgotten, orphaned, ...) entitlements from the 'copr-team' RHN account. """ +import argparse import logging import os import random @@ -49,7 +50,18 @@ def _auth_headers(opts): def get_auth(url, opts): """ Get, with auth header """ - return requests.get(url, headers=_auth_headers(opts)) + + if "access_token" not in opts or not opts["access_token"]: + get_access_token(opts) + + response = None + for _ in range(2): + response = requests.get(url, headers=_auth_headers(opts)) + if response.status_code == 401: + get_access_token(opts) + continue + return response + def delete_auth(url, opts): """ Get, with auth header """ @@ -148,11 +160,15 @@ def drop_system(system, opts): def _main(): + + parser = argparse.ArgumentParser() + parser.add_argument("--token-file", default=OFFLINE_TOKEN_FILE) + args = parser.parse_args() + opts = {} opts["offline_token"] = None - with open(os.path.expanduser(OFFLINE_TOKEN_FILE), "r") as file: + with open(os.path.expanduser(args.token_file), "r") as file: opts["offline_token"] = file.read().strip() - get_access_token(opts) systems = get_systems(opts) remove_candidates = filter_out_systems(systems) for system in remove_candidates: