containers: export all images in one file
[infra/stack/kubernetes.git] / playbooks / roles / package / files / save-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 set -o errexit
22 set -o nounset
23 set -o pipefail
24
25 #-------------------------------------------------------------------------------
26 # Print the help message which includes the usage, the expected parameters
27 # and their default values if they are not specified
28 #-------------------------------------------------------------------------------
29 function usage() {
30
31   # NOTE: shellcheck complains quoting in the example so SC2086 is disabled
32   # shellcheck disable=SC2086
33   cat <<EOF
34
35 Usage: $(basename ${0}) [-l <file with list of docker images>] [-c <container-tool>] [-o <output-path> ] [-v] [-h]
36
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)
41     -h: This message.
42
43 EOF
44   exit 0
45
46 }
47
48 #-------------------------------------------------------------------------------
49 # Parse the arguments that are passed to the script
50 # If an argument is not specified, default values for those are set
51 #
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
56 #   line.
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() {
61
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"}
65   VERBOSITY="false"
66   CONTAINER_TOOL="docker"
67
68   while getopts "hl:c:o:v" o; do
69     case "${o}" in
70       h) usage ;;
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 ;;
76     esac
77   done
78
79   # Do all the exports
80   export LISTOFDOCKERIMAGES="${LISTOFDOCKERIMAGES}"
81   export VERBOSITY="${VERBOSITY}"
82   export CONTAINER_TOOL="${CONTAINER_TOOL}"
83 }
84
85 parse_cmdline_opts "$@"
86
87 mkdir -p "$(dirname "${OUTPUT_PATH}")"
88 xargs "${CONTAINER_TOOL}" save < "${LISTOFDOCKERIMAGES}" | gzip --rsyncable - > "${OUTPUT_PATH}"