copr-backend: cleanup-subscriptions: take a new access_token when needed

This commit is contained in:
Pavel Raiskup 2022-01-18 12:57:07 +01:00
parent 8112552d10
commit bbda47d9a9

View file

@ -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: