onecloud-switch-edition.sh.j2 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. set -e
  3. export KUBECONFIG="{{ ENV_KUBECONFIG }}"
  4. K3S_CMDLINE_PREFIX="{{ K3S_CMDLINE_PREFIX }}"
  5. KUBECTL="$K3S_CMDLINE_PREFIX kubectl -n onecloud"
  6. TARGET_EDITION="{{ TARGET_EDITION }}"
  7. CLIMC="/opt/yunion/bin/climc"
  8. # UUID_REG='[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}'
  9. SVC_UUID_REG='[0-9a-f]{32}'
  10. OCBOOT_BK_DIR="/opt/yunion/ocboot/_switch_edition"
  11. source_auth_env() {
  12. if [[ -z "$K3S_CMDLINE_PREFIX" ]]; then
  13. source <(/opt/yunion/bin/ocadm cluster rcadmin)
  14. else
  15. source <(/opt/yunion/bin/fetch-rcadmin.sh)
  16. fi
  17. }
  18. check_svc() {
  19. local svc="$1"
  20. local status=$($KUBECTL get pods | grep "$svc" | grep -v Terminating | awk '{print $3}' | sort -u | xargs)
  21. if [[ "$status" == "Running" ]]; then
  22. echo "true"
  23. else
  24. echo "service pod $svc status is $status"
  25. echo "false"
  26. fi
  27. }
  28. wait_svc() {
  29. local svc="$1"
  30. local is_running=$(check_svc "$svc")
  31. sleep 1
  32. while true; do
  33. if [[ "$is_running" == "true" ]]; then
  34. echo "service pod $svc is running"
  35. return
  36. else
  37. echo "continue waiting service pod $svc, sleep 30s"
  38. is_running=$(check_svc "$svc")
  39. sleep 30
  40. fi
  41. done
  42. }
  43. clean_service_endpoint() {
  44. source_auth_env
  45. local service="$1"
  46. for ep in `$CLIMC endpoint-list --service "$service" | egrep "$SVC_UUID_REG" | awk '{print $2}'`; do
  47. echo "delete endpoint $ep of service $service"
  48. $CLIMC endpoint-update --disabled $ep
  49. $CLIMC endpoint-delete $ep
  50. done
  51. for svc in `$CLIMC service-list --name "$service" | egrep "$SVC_UUID_REG" | awk '{print $2}'`; do
  52. echo "delete service $service"
  53. $CLIMC service-update --disabled $svc
  54. $CLIMC service-delete $svc
  55. done
  56. }
  57. clean_compoent() {
  58. local k8s_res="$1"
  59. local k8s_name="$2"
  60. local cloud_svc="$3"
  61. $KUBECTL delete "$k8s_res" "$k8s_name" || true
  62. clean_service_endpoint "$cloud_svc"
  63. }
  64. backup_files() {
  65. local bk_dir="$OCBOOT_BK_DIR/$TARGET_EDITION-`date +%Y.%m.%d-%H:%M:%S`"
  66. local defaul_web_configmap_yml="$bk_dir/default-web-configmap.yml"
  67. local default_oc_yml="$bk_dir/oc.yml"
  68. mkdir -p $bk_dir
  69. echo "backup default-web configmap to $defaul_web_configmap_yml"
  70. $KUBECTL get configmap default-web -o yaml > $defaul_web_configmap_yml
  71. echo "backup default onecloudcluster to $defaul_web_configmap_yml"
  72. $KUBECTL get oc default -o yaml >$default_oc_yml
  73. }
  74. backup_files
  75. $KUBECTL annotate --overwrite onecloudcluster default onecloud.yunion.io/edition="$TARGET_EDITION"
  76. $KUBECTL patch onecloudcluster default --type='json' -p="[{'op': 'replace', 'path': '/spec/web/imageName', 'value': ''}]"
  77. $KUBECTL delete configmap default-web
  78. $KUBECTL rollout restart deployment default-web
  79. services=(
  80. default-web-
  81. default-apigateway
  82. default-yunionagent
  83. )
  84. if [[ "$TARGET_EDITION" == "ce" ]]; then
  85. services=(
  86. default-web-
  87. )
  88. fi
  89. if [[ "$TARGET_EDITION" == "ee" ]]; then
  90. services=(
  91. default-web-
  92. default-apigateway
  93. default-yunionagent
  94. default-itsm
  95. )
  96. fi
  97. for svc in ${@:-${services[@]}}; do
  98. echo "waiting service pod $svc to be running"
  99. wait_svc "$svc"
  100. done
  101. if [[ "$TARGET_EDITION" == "ce" ]]; then
  102. clean_compoent daemonset default-yunionagent yunionagent
  103. clean_compoent deployment default-meter meter
  104. clean_compoent deployment default-autoupdate autoupdate
  105. clean_compoent deployment default-itsm itsm
  106. fi
  107. if [[ "$TARGET_EDITION" == "ce/support" ]]; then
  108. clean_compoent deployment default-meter meter
  109. clean_compoent deployment default-autoupdate autoupdate
  110. clean_compoent deployment default-itsm itsm
  111. fi