From 6b330f9e0da7cc70db50d87eed8f452c4d5e9405 Mon Sep 17 00:00:00 2001
From: Alexander Wellbrock <a.wellbrock@mailbox.org>
Date: Thu, 14 Jan 2021 14:27:28 +0100
Subject: [PATCH] add si-pull tool

The image pull is now done in it's own script file to handle the logic
of trying different expected images and fail if none was found.
---
 gitlab-ci-template.yml            |  4 +-
 resources/shell-tools/bin/pull.sh | 86 +++++++++++++++++++++++++++++++
 resources/shell-tools/install.sh  |  3 ++
 3 files changed, 90 insertions(+), 3 deletions(-)
 create mode 100755 resources/shell-tools/bin/pull.sh

diff --git a/gitlab-ci-template.yml b/gitlab-ci-template.yml
index e3648ee..8c8ffca 100644
--- a/gitlab-ci-template.yml
+++ b/gitlab-ci-template.yml
@@ -58,9 +58,7 @@ container-tagging:
     - podman login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
     - export BUILD_DATE="$(date --rfc-3339 ns)"
   script:
-    - podman pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA || true
-    - podman pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-amd64" || true
-    - podman pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-arm64" || true
+    - si-pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA"
     - si-tagging -l "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA" "$CI_REGISTRY_IMAGE" "${CI_REGISTRY_IMAGE_VERSION}"
     - si-push "$CI_REGISTRY_IMAGE"
   resource_group: latest
diff --git a/resources/shell-tools/bin/pull.sh b/resources/shell-tools/bin/pull.sh
new file mode 100755
index 0000000..97ad9d3
--- /dev/null
+++ b/resources/shell-tools/bin/pull.sh
@@ -0,0 +1,86 @@
+#!/bin/bash
+
+set -o pipefail
+set -u
+set -e
+
+VERSION=0.1.0
+
+printVersion() {
+    echo "$VERSION"
+}
+
+printUsage() {
+    printVersion
+    echo "
+    Shivering-Isles pull tool
+
+    This tool will pull all available base container images from upstream
+
+    Usage of $0:
+        $0 <IMAGE REFERENCE>
+
+    Example:
+        $0 registry.example.com/example/app
+    "
+    exit 1
+}
+
+CONTAINER_CMD=podman
+
+if ! command -v "$CONTAINER_CMD" >/dev/null 2>&1; then
+    CONTAINER_CMD=docker
+fi
+
+if [ "$1" = "--help" ]; then
+    printUsage
+fi
+
+for i in "$@"
+do
+case $i in
+    --container-cmd=*)
+    CONTAINER_CMD="${i#*=}"
+    shift # past argument with no value
+    ;;
+    -v|--version)
+    printVersion
+    exit 0
+    shift # past argument with no value
+    ;;
+    -h|--help)
+    printUsage
+    shift
+    ;;
+    *)
+          # further/unknown options
+    ;;
+esac
+done
+
+CONTAINER_IMAGE_NAME=${1:-invalid}
+
+if [ "$CONTAINER_IMAGE_NAME" = "invalid" ]; then
+    echo "Error: Invalid image name" >&2
+    printUsage
+fi
+
+IMAGE_PULL_SUCCESS=0
+
+podman pull "$CONTAINER_IMAGE_NAME" && \
+  IMAGE_PULL_SUCCESS=1 || \
+  true
+
+podman pull "$CONTAINER_IMAGE_NAME-amd64" && \
+  IMAGE_PULL_SUCCESS=1 || \
+  true
+
+podman pull "$CONTAINER_IMAGE_NAME-arm64" || \
+  podman pull "$CONTAINER_IMAGE_NAME-aarch64" && \
+  IMAGE_PULL_SUCCESS=1 || \
+  true
+
+if [ "$IMAGE_PULL_SUCCESS" == "0" ]; then
+  echo "Error: No images pulled" >&2
+  exit 1
+fi
diff --git a/resources/shell-tools/install.sh b/resources/shell-tools/install.sh
index 6480b76..4ec0a50 100755
--- a/resources/shell-tools/install.sh
+++ b/resources/shell-tools/install.sh
@@ -4,10 +4,13 @@ BASENAME="$(dirname "$0")"
 
 cp "$BASENAME"/./bin/tagging.sh /usr/local/bin/si-tagging
 cp "$BASENAME"/./bin/push.sh /usr/local/bin/si-push
+cp "$BASENAME"/./bin/pull.sh /usr/local/bin/si-pull
 cp "$BASENAME"/./bin/fix-dockerfile.sh /usr/local/bin/si-fix
 chown root:root /usr/local/bin/si-tagging
 chown root:root /usr/local/bin/si-push
+chown root:root /usr/local/bin/si-pull
 chown root:root /usr/local/bin/si-fix
 chmod 0755 /usr/local/bin/si-tagging
 chmod 0755 /usr/local/bin/si-push
+chmod 0755 /usr/local/bin/si-pull
 chmod 0755 /usr/local/bin/si-fix
-- 
GitLab