You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.8 KiB

  1. #!/bin/bash
  2. # This file is part of Kargo.
  3. #
  4. # Foobar is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # Foobar is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  16. #color variables
  17. txtbld=$(tput bold) # Bold
  18. bldred=${txtbld}$(tput setaf 1) # red
  19. bldgre=${txtbld}$(tput setaf 2) # green
  20. bldylw=${txtbld}$(tput setaf 3) # yellow
  21. txtrst=$(tput sgr0) # Reset
  22. err=${bldred}ERROR${txtrst}
  23. info=${bldgre}INFO${txtrst}
  24. warn=${bldylw}WARNING${txtrst}
  25. usage()
  26. {
  27. cat << EOF
  28. Update ansible playbook with a specific kubernetes version
  29. Usage : $(basename $0) -v <k8s version>
  30. -h | --help : Show this message
  31. -i | --init : Initial upgrade (download binaries)
  32. -v | --version : Kubernetes version
  33. ex : switch to kubernetes v1.2.4
  34. $(basename $0) -v v1.2.4
  35. EOF
  36. }
  37. # Options parsing
  38. while (($#)); do
  39. case "$1" in
  40. -h | --help) usage; exit 0;;
  41. -i | --init) INIT=1; shift;;
  42. -v | --version) VERS=${2}; shift 2;;
  43. *)
  44. usage
  45. echo "ERROR : Unknown option"
  46. exit 3
  47. ;;
  48. esac
  49. done
  50. if [ -z ${VERS} ]; then
  51. usage
  52. echo -e "\n${err}: The option version must be defined"
  53. exit 3
  54. else
  55. if ! [[ ${VERS} =~ ^v[0-9]\.[0-9]\.[0-9]$ ]]; then
  56. echo -e "\n${err}: Invalid version format (ex: v1.2.4)"
  57. exit 1
  58. fi
  59. fi
  60. UPLOAD_VARFILE="roles/uploads/defaults/main.yml"
  61. DOWNLOAD_VARFILE="roles/download/defaults/main.yml"
  62. K8S_BIN="kubelet kubectl kube-apiserver"
  63. if [[ ${INIT} -eq 1 ]]; then
  64. DOWNLOAD_URL=https://storage.googleapis.com/kubernetes-release/release/${VERS}/bin/linux/amd64
  65. TMP_DIR=$(mktemp -d --tmpdir kubernetes_tmpbin_XXXXXXX)
  66. sed -i "s/^hyperkube_image_tag.*$/hyperkube_image_tag: \"${VERS}\"/" roles/kubernetes/node/defaults/main.yml
  67. trap 'rm -rf "${tmpdir}"' EXIT
  68. cd "${tmpdir}"
  69. for BIN in ${K8S_BIN}; do
  70. curl -s -o ${BIN} ${DOWNLOAD_URL}/${BIN}
  71. if [ $? -ne 0 ]; then
  72. echo -e "\n${err}: Downloading ${BIN} failed! Try again"
  73. exit 1
  74. else
  75. echo -e "\n${info}: ${BIN} downloaded successfuly"
  76. fi
  77. done
  78. for varfile in ${UPLOAD_VARFILE} ${DOWNLOAD_VARFILE}; do
  79. sed -i "s/^kube_version.*$/kube_version: \"${VERS}\"/" ${varfile}
  80. for BIN in ${K8S_BIN}; do
  81. CHECKSUM=$(sha256sum ${BIN} | cut -d' ' -f1)
  82. BIN=$(echo ${BIN} | tr '-' '_')
  83. sed -i "s/^${BIN}_checksum.*$/${BIN}_checksum: \"${CHECKSUM}\"/" ${varfile}
  84. done
  85. done
  86. rm -rf "${tmpdir}"
  87. else
  88. CHECKSUM_URL=https://storage.googleapis.com/kargo/${VERS}_k8s-sha256
  89. sed -i "s/^hyperkube_image_tag.*$/hyperkube_image_tag: \"${VERS}\"/" roles/kubernetes/node/defaults/main.yml
  90. for varfile in ${UPLOAD_VARFILE} ${DOWNLOAD_VARFILE}; do
  91. sed -i "s/^kube_version.*$/kube_version: \"${VERS}\"/" ${varfile}
  92. for BIN in ${K8S_BIN}; do
  93. if [[ "${BIN}" =~ "apiserver" ]]; then
  94. BIN="apiserver"
  95. fi
  96. line=$(curl -sk ${CHECKSUM_URL} | grep ${BIN})
  97. CHECKSUM=$(echo ${line} | cut -d':' -f2)
  98. if [[ "${BIN}" =~ "apiserver" ]]; then
  99. BIN="kube_apiserver"
  100. fi
  101. sed -i "s/^${BIN}_checksum.*$/${BIN}_checksum: \"${CHECKSUM}\"/" ${varfile}
  102. done
  103. done
  104. fi