#!/usr/bin/env bash
# 文 件: img_to_local.sh
# 功 能: 快速将远镜像仓库的镜像迁移到本地
# 作 者: 树辉<shuhui@vqiu.cn>

set -o errexit
set -o pipefail

# set output color
NC='\033[0m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'

export images_file=${1:-images.list}
export local_registry=${2:-127.0.0.1:5000}

if [[ -e $images_file  ]]
then

    skopeo --version >/dev/null
    if [[ "$?" -ne "0" ]]
    then
        echo "skopeo工具不存在,下载中..."
        wget https://dl.vqiu.cn/package/skopeo/skopeo-linux-$(uname -m) -O /usr/local/bin/skopeo && chmod +x /usr/local/bin/skopeo
    fi

    for img in $(awk '{print $1=$1}'  images.list | grep -v "#")
    do
        skopeo copy --insecure-policy --dest-tls-verify=false docker://${img} docker://${local_registry}/${img#*/} &
    done
    wait
else
    echo "错误: $images_file 不存在,退出."
    exit 67
fi

