diff --git a/hack/tools/offline-installation-tool.sh b/hack/tools/offline-installation-tool.sh new file mode 100644 index 000000000..4b93ddbe5 --- /dev/null +++ b/hack/tools/offline-installation-tool.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash + +# 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 + +CurrentDIR=$(cd "$(dirname "$0")" || exit;pwd) +ImagesDirDefault=${CurrentDIR}/komos-images +registryurl="" +save="false" + +func() { + echo "Usage:" + echo + echo " $0 [-l IMAGES-LIST] [-d IMAGES-DIR] [-r PRIVATE-REGISTRY]" + echo + echo "Description:" + echo " -d IMAGES-DIR : the dir of files (tar.gz) which generated by \`docker save\`. default: ${ImagesDirDefault}" + echo " -l IMAGES-LIST : text file with list of images." + echo " -r PRIVATE-REGISTRY : target private registry:port." + echo " -s : save model will be applied. Pull the images in the IMAGES-LIST and save images as a tar.gz file." + echo " -h : usage message" + echo + echo "Examples:" + echo + echo "# Docker pull/save, while imageName should exist in images-list, e.g:" + echo "./offline-installtion-tool.sh -s -l images-list.text -d /home/apps" + echo + echo "# Docker load/tag/push images from images-dir to private-registry, while imageName should exist in images-list, e.g:" + echo "./offline-installtion-tool.sh -l images-list.text -d /home/apps -r quay.azk8s.cn/kosmos-io" + exit +} + +while getopts 'sl:r:d:v:h' OPT; do + case $OPT in + d) ImagesDir="$OPTARG";; + l) ImagesList="$OPTARG";; + r) Registry="$OPTARG";; + s) save="true";; + h) func;; + ?) func;; + *) func;; + esac +done + +if [ -z "${ImagesDir}" ]; then + ImagesDir=${ImagesDirDefault} +fi + +if [ -n "${Registry}" ]; then + registryUrl=${Registry} +fi + + +if [[ ${save} == "true" ]] && [[ -n "${ImagesList}" ]]; then + # create ImageDir if not exist + if [ ! -d ${ImagesDir} ]; then + mkdir -p ${ImagesDir} + fi + + # count the number of images in ImageList + ImagesListLen=$(cat ${ImagesList} | wc -l) + name="" + images="" + index=0 + for image in $(<${ImagesList}); do + if [[ ${image} =~ ^\#\#.* ]]; then + if [[ -n ${images} ]]; then + echo "" + echo "Save images: "${name}" to "${ImagesDir}"/"${name}".tar.gz <<<" + docker save ${images} | gzip -c > ${ImagesDir}"/"${name}.tar.gz + echo "" + fi + images="" + name=$(echo "${image}" | sed 's/#//g' | sed -e 's/[[:space:]]//g') + ((index++)) + continue + fi + + image=$(echo "${image}" |tr -d '\r') + docker pull "${image}" + images=${images}" "${image} + + if [[ ${index} -eq ${ImagesListLen}-1 ]]; then + if [[ -n ${images} ]]; then + echo "" + echo "Save images: "${name}" to "${ImagesDir}"/"${name}".tar.gz <<<" + docker save ${images} | gzip -c > ${ImagesDir}"/"${name}.tar.gz + echo "" + fi + fi + ((index++)) + done +elif [ -n "${ImagesList}" ]; then + # docker load images + for image in $(ls ${ImagesDir}/*.tar.gz); do + echo "Load images: "${image}" <<<" + docker load < $image + done + + for image in $(<${ImagesList}); do + if [[ ${image} =~ ^\#\#.* ]]; then + continue + fi + # docker retag and push images + ImageName=${image##*/} + imageUrl=$registryUrl"/"$ImageName + image=$(echo "${image}" |tr -d '\r') + imageUrl=$(echo "${imageUrl}" |tr -d '\r') + docker tag $image $imageUrl + docker push $imageUrl + done +fi \ No newline at end of file