#!/bin/sh
#****************************************************************#
# ScriptName: downrookimages.sh
# Author: xhy
# Create Date: 2021-04-15 14:03
# Modify Author: xhy
# Modify Date: 2021-04-20 01:05
# Version: 
#***************************************************************#
# Rook v1.6.0 images
CEPHCSI_VERSION=v3.3.0
CSINODE_VERSION=v2.0.1
CSIRES_VERSION=v1.0.1
CSIPRO_VERSION=v2.0.4
CSISNAP_VERSION=v3.0.2
CSIATT_VERSION=v3.0.2

UCLOUD_URL=uhub.service.ucloud.cn/uxhy
QUAY_URL=quay.io
GCR_URL=k8s.gcr.io/sig-storage

mkdir -p cephimages/

# 设置节点信息
export ALL_NAMES=(master02 master03 worker01 worker02 worker03)

rookimages01=(csi-node-driver-registrar:${CSINODE_VERSION}
csi-resizer:${CSIRES_VERSION}
csi-snapshotter:${CSISNAP_VERSION}
csi-provisioner:${CSIPRO_VERSION}
csi-attacher:${CSIATT_VERSION}
)

for rookimageName in ${rookimages01[@]} ; do
echo ${rookimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${rookimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${rookimageName} ${GCR_URL}/${rookimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${rookimageName}
ctr -n k8s.io images export cephimages/${rookimageName}\.tar ${GCR_URL}/${rookimageName}
done

ctr -n k8s.io images pull ${UCLOUD_URL}/cephcsi:${CEPHCSI_VERSION}
ctr -n k8s.io images tag ${UCLOUD_URL}/cephcsi:${CEPHCSI_VERSION} quay.io/cephcsi/cephcsi:${CEPHCSI_VERSION}
ctr -n k8s.io images rm ${UCLOUD_URL}/cephcsi:${CEPHCSI_VERSION}
ctr -n k8s.io images export cephimages/cephcsi:${CEPHCSI_VERSION}\.tar quay.io/cephcsi/cephcsi:${CEPHCSI_VERSION}

for all_name in ${ALL_NAMES[@]}
  do  
    echo ">>> ${all_name}"
    ssh root@${all_name} "mkdir /root/cephimages"
    scp -rp cephimages/* root@${all_name}:/root/cephimages/
  done

for rookimageName in ${rookimages01[@]}
  do
  for all_name in ${ALL_NAMES[@]}
    do
    echo "cephcsi:${CEPHCSI_VERSION} copy to ${all_name}"
    ssh root@${all_name} "ctr -n k8s.io images import cephimages/cephcsi:${CEPHCSI_VERSION}\.tar"
    
    echo "${rookimageName} copy to ${all_name}"
    ssh root@${all_name} "ctr -n k8s.io images import cephimages/${rookimageName}\.tar"
    done
  done
