From 57549e2f0f674769f251dc1e163993a6ed6dee11 Mon Sep 17 00:00:00 2001 From: Dirk Steinkopf Date: Sat, 26 Jun 2021 08:42:49 +0200 Subject: [PATCH 1/2] make it work for alpine and centos based images, too --- docker_push_latest_if_changed.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docker_push_latest_if_changed.py b/docker_push_latest_if_changed.py index e64da95..0ee3f56 100755 --- a/docker_push_latest_if_changed.py +++ b/docker_push_latest_if_changed.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3.6 +#!/usr/bin/env python3 import argparse import hashlib import subprocess @@ -191,7 +191,19 @@ def _get_commands_hash(image_uri: str) -> str: def _get_packages_hash(image_uri: str) -> str: - packages = _run_in_image(image_uri, ('dpkg', '-l')) + try: + packages = _run_in_image(image_uri, ('dpkg', '-l')) + except subprocess.CalledProcessError as oserr: + print(f'failed. returncode={oserr.returncode}. stderr:\n{oserr.stderr}') + if oserr.returncode == 127: + # retry - maybe we have an alpine based image + try: + packages = _run_in_image(image_uri, ('apk', 'list')) + except subprocess.CalledProcessError as oserr: + print(f'failed. returncode={oserr.returncode}. stderr:\n{oserr.stderr}') + if oserr.returncode == 127: + # retry - maybe we have an centos based image + packages = _run_in_image(image_uri, ('yum', 'list', '--quiet', 'installed')) print(f'Packages for {image_uri}:\n{packages}') return _get_digest(packages.encode()) @@ -207,8 +219,9 @@ def _run_in_image(image_uri: str, command: Tuple[str, ...]) -> str: '--rm', '--net=none', '--user=nobody', + '--entrypoint', command[0], image_uri, - *command, + *command[1:], ) return _check_output_and_print(run_command) From bd2c1b0e2f7e3d18df0c298e92789ce35fe30321 Mon Sep 17 00:00:00 2001 From: Dirk Steinkopf Date: Sat, 10 Jul 2021 07:19:08 +0200 Subject: [PATCH 2/2] improve error handling for hash creation --- docker_push_latest_if_changed.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/docker_push_latest_if_changed.py b/docker_push_latest_if_changed.py index 0ee3f56..f65666b 100755 --- a/docker_push_latest_if_changed.py +++ b/docker_push_latest_if_changed.py @@ -2,6 +2,7 @@ import argparse import hashlib import subprocess +import random from typing import NamedTuple from typing import Optional from typing import Sequence @@ -194,14 +195,20 @@ def _get_packages_hash(image_uri: str) -> str: try: packages = _run_in_image(image_uri, ('dpkg', '-l')) except subprocess.CalledProcessError as oserr: - print(f'failed. returncode={oserr.returncode}. stderr:\n{oserr.stderr}') - if oserr.returncode == 127: + if oserr.returncode != 127: + print(f'failed. returncode={oserr.returncode}. stderr:\n{oserr.stderr}') + else: # retry - maybe we have an alpine based image try: packages = _run_in_image(image_uri, ('apk', 'list')) except subprocess.CalledProcessError as oserr: - print(f'failed. returncode={oserr.returncode}. stderr:\n{oserr.stderr}') - if oserr.returncode == 127: + if oserr.returncode == 1: + # return code 1 seems ok here + packages = oserr.stdout + elif oserr.returncode != 127: + print(f'failed. returncode={oserr.returncode}. stdout:\n{oserr.stdout}\nstderr:\n{oserr.stderr}') + raise + else: # retry - maybe we have an centos based image packages = _run_in_image(image_uri, ('yum', 'list', '--quiet', 'installed')) print(f'Packages for {image_uri}:\n{packages}') @@ -219,6 +226,7 @@ def _run_in_image(image_uri: str, command: Tuple[str, ...]) -> str: '--rm', '--net=none', '--user=nobody', + '--name', 'tmp_' + str(random.randint(10000, 50000)), '--entrypoint', command[0], image_uri, *command[1:], @@ -228,7 +236,7 @@ def _run_in_image(image_uri: str, command: Tuple[str, ...]) -> str: def _check_output_and_print(command: Tuple[str, ...]) -> str: print(' '.join(command)) - output = subprocess.check_output(command, encoding='utf-8') + output = subprocess.check_output(command, encoding='utf-8', stderr=subprocess.STDOUT) return output