#!/bin/sh
#****************************************************************#
# ScriptName: install_images.sh
# Author: xhy
# Create Date: 2021-04-25 12:53
# Modify Author: xhy
# Modify Date: 2021-04-25 12:53
# Version: 
#***************************************************************#

KUBE_VERSION=v1.21.0
CALICO_VERSION=v3.18.1
CALICO_URL='docker.io/calico'
KUBE_PAUSE_VERSION=3.4.1
ETCD_VERSION=3.4.13-0
CORE_DNS_VERSION=v1.8.0
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
UCLOUD_URL=uhub.service.ucloud.cn/uxhy
QUAY_URL=quay.io
GCR_URL=k8s.gcr.io

mkdir -p dockerimages/

# config node hostname
export ALL_NAMES=(worker01)

kubeimages=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
)

corednsimages=(coredns:${CORE_DNS_VERSION}
)

for corednsimageName in ${corednsimages[@]} ; do
echo ${corednsimageName}
docker pull ${UCLOUD_URL}/${corednsimageName}
docker tag ${UCLOUD_URL}/${corednsimageName} ${GCR_URL}/coredns/${corednsimageName}
docker rmi ${UCLOUD_URL}/${corednsimageName}
docker save -o dockerimages/${corednsimageName}\.tar ${GCR_URL}/coredns/${corednsimageName}
done

for kubeimageName in ${kubeimages[@]} ; do
echo ${kubeimageName}
docker pull ${UCLOUD_URL}/${kubeimageName}
docker tag ${UCLOUD_URL}/${kubeimageName} ${GCR_URL}/${kubeimageName}
docker rmi ${UCLOUD_URL}/${kubeimageName}
docker save -o dockerimages/${kubeimageName}\.tar $GCR_URL/${kubeimageName}
done

allimages=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION}
)

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

for allimageName in ${allimages[@]}
  do
  for all_name in ${ALL_NAMES[@]}
    do
    echo "$allimageName copy to ${all_name}"
    ssh root@${all_name} "docker load -i dockerimages/$allimageName\.tar"
    done
  done
