Skip to content

antony@notes:~/rancher$ cat "準備-Rancher-Airgap-程式.md"

準備 Rancher Airgap 程式

2024-03-29· rancher

準備 Rancher Airgap 程式

setup_env() {
  # check internet connection
  if ! nc -vz google.com 443 &> /dev/null; then
    echo "internet connection is offline" && exit 1
  fi

  # check Command is installed
  for command in wget curl docker
  do
    if ! which $command &> /dev/null; then
      echo "${Command} command not found!" && exit 1
    fi
  done

  # make sure the version of the Rancher is defined
  if [[ -z "${Rancher_Version}" ]]; then
    Rancher_Version="v2.8.2"
  fi

  # make sure the version of the Helm is defined
  if [[ -z "${Helm_Version}" ]]; then
    Helm_Version="v3.14.2"
  fi

  # make sure the version of the Cert-Manager is defined
  if [[ -z "${Cert_Manager_Version}" ]]; then
    Cert_Manager_Version="v1.11.0"
  fi

  # make sure the name of the Private Images Registry is defined
  if [[ -z "${Private_Registry_Name}" ]]; then
    Private_Registry_Name="harbor.example.com"
  fi
}

prepare_rancher() {

  setup_env
  # 切換工作目錄
  cd ~/work/rancher/"${Rancher_Version}"

  # 安裝 helm
  curl -s https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
  [[ "$?" != "0" ]] && echo "Install helm failed" && exit 1

  # 新增並刷新 Rancher Prime 的 Helm Chart Repository
  helm repo add rancher-prime https://charts.rancher.com/server-charts/prime  && \
  helm repo update 
  [[ "$?" != "0" ]] && echo "helm repo add or update rancher-prime failed" && exit 1

  # 下載 Rancher chart
  helm fetch rancher-prime/rancher --version="${Rancher_Version}" 
  [[ "$?" != "0" ]] && echo "helm fetch rancher failed" && exit 1

  # 新增和刷新 cert-manager repo
  helm repo add jetstack https://charts.jetstack.io  && \
  helm repo update 
  [[ "$?" != "0" ]] && echo "helm repo add or update cert-manager failed" && exit 1

  # 下載 cert-manager chart
  helm fetch jetstack/cert-manager --version "${Cert_Manager_Version}" 
  [[ "$?" != "0" ]] && echo "helm fetch Cert_Manager failed" && exit 1

  # 下載 cert-manager 要求的 CRD
  curl -L -o cert-manager-crd.yaml https://github.com/cert-manager/cert-manager/releases/download/"${Cert_Manager_Version}"/cert-manager.crds.yaml 
  [[ "$?" != "0" ]] && echo "Download Cert_Manager CRD failed" && exit 1

  # 處理 cert-manager 的 Container Images
  for i in $(helm template cert-manager-*.tgz | awk '$1 ~ /image:/ {print $2}' | sed -e 's/\"//g')
  do
    # 下載 cert-manager 的 Container Images
    sudo docker pull "$i" 
    [[ "$?" != "0" ]] && echo "Pull quay.io/jetstack/$i Container images failed" && exit 1

    # 修改 cert-manager 的所有 Container Images Tag
    sudo docker tag "${i}" "${Private_Registry_Name}"/rancher/"${i##*/}" 
    [[ "$?" != "0" ]] && echo "tag ${Private_Registry_Name}/rancher/${i##*/} Container images failed" && exit 1
  done

  # 將 cert-manager 的所有 Container Images 打包成 .tar.gz 壓縮檔
  sudo docker save $(helm template cert-manager-*.tgz | awk '$1 ~ /image:/ {print $2}' | sed -e 's/\"//g' | sed "s|quay.io/jetstack|${Private_Registry_Name}/rancher|g") | gzip --stdout > cert-manager-image-"${Cert_Manager_Version}".tar.gz
  [[ "$?" != "0" ]] && echo "Docker save Cert-manager ${Cert_Manager_Version} images failed" && exit 1

  # 下載 Helm 壓縮檔
  wget -q https://get.helm.sh/helm-"${Helm_Version}"-linux-amd64.tar.gz -O helm-"${Helm_Version}"-linux-amd64.tar.gz 
  [[ "$?" != "0" ]] && echo "Download helm ${Helm_Version} failed"

  # 下載 Rancher Images List 文字檔及蒐集 Image 所需的 Shell Script
  for x in rancher-images.txt rancher-load-images.sh rancher-save-images.sh
  do
    wget -q https://github.com/rancher/rancher/releases/download/"${Rancher_Version}"/"${x}" 
    [[ "$?" != "0" ]] && echo "Download ${x} failed" && exit 1
  done

  # 對 Image List 進行排序和唯一化,以消除來源之間的任何重疊
  sort -u rancher-images.txt -o rancher-images.txt 

  [[ "$?" == "0" ]] && echo "Start pulling and saving rancher ${Rancher_Version} images in the background..."
  # 下載離線安裝 Rancher 所需的所有 Container Images 並打包成 rancher-images.tar.gz
  while read image
  do
    if ! sudo docker pull registry.rancher.com/"${image}" ; then
      echo pull "$image" failed && exit 1
    fi
  done <<< $(cat rancher-images.txt)

  rancher_all_image=$(cat rancher-images.txt | sed 's|^|registry.rancher.com/|' | tr '\n' ' ')
  for n in $rancher_all_image
  do
    if ! sudo docker images "$n" | grep -q "${n%:*}"; then
      if ! sudo docker pull registry.rancher.com/"$n" ; then
        echo pull "$n" failed twice && exit 1
      fi
    else
      sudo docker tag "${n}" "${Private_Registry_Name}"/rancher/"${n##*/}" 
      [[ "$?" != "0" ]] && echo "tag ${Private_Registry_Name}/rancher/${n##*/} Container images failed" && exit 1
    fi
  done

  rename_rancher_all_image=$(cat rancher-images.txt | sed "s|^|${Private_Registry_Name}/|" | tr '\n' ' ')
  sudo docker save $rename_rancher_all_image | gzip --stdout > rancher-"${Rancher_Version}"-image.tar.gz
  [[ (( $(stat -c%s rancher-"${Rancher_Version}"-image.tar.gz) -lt 50000000 )) ]] && echo "Docker Save rancher ${Rancher_Version} images failed" && exit 1

  cd ../..; tar -czf compressed_files/rancher-airgap-"${Rancher_Version}".tar.gz rancher/"${Rancher_Version}" 
  if [[ "$?" != "0" ]]; then
    echo "Preparing Rancher "${Rancher_Version}" Air Gap installer failed" && exit 1
  else
    echo "Prepare Rancher "${Rancher_Version}" OK."
  fi
}

完整程式 : https://github.com/braveantony/Prepare-AirGap-Installer/tree/main