#!/usr/bin/env bash
# 文件名: import_images.sh
# 功  能: 导入k8s集群镜像
# 作  者: 邱树辉<qiushuhui@oschina.cn>

set -o pipefail

txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}

image_path=$(mktemp -d)
imgSuccessful=0
imgFailed=0

if [[ "$@" < "1" ]]; then
    echo """
用法: $(basename $0) [选项]
选项:
  --target      [必填] 导入的目标, 支持: hub | local
  --registry    [选填] 私有镜像仓库地址, 缺省: hub.gitee.local:5000
  --repository  [选填] 私有镜像仓库导入的项目名，缺省: base-platform
  --quiet       [选填] 部署命名空间,若为空则使用gitee

使用案例

示例1. 将镜像导入到私有镜像仓库,地址为: hub.gitee.local
# bash $(basename $0) --target hub --registry hub.gitee.local

示例2. 导入到docker-daemon
# bash $(basename $0) --target local

"""
    exit 67
fi

# 读取命令行参数
ARGUMENT_LIST=(
    "target"
    "registry"
    "repository"
    "quite"
)

# 读取参数
opts=$(getopt \
    --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
    --name "$(basename "$0")" \
    --options "" \
    -- "$@"
)

eval set --$opts

while true; do
    case "$1" in
    --target)
        shift
        target=$1
        ;;
    --registry)
        shift
        registry=$1
        ;;
    --repository)
        shift
        repository=$1
        ;;
    --quite)
        shift
        quite=true
        ;;
      --)
        shift
        break
        ;;
    esac
    shift
done


if [[ -z $registry ]]; then
    registry=hub.gitee.local:5000
fi

if [[ -z $repository ]]; then
    repository=base-platform
fi

if [[ -z ${quite} ]]; then
# 输出当前部署信息
cat<< EOF
`echo -e " =========== 镜像导入指要 ============="`
`echo -e " - 导入目标: ${target:-hub}"`
`echo -e " - 仓库地址: ${registry}"`
`echo -e " - 项目名称: ${repository}"`
`echo -e " - 安静模式: False"`
`echo -e " ======================================"`
`echo -e ""`
EOF
read -p "确认无误后,请按Enter键进行部署..."
fi

sed -n -e '1,/^exit 0$/!p' $0 > ${image_path}/kubernetes-images-$(uname -m).tar.xz 2>/dev/null

tar axvf ${image_path}/kubernetes-images-$(uname -m).tar.xz -C ${image_path} >/dev/null

for image_file in $(ls ${image_path} | egrep ".tar$" | sed 's/.tar//')
do
    image=$(echo ${image_file##*/} | sed 's/@/:/')

    printf "$info: [ ${bldylw}${target}${txtrst} | ${bldylw}${image##*/}${txtrst} ] 镜像开始导入"

    if [[ "${target}" = "hub" ]]
    then
        skopeo copy docker-archive:${image_path}/${image_file}.tar docker://${registry}/${repository}/${image} >/dev/null 2>/dev/null || \
        skopeo copy --insecure-policy --dest-tls-verify=false docker-archive:${image_path}/${image_file}.tar docker://${registry}/${repository}/${image} >/dev/null 2>/dev/null

    elif [[ "${target}" = "local" ]]
    then
        skopeo copy docker-archive:${image_path}/${image_file}.tar docker-daemon:${registry}/${repository}/${image} >/dev/null 2>/dev/null
    fi

    if [[ "$?" -eq "0" ]]; then
        printf " | ${bldgre}√${txtrst}\n"
        let imgSuccessful=${imgSuccessful}+1
    else
        printf " | ${bldred}×${txtrst}\n"
        let imgFailed=${imgFailed}+1
    fi

done
printf "$info: 导入成功镜像数量: [ ${bldgre}${imgSuccessful}${txtrst} ]\n"

if [[ "${imgFailed}" != "0" ]]
then
    printf "$err: 导入失败镜像数量: [ ${bldred}${imgFailed}${txtrst} ]\n"
fi

rm -rf ${image_path}
exit 0
