upgrade_3_11.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env bash
  2. # 登录cloudpods控制节点获取mysql账号密码信息,填写到下面变量
  3. # kubectl get oc -n onecloud default -o yaml | grep -A 4 mysql
  4. #
  5. # 升级到3.11版本之后,等待所有华为云账号同步完成后,执行此升级脚本
  6. # 脚本原理如下:
  7. # 脚本会检索1天内deleted=1的华为云资源,通过external_id反查当前数据库中对应external_id相同且deleted=0的资源
  8. # 先会将deleted=1的资源id设为id=id-old, 再将deleted=0的资源id变更到deleted=1的资源id
  9. #
  10. HOST='127.0.0.1'
  11. USERNAME='root'
  12. PASSWORD=''
  13. DATETIME=''
  14. if ["$(uname)"=="Darwin"]; then
  15. DATETIME=$(date -v-1d -u "+%Y-%m-%dT%H:%M%SZ")
  16. else
  17. DATETIME=$(date -d "1 day ago" -u "+%Y-%m-%dT%H:%M%SZ")
  18. if
  19. function exec_sql_with_db() {
  20. export MYSQL_PWD=$PASSWORD
  21. local db=$1
  22. local sql=$2
  23. echo $(mysql -u"$USERNAME" -h "$HOST" -D"$db" -s -e "$sql")
  24. }
  25. function get_relation_tables() {
  26. local field=$1
  27. exec_sql_with_db "information_schema" "select table_name from columns where column_name='$field' and table_schema='yunioncloud'"
  28. }
  29. function exec_sql() {
  30. exec_sql_with_db "yunioncloud" "$1"
  31. }
  32. function uuid() {
  33. echo $1 | awk -F '-' '{print $1"-"$2"-"$3"-"$4"-"$5}'
  34. }
  35. function change_uuid() {
  36. local table=$1
  37. local target_id=$2
  38. uid=$(uuid $target_id)
  39. if [ "$uid" != "$target_id" ]; then
  40. target_id=$uid
  41. else
  42. exec_sql "update $table set id='$target_id-old' where id='$target_id'"
  43. fi
  44. echo $target_id
  45. }
  46. echo "upgrade guests_tbl"
  47. exec_sql "select name, id, external_id from guests_tbl where deleted=1 and length(external_id) > 0 and host_id in (select id from hosts_tbl where deleted=1 and manager_id in (select id from cloudproviders_tbl where deleted=1 and provider='Huawei' and deleted_at > '$DATETIME'))" | while read -r line; do
  48. info=($(echo $line | tr " ", "\n"))
  49. name=${info[0]}
  50. target_id=${info[1]}
  51. external_id=${info[2]}
  52. id=$(exec_sql "select id from guests_tbl where deleted=0 and external_id='$external_id' and host_id in (select id from hosts_tbl where deleted=0 and manager_id in (select id from cloudproviders_tbl where deleted=0 and provider='Huawei'))")
  53. if [ -n "$id" ]; then
  54. target_id=$(change_uuid "guests_tbl" $target_id)
  55. echo "change server $name id from $id => $target_id"
  56. exec_sql "update guests_tbl set id='$target_id' where id='$id' and deleted=0"
  57. tables=$(get_relation_tables "guest_id")
  58. for table in $tables; do
  59. exec_sql "update $table set guest_id='$target_id' where guest_id='$id' and deleted=0"
  60. done
  61. fi
  62. done
  63. echo "upgrade disks_tbl"
  64. exec_sql "select name, id, external_id from disks_tbl where deleted=1 and length(external_id) > 0 and storage_id in (select id from storages_tbl where deleted=1 and manager_id in (select id from cloudproviders_tbl where deleted=1 and provider='Huawei' and deleted_at > '$DATETIME'))" | while read -r line; do
  65. info=($(echo $line | tr " ", "\n"))
  66. name=${info[0]}
  67. target_id=${info[1]}
  68. external_id=${info[2]}
  69. id=$(exec_sql "select id from disks_tbl where deleted=0 and external_id='$external_id' and storage_id in (select id from storages_tbl where deleted=0 and manager_id in (select id from cloudproviders_tbl where deleted=0 and provider='Huawei'))")
  70. if [ -n "$id" ]; then
  71. target_id=$(change_uuid "disks_tbl" $target_id)
  72. echo "change disk $name id from $id => $target_id"
  73. exec_sql "update disks_tbl set id='$target_id' where id='$id' and deleted=0"
  74. tables=$(get_relation_tables "disk_id")
  75. for table in $tables; do
  76. exec_sql "update $table set disk_id='$target_id' where disk_id='$id' and deleted=0"
  77. done
  78. fi
  79. done
  80. echo "upgrade networks_tbl"
  81. exec_sql "select name, id, external_id from networks_tbl where deleted=1 and length(external_id) > 0 and wire_id in (select id from wires_tbl where deleted=1 and vpc_id in (select id from vpcs_tbl where deleted=1 and manager_id in (select id from cloudproviders_tbl where deleted=1 and provider='Huawei' and deleted_at > '$DATETIME')))" | while read -r line; do
  82. info=($(echo $line | tr " ", "\n"))
  83. name=${info[0]}
  84. target_id=${info[1]}
  85. external_id=${info[2]}
  86. id=$(exec_sql "select id from networks_tbl where deleted=0 and external_id='$external_id' and wire_id in (select id from wires_tbl where deleted=0 and vpc_id in (select id from vpcs_tbl where deleted=0 and manager_id in (select id from cloudproviders_tbl where deleted=0 and provider='Huawei')))")
  87. if [ -n "$id" ]; then
  88. target_id=$(change_uuid "networks_tbl" $target_id)
  89. echo "change network $name id from $id => $target_id"
  90. exec_sql "update networks_tbl set id='$target_id' where id='$id' and deleted=0"
  91. tables=$(get_relation_tables "network_id")
  92. for table in $tables; do
  93. if [ "$table" == "network_additional_wire_tbl" ]; then
  94. exec_sql "update $table set network_id='$target_id' where network_id='$id'"
  95. else
  96. exec_sql "update $table set network_id='$target_id' where network_id='$id' and deleted=0"
  97. fi
  98. done
  99. fi
  100. done
  101. function upgrade_managed_resources_table() {
  102. local res_type=$1
  103. local table_name=$2
  104. local field=$3
  105. echo "upgrade $table_name"
  106. exec_sql "select name, id, external_id from $table_name where deleted=1 and length(external_id) > 0 and manager_id in (select id from cloudproviders_tbl where deleted=1 and provider='Huawei' and deleted_at > '$DATETIME')" | while read -r line; do
  107. info=($(echo $line | tr " ", "\n"))
  108. name=${info[0]}
  109. target_id=${info[1]}
  110. external_id=${info[2]}
  111. id=$(exec_sql "select id from $table_name where deleted=0 and external_id='$external_id' and manager_id in (select id from cloudproviders_tbl where deleted=0 and provider='Huawei')")
  112. if [ -n "$id" ]; then
  113. target_id=$(change_uuid $table_name $target_id)
  114. echo "change $res_type $name id from $id => $target_id"
  115. exec_sql "update $table_name set id='$target_id' where id='$id' and deleted=0"
  116. tables=$(get_relation_tables $field)
  117. for table in $tables; do
  118. exec_sql "update $table set $field='$target_id' where $field='$id' and deleted=0"
  119. done
  120. fi
  121. done
  122. }
  123. upgrade_managed_resources_table "elasticcacheinstance" "elasticcacheinstances_tbl" "elasticcache_id"
  124. upgrade_managed_resources_table "dbinstance" "dbinstances_tbl" "dbinstance_id"
  125. upgrade_managed_resources_table "bucket" "buckets_tbl" "bucket_id"
  126. upgrade_managed_resources_table "vpc" "vpcs_tbl" "vpc_id"
  127. upgrade_managed_resources_table "cdn" "cdn_domains_tbl" "cdn_domain_id"
  128. upgrade_managed_resources_table "dns" "dnszones_tbl" "dns_zone_id"
  129. upgrade_managed_resources_table "elastic_search" "elastic_searchs_tbl" "elastic_search_id"
  130. upgrade_managed_resources_table "eip" "elasticips_tbl" "eip_id"
  131. upgrade_managed_resources_table "inter_vpc_network" "inter_vpc_networks_tbl" "inter_vpc_network_id"
  132. upgrade_managed_resources_table "kafka" "kafkas_tbl" "kafka_id"
  133. upgrade_managed_resources_table "loadbalancer" "loadbalancers_tbl" "loadbalancer_id"
  134. upgrade_managed_resources_table "snapshot" "snapshots_tbl" "snapshot_id"
  135. upgrade_managed_resources_table "sslcertificate" "sslcertificates_tbl" "sslcertificate_id"