-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/env bash | ||
|
||
HOST='127.0.0.1' | ||
USERNAME='root' | ||
PASSWORD='' | ||
DATETIME=$(date -v-10d -u "+%Y-%m-%dT%H:%M%SZ") | ||
echo $DATETIME | ||
|
||
function exec_sql_with_db() { | ||
export MYSQL_PWD=$PASSWORD | ||
local db=$1 | ||
local sql=$2 | ||
echo $(mysql -u"$USERNAME" -h "$HOST" -D"$db" -s -e "$sql") | ||
} | ||
|
||
function get_relation_tables() { | ||
local field=$1 | ||
exec_sql_with_db "information_schema" "select table_name from columns where column_name='$field' and table_schema='yunioncloud'" | ||
} | ||
|
||
function exec_sql() { | ||
exec_sql_with_db "yunioncloud" "$1" | ||
} | ||
|
||
function uuid() { | ||
echo $1 | awk -F '-' '{print $1"-"$2"-"$3"-"$4"-"$5}' | ||
} | ||
|
||
function change_uuid() { | ||
local table=$1 | ||
local target_id=$2 | ||
|
||
uid=$(uuid $target_id) | ||
if [ "$uid" != "$target_id" ]; then | ||
target_id=$uid | ||
else | ||
exec_sql "update $table set id='$target_id-old' where id='$target_id'" | ||
fi | ||
echo $target_id | ||
} | ||
|
||
|
||
echo "upgrade guests_tbl" | ||
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 | ||
info=($(echo $line | tr " ", "\n")) | ||
name=${info[0]} | ||
target_id=${info[1]} | ||
external_id=${info[2]} | ||
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'))") | ||
if [ -n "$id" ]; then | ||
target_id=$(change_uuid "guests_tbl" $target_id) | ||
echo "change server $name id from $id => $target_id" | ||
|
||
exec_sql "update guests_tbl set id='$target_id' where id='$id' and deleted=0" | ||
|
||
tables=$(get_relation_tables "guest_id") | ||
for table in $tables; do | ||
exec_sql "update $table set guest_id='$target_id' where guest_id='$id' and deleted=0" | ||
done | ||
|
||
fi | ||
|
||
done | ||
|
||
echo "upgrade networks_tbl" | ||
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 | ||
info=($(echo $line | tr " ", "\n")) | ||
name=${info[0]} | ||
target_id=${info[1]} | ||
external_id=${info[2]} | ||
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')))") | ||
if [ -n "$id" ]; then | ||
target_id=$(change_uuid "networks_tbl" $target_id) | ||
echo "change network $name id from $id => $target_id" | ||
|
||
exec_sql "update networks_tbl set id='$target_id' where id='$id' and deleted=0" | ||
|
||
tables=$(get_relation_tables "network_id") | ||
for table in $tables; do | ||
if [ "$table" == "network_additional_wire_tbl" ]; then | ||
exec_sql "update $table set network_id='$target_id' where network_id='$id'" | ||
else | ||
exec_sql "update $table set network_id='$target_id' where network_id='$id' and deleted=0" | ||
fi | ||
done | ||
|
||
fi | ||
done | ||
|
||
|
||
function upgrade_managed_resources_table() { | ||
local res_type=$1 | ||
local table_name=$2 | ||
local field=$3 | ||
|
||
echo "upgrade $table_name" | ||
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 | ||
info=($(echo $line | tr " ", "\n")) | ||
name=${info[0]} | ||
target_id=${info[1]} | ||
external_id=${info[2]} | ||
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')") | ||
if [ -n "$id" ]; then | ||
target_id=$(change_uuid $table_name $target_id) | ||
echo "change $res_type $name id from $id => $target_id" | ||
|
||
exec_sql "update $table_name set id='$target_id' where id='$id' and deleted=0" | ||
|
||
tables=$(get_relation_tables $field) | ||
for table in $tables; do | ||
exec_sql "update $table set $field='$target_id' where $field='$id' and deleted=0" | ||
done | ||
fi | ||
done | ||
} | ||
|
||
|
||
upgrade_managed_resources_table "elasticcacheinstance" "elasticcacheinstances_tbl" "elasticcache_id" | ||
upgrade_managed_resources_table "dbinstance" "dbinstances_tbl" "dbinstance_id" | ||
upgrade_managed_resources_table "bucket" "buckets_tbl" "bucket_id" | ||
upgrade_managed_resources_table "vpc" "vpcs_tbl" "vpc_id" | ||
upgrade_managed_resources_table "cdn" "cdn_domains_tbl" "cdn_domain_id" | ||
upgrade_managed_resources_table "dns" "dnszones_tbl" "dns_zone_id" | ||
upgrade_managed_resources_table "elastic_search" "elastic_searchs_tbl" "elastic_search_id" | ||
upgrade_managed_resources_table "eip" "elasticips_tbl" "eip_id" | ||
upgrade_managed_resources_table "inter_vpc_network" "inter_vpc_networks_tbl" "inter_vpc_network_id" | ||
upgrade_managed_resources_table "kafka" "kafkas_tbl" "kafka_id" | ||
upgrade_managed_resources_table "loadbalancer" "loadbalancers_tbl" "loadbalancer_id" | ||
upgrade_managed_resources_table "snapshot" "snapshots_tbl" "snapshot_id" | ||
upgrade_managed_resources_table "sslcertificate" "sslcertificates_tbl" "sslcertificate_id" | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.