#!/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 errexit set -o nounset 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 <] [-c ] [-o ] [-v] [-h] -l: File with list of docker images. (Default /tmp/dockersave-list.tmp) -c: container command. (Default docker) -v: Increase verbosity and keep logs for troubleshooting. (Default false) -o: Path in which to save the images. (Default /tmp/images.tgz) -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/dockersave-list.tmp"} OUTPUT_PATH=${OUTPUT_PATH:-"/tmp/images.tgz"} VERBOSITY="false" CONTAINER_TOOL="docker" while getopts "hl:c:o:v" o; do case "${o}" in h) usage ;; l) LISTOFDOCKERIMAGES="${OPTARG}" ;; c) CONTAINER_TOOL="${OPTARG}" ;; o) OUTPUT_PATH="${OPTARG}" ;; v) VERBOSITY="true" ;; *) echo "ERROR: Invalid option '-${OPTARG}'"; usage ;; esac done # Do all the exports export LISTOFDOCKERIMAGES="${LISTOFDOCKERIMAGES}" export VERBOSITY="${VERBOSITY}" export CONTAINER_TOOL="${CONTAINER_TOOL}" } parse_cmdline_opts "$@" mkdir -p "$(dirname "${OUTPUT_PATH}")" xargs "${CONTAINER_TOOL}" save < "${LISTOFDOCKERIMAGES}" | gzip --rsyncable - > "${OUTPUT_PATH}"