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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
25 #-------------------------------------------------------------------------------
26 # Print the help message which includes the usage, the expected parameters
27 # and their default values if they are not specified
28 #-------------------------------------------------------------------------------
31 # NOTE: shellcheck complains quoting in the example so SC2086 is disabled
32 # shellcheck disable=SC2086
35 Usage: $(basename ${0}) [-l <file with list of docker images>] [-c <container-tool>] [-o <output-path> ] [-v] [-h]
37 -l: File with list of docker images. (Default /tmp/dockersave-list.tmp)
38 -c: container command. (Default docker)
39 -v: Increase verbosity and keep logs for troubleshooting. (Default false)
40 -o: Path in which to save the images. (Default /tmp/images.tgz)
48 #-------------------------------------------------------------------------------
49 # Parse the arguments that are passed to the script
50 # If an argument is not specified, default values for those are set
52 # The priority order is
53 # - arguments: overrides the default values and values set as environment
54 # values. highest prio.
55 # - env vars: overrides the default values but not the values set from command
57 # - default values: only takes effect if the user doesn't specify the value
58 # of an argument either as an env var or from command line. lowest prio.
59 #-------------------------------------------------------------------------------
60 function parse_cmdline_opts() {
62 # set variables to the values set in env - otherwise, set them to defaults
63 LISTOFDOCKERIMAGES=${LISTOFDOCKERIMAGES:-"/tmp/dockersave-list.tmp"}
64 OUTPUT_PATH=${OUTPUT_PATH:-"/tmp/images.tgz"}
66 CONTAINER_TOOL="docker"
68 while getopts "hl:c:o:v" o; do
71 l) LISTOFDOCKERIMAGES="${OPTARG}" ;;
72 c) CONTAINER_TOOL="${OPTARG}" ;;
73 o) OUTPUT_PATH="${OPTARG}" ;;
74 v) VERBOSITY="true" ;;
75 *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;;
80 export LISTOFDOCKERIMAGES="${LISTOFDOCKERIMAGES}"
81 export VERBOSITY="${VERBOSITY}"
82 export CONTAINER_TOOL="${CONTAINER_TOOL}"
85 parse_cmdline_opts "$@"
87 mkdir -p "$(dirname "${OUTPUT_PATH}")"
88 xargs "${CONTAINER_TOOL}" save < "${LISTOFDOCKERIMAGES}" | gzip --rsyncable - > "${OUTPUT_PATH}"