Ingress Nginx Integration
[infra/stack/kubernetes.git] / playbooks / roles / package / files / pull-images.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2021 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21
22 set -o nounset
23 set -o errexit
24 set -o pipefail
25
26 #-------------------------------------------------------------------------------
27 # Print the help message which includes the usage, the expected parameters
28 # and their default values if they are not specified
29 #-------------------------------------------------------------------------------
30 function usage() {
31
32   # NOTE: shellcheck complains quoting in the example so SC2086 is disabled
33   # shellcheck disable=SC2086
34   cat <<EOF
35
36 Usage: $(basename ${0}) [-l <file with list of docker images>] [-p <number in parallel>] [-c <container-tool>]  [-v] [-h]
37
38     -l: File with list of docker images. (Default /tmp/docker-list.tmp)
39     -c: container command. (Default docker)
40     -p: number in parallel. (Default 4)
41     -v: Increase verbosity and keep logs for troubleshooting. (Default false)
42     -h: This message.
43
44 EOF
45   exit 0
46
47 }
48
49
50 #-------------------------------------------------------------------------------
51 # Parse the arguments that are passed to the script
52 # If an argument is not specified, default values for those are set
53 #
54 # The priority order is
55 # - arguments: overrides the default values and values set as environment
56 #   values. highest prio.
57 # - env vars: overrides the default values but not the values set from command
58 #   line.
59 # - default values: only takes effect if the user doesn't specify the value
60 #   of an argument either as an env var or from command line. lowest prio.
61 #-------------------------------------------------------------------------------
62 function parse_cmdline_opts() {
63
64   # set variables to the values set in env - otherwise, set them to defaults
65   LISTOFDOCKERIMAGES=${LISTOFDOCKERIMAGES:-"/tmp/docker-list.tmp"}
66   PARALLELNU=${PARALLELNU:-4}
67   VERBOSITY="false"
68   CONTAINER_TOOL="docker"
69
70   while getopts ":hl:p:c:v" o; do
71     case "${o}" in
72       h) usage ;;
73       l) LISTOFDOCKERIMAGES="${OPTARG}" ;;
74       p) PARALLELNU="${OPTARG}" ;;
75       c) CONTAINER_TOOL="${OPTARG}" ;;
76       v) VERBOSITY="true" ;;
77       *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;;
78     esac
79   done
80
81   # Do all the exports
82   export LISTOFDOCKERIMAGES="${LISTOFDOCKERIMAGES}"
83   export PARALLELNU="${PARALLELNU}"
84   export VERBOSITY="${VERBOSITY}"
85   export CONTAINER_TOOL="${CONTAINER_TOOL}"
86 }
87
88 parse_cmdline_opts "$@"
89
90 xargs -n 1 -P "$PARALLELNU" "$CONTAINER_TOOL" pull < "$LISTOFDOCKERIMAGES"