#!/usr/bin/env bash
# 文件：images-push.sh
# 功能：将镜像推送至私有镜像仓库中心，依赖 skopeo 工具

image_hub=127.0.0.1:5000
image_prefix="base-platform/longhorn"
image_tar_file=longhorn-1.3.3-images.arm64.tar.xz

# 终端颜色定义
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}

declare -A images
images=(
    [longhorn-manager]="v1.3.3"
    [longhorn-ui]="v1.3.3"
    [longhorn-engine]="v1.3.3"
    [longhorn-instance-manager]="v1_20230407"
    [longhorn-share-manager]="v1_20230320"
    [backing-image-manager]="v3_20230320"
    [csi-attacher]="v3.4.0"
    [csi-provisioner]="v2.1.2"
    [csi-node-driver-registrar]="v2.5.0"
    [csi-resizer]="v1.2.0"
    [csi-snapshotter]="v3.0.3"
    [livenessprobe]="v2.8.0"
)
[[ -e images ]] || mkdir images

if [[ -e "$(realpath .)/${image_tar_file}" ]]
then
    printf "$info: 镜像压缩包解压中...\n"
    #tar axvf $(realpath .)/${image_tar_file} -C $(realpath .)/images  >/dev/null
else
    printf "$err: 镜像压缩包不存在...\n"
    exit 67
fi

# 镜像导入
for img in $(echo ${!images[*]})
do
    printf "$info: 镜像导入 [ ${bldgre}${img}${txtrst} ]"
    skopeo copy docker-archive:$(realpath .)/images/${img}.tar docker://${image_hub}/${image_prefix}/${img}:${images[$img]} >/dev/null 2>/dev/null || \
    skopeo copy --insecure-policy --dest-tls-verify=false docker-archive:$(realpath .)/images/${img}.tar docker://${image_hub}/${image_prefix}/${img}:${images[$img]} >/dev/null 2>/dev/null
    if [[ "$?" -eq "0" ]]; then
        printf " | ${bldgre}√${txtrst}\n"
        let imgSuccessful=${imgSuccessful}+1
    else
        printf " | ${bldred}×${txtrst}\n"
        let imgFailed=${imgFailed}+1
    fi
done

