containers: export all images in one file
[infra/stack/kubernetes.git] / playbooks / roles / package / files / pull-images.sh
diff --git a/playbooks/roles/package/files/pull-images.sh b/playbooks/roles/package/files/pull-images.sh
new file mode 100755 (executable)
index 0000000..3ceec85
--- /dev/null
@@ -0,0 +1,90 @@
+#!/bin/bash
+
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2021 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+
+set -o nounset
+set -o errexit
+set -o pipefail
+
+#-------------------------------------------------------------------------------
+# Print the help message which includes the usage, the expected parameters
+# and their default values if they are not specified
+#-------------------------------------------------------------------------------
+function usage() {
+
+  # NOTE: shellcheck complains quoting in the example so SC2086 is disabled
+  # shellcheck disable=SC2086
+  cat <<EOF
+
+Usage: $(basename ${0}) [-l <file with list of docker images>] [-p <number in parallel>] [-c <container-tool>]  [-v] [-h]
+
+    -l: File with list of docker images. (Default /tmp/docker-list.tmp)
+    -c: container command. (Default docker)
+    -p: number in parallel. (Default 4)
+    -v: Increase verbosity and keep logs for troubleshooting. (Default false)
+    -h: This message.
+
+EOF
+  exit 0
+
+}
+
+
+#-------------------------------------------------------------------------------
+# Parse the arguments that are passed to the script
+# If an argument is not specified, default values for those are set
+#
+# The priority order is
+# - arguments: overrides the default values and values set as environment
+#   values. highest prio.
+# - env vars: overrides the default values but not the values set from command
+#   line.
+# - default values: only takes effect if the user doesn't specify the value
+#   of an argument either as an env var or from command line. lowest prio.
+#-------------------------------------------------------------------------------
+function parse_cmdline_opts() {
+
+  # set variables to the values set in env - otherwise, set them to defaults
+  LISTOFDOCKERIMAGES=${LISTOFDOCKERIMAGES:-"/tmp/docker-list.tmp"}
+  PARALLELNU=${PARALLELNU:-4}
+  VERBOSITY="false"
+  CONTAINER_TOOL="docker"
+
+  while getopts ":hl:p:c:v" o; do
+    case "${o}" in
+      h) usage ;;
+      l) LISTOFDOCKERIMAGES="${OPTARG}" ;;
+      p) PARALLELNU="${OPTARG}" ;;
+      c) CONTAINER_TOOL="${OPTARG}" ;;
+      v) VERBOSITY="true" ;;
+      *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;;
+    esac
+  done
+
+  # Do all the exports
+  export LISTOFDOCKERIMAGES="${LISTOFDOCKERIMAGES}"
+  export PARALLELNU="${PARALLELNU}"
+  export VERBOSITY="${VERBOSITY}"
+  export CONTAINER_TOOL="${CONTAINER_TOOL}"
+}
+
+parse_cmdline_opts "$@"
+
+xargs -n 1 -P "$PARALLELNU" "$CONTAINER_TOOL" pull < "$LISTOFDOCKERIMAGES"