diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 1140a7c..4c8672b 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -55,10 +55,8 @@ def filter_image_list(images_list): return filter_images -def get_build_number_1(token, latest_image_digest): +def get_build_number(token, latest_image_digest): _, image_lists = get_images_list(token) - # images_list = [d["name"] for d in image_lists] - # images_list = filter_image_list(images_list) match_and_return_name = next( ( d["name"] @@ -71,39 +69,16 @@ def get_build_number_1(token, latest_image_digest): return match_and_return_name -def get_image_digest(token, image): - headers = { - "Authorization": f"Bearer {token}", - "Accept": "application/vnd.docker.distribution.manifest.v2+json", - } - image_digest_url = ( - f"https://registry.hub.docker.com/v2/splunk/splunk/manifests/{image}" +def get_image_digest(token, image, image_list): + return next( + ( + image_data['digest'] + for d in image_list + if d['name'] == image + for image_data in d.get('images', []) + ), + None ) - response = requests.get(image_digest_url, headers=headers) - response.raise_for_status() - if response.headers["Docker-Content-Digest"]: - return response.headers["Docker-Content-Digest"] - else: - token = get_token() - headers = { - "Authorization": f"Bearer {token}", - "Accept": "application/vnd.docker.distribution.manifest.v2+json", - } - image_digest_url = ( - "https://registry.hub.docker.com/v2/splunk/splunk/manifests/{}".format( - image - ) - ) - response = requests.get(image_digest_url, headers=headers) - response.raise_for_status() - return response.headers["Docker-Content-Digest"] - - -def get_build_number(token, filter_images, latest_image_digest): - for image in filter_images: - if get_image_digest(token, image) == latest_image_digest: - return image - def update_splunk_version(token): if os.path.isfile("config/splunk_matrix.conf"): @@ -116,19 +91,12 @@ def update_splunk_version(token): # images_list = [d["name"] for d in all_images] for stanza in config.sections(): if stanza != "GENERAL": - print(images_list) latest_image_version = get_latest_image(stanza, images_list) stanza_image_version = config.get(stanza, "VERSION") - print(latest_image_version,stanza_image_version) if check_image_version(latest_image_version, stanza_image_version): config.set(stanza, "VERSION", latest_image_version) - latest_image_digest = get_image_digest(token, latest_image_version) - # build_number = get_build_number( - # token, filter_images, latest_image_digest - # ) - print(latest_image_digest) - build_number = get_build_number_1(token, latest_image_digest) - print(build_number) + latest_image_digest = get_image_digest(token, latest_image_version, all_images) + build_number = get_build_number(token, latest_image_digest) config.set(stanza, "BUILD", build_number) update_file = True