X-Git-Url: https://gerrit.nordix.org/gitweb?p=infra%2Fstack%2Fkubernetes.git;a=blobdiff_plain;f=playbooks%2Froles%2Fpackage%2Ffiles%2Fsave-images.sh;fp=playbooks%2Froles%2Fpackage%2Ffiles%2Fsave-images.sh;h=0b2cb748934a2890aa85421e247bd83e87db6e04;hp=0000000000000000000000000000000000000000;hb=dbb881e7fd077d2818d01dd6b46d4899af6940d8;hpb=df6ea7a608badaa4f78783bc0ac6184ce484e522 diff --git a/playbooks/roles/package/files/save-images.sh b/playbooks/roles/package/files/save-images.sh new file mode 100755 index 0000000..0b2cb74 --- /dev/null +++ b/playbooks/roles/package/files/save-images.sh @@ -0,0 +1,88 @@ +#!/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}"