#!/usr/bin/env bash

### 需要修改以下的内容  ###
#### 需要修改以下的内容  ###
#### 需要修改以下的内容  ###

cmlc_app_image_list="cmlc-app-images-4.1.6.txt"         # 需要修改版本
rancher_image_list="kubernetes-images-2.5.7-1.20.4.txt"  # 一般不需要修改
middleware_image_list="middleware-images.txt"     # 一般不需要修改
#DockerRegisterDomain="20.47.129.116:8033"     # 需要根据实际修改
DockerRegisterDomain="172.10.125.92:8033"     # 需要根据实际修改
HarborAdminPass=V2ryStr@ngPss                     # 需要跟第一脚本中的密码保持一致

#### 需要修改以上的内容  ###
#### 需要修改以上的内容  ###
#### 需要修改以上的内容  ###

downloadAllNeededImages() {
  while [[ $# > 0 ]]; do
    pulled=""
    while IFS= read -r i; do
      [ -z "${i}" ] && continue
      echo "开始下载：${i}"
      if docker pull "${i}" >/dev/null 2>&1; then
        echo "Image pull success: ${i}"
        pulled="${pulled} ${i}"
      else
        if docker inspect "${i}" >/dev/null 2>&1; then
          pulled="${pulled} ${i}"
        else
          echo "Image pull failed: ${i}"
        fi
      fi
      echo "-------------------------------------------------"
    done <"${1}"
    shift
  done
}

downloadAllNeededImagesAndCompress() {
  while [[ $# > 0 ]]; do
    pulled=""
    while IFS= read -r i; do
      [ -z "${i}" ] && continue
      echo "开始下载：${i}"
      if docker pull "${i}" >/dev/null 2>&1; then
        echo "Image pull success: ${i}"
        pulled="${pulled} ${i}"
      else
        if docker inspect "${i}" >/dev/null 2>&1; then
          pulled="${pulled} ${i}"
        else
          echo "Image pull failed: ${i}"
        fi
      fi
      echo "-------------------------------------------------"
    done <"${1}"
    compressPacName="$(echo ${1} | cut -d"." -f1).tar.gz"

    echo "Creating ${compressPacName} with $(echo ${pulled} | wc -w | tr -d '[:space:]') images"
    docker save $(echo ${pulled}) | gzip --stdout > ${compressPacName}

    shift
  done


  echo "已经完成打包工作！"
}

pushRKEImageToHarbor(){
  linux_images=()
  while IFS= read -r i; do
      [ -z "${i}" ] && continue
      linux_images+=("${i}");
  done < "${rancher_image_list}"

  docker login -u admin -p ${HarborAdminPass} ${DockerRegisterDomain}

  for i in "${linux_images[@]}"; do
      [ -z "${i}" ] && continue
      case $i in
      */*)
          image_name="${DockerRegisterDomain}/${i}"
          ;;
      *)
          image_name="${DockerRegisterDomain}/rancher/${i}"
          ;;
      esac

      echo "开始镜像至私有仓库推送：${image_name}"
      docker tag "${i}" "${image_name}"
      docker push "${image_name}"
      echo "-------------------------------------------------"
  done
}

pushCMLCAPPImageToHarbor(){
  app_images=()
  while IFS= read -r i; do
      [ -z "${i}" ] && continue
      app_images+=("${i}");
  done < "${cmlc_app_image_list}"

  docker login -u admin -p ${HarborAdminPass} ${DockerRegisterDomain}
  for app in "${app_images[@]}"; do
      [ -z "${app}" ] && continue
      image_name="${DockerRegisterDomain}/$(echo ${app} | cut -d"/" -f2-8)"
      echo "开始镜像至私有仓库推送：${image_name}"
      docker tag "${app}" "${image_name}"
      docker push "${image_name}"
      echo "-------------------------------------------------"
  done
}

pushMiddlewareImageToHarbor(){
  middleware_image=()
  while IFS= read -r i; do
      [ -z "${i}" ] && continue
      middleware_image+=("${i}");
  done < "${middleware_image_list}"

  docker login -u admin -p ${HarborAdminPass} ${DockerRegisterDomain}
  for app in "${middleware_image[@]}"; do
      [ -z "${app}" ] && continue
      case ${app} in
      */*/*)
          image_name="${DockerRegisterDomain}/cmii/$(echo "${app}" | cut -d"/" -f3-8)"
          ;;
      */*)
          image_name="${DockerRegisterDomain}/cmii/$(echo "${app}" | cut -d"/" -f2-8)"
          ;;
      esac

      echo "开始镜像至私有仓库推送：${image_name}"
      docker tag "${app}" "${image_name}"
      docker push "${image_name}"
      echo "-------------------------------------------------"
  done
}


#downloadAllNeededImagesAndCompress "${middleware_image_list}"
downloadAllNeededImages "${rancher_image_list}"

#pushRKEImageToHarbor
#pushCMLCAPPImageToHarbor
#pushMiddlewareImageToHarbor
