-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrate.sh
118 lines (96 loc) · 3.85 KB
/
migrate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
if ! command -v jq &>/dev/null; then
echo "Error: jq is not installed. Please install it before running this script."
exit 1
fi
if ! command -v vfcli-ctrl &>/dev/null; then
echo "Error: vfcli-ctrl is not installed. Please install it before running this script."
exit 1
fi
CONCURRENCY=1
OFFLINE=""
SPEED=""
DRAIN_HV=false
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c) CONCURRENCY="$2"; shift; shift ;;
-o) OFFLINE="--force-offline"; shift ;;
-s) SPEED_VALUE_MiBs=$(( $2 / 8 )); SPEED="--xfer-speed=$SPEED_VALUE_MiBs"; shift; shift ;;
--drain-hv) DRAIN_HV=true; shift ;;
*) POSITIONAL+=("$1"); shift ;;
esac
done
set -- "${POSITIONAL[@]}"
if [[ ${#POSITIONAL[@]} -lt 2 ]]; then
cat <<EOL
$(tput bold)$(tput setaf 6)Script Usage:$(tput sgr0)
--------------
$(tput bold)Command:$(tput sgr0)
$0 [SOURCE_HYPERVISOR_ID] DESTINATION_HYPERVISOR_ID [SERVER_ID(s)] [-c <number>] [-o] [-s <speed in Mbps>] [--drain-hv]
$(tput bold)Options:$(tput sgr0)
$(tput setaf 2)-c <number>$(tput sgr0) Set concurrency. Determines how many VMs simultaneously migrate.
$(tput setaf 2)-o$(tput sgr0) Allows migration of offline VMs.
$(tput setaf 2)-s <speed in Mbps>$(tput sgr0) Set transfer speed limit in Mbps.
$(tput setaf 2)--drain-hv$(tput sgr0) Migrate all VMs from SOURCE_HYPERVISOR_ID.
$(tput bold)Examples:$(tput sgr0)
$0 SRC_ID DST_ID --drain-hv -c 5 -o -s 80 : Migrate all VMs from SRC_ID to DST_ID, 5 at a time, using offline migration with a speed limit of 80Mbps.
$0 DST_ID SERVER_ID : Migrate a single VM (SERVER_ID) to DST_ID without any concurrency.
$0 DST_ID SERVER_ID(s) -o : Migrate specific VMs to DST_ID, and if a VM is offline at the time of running, it'll migrate offline.
$0 DST_ID SERVER_ID(s) -s 40 : Migrate specific VMs to DST_ID at a limited speed of 40 Mbps.
$0 DST_ID SERVER_ID(s) -c 5 : Migrate specific VMs to DST_ID with concurrency, meaning 5 VMs will migrate simultaneously.
EOL
exit 1
fi
function migrate_server {
SERVER_ID=$1
DST_HV_ID=$2
MIGRATION_RESPONSE=$(vfcli-ctrl server:migrate-live start --server=$SERVER_ID --dst=$DST_HV_ID $OFFLINE $SPEED --json)
SUCCESS=$(echo "$MIGRATION_RESPONSE" | jq -r '.success')
ERRORS=$(echo "$MIGRATION_RESPONSE" | jq -r '.errors[]?')
if [[ "$SUCCESS" == "true" ]]; then
echo "Migration of server $SERVER_ID was successful!"
else
echo "Migration failed for server $SERVER_ID."
echo "Error: $ERRORS"
sleep 3
vfcli-ctrl server:migrate-live cancel --server=$SERVER_ID
exit 1
fi
}
if $DRAIN_HV; then
SRC_HV_ID=$1
DST_HV_ID=$2
SERVER_LIST_JSON=$(vfcli-ctrl server:migrate-live servers --src=$SRC_HV_ID --json)
if [[ -z "$SERVER_LIST_JSON" ]]; then
echo "Failed to retrieve the server list."
exit 1
fi
SERVER_IDS=$(echo "$SERVER_LIST_JSON" | jq -r '.servers[].id')
TOTAL_SERVERS=$(echo "$SERVER_IDS" | wc -w)
MIGRATED_COUNT=0
for SERVER_ID in $SERVER_IDS; do
MIGRATED_COUNT=$((MIGRATED_COUNT + 1))
echo "Migrating server with ID: $SERVER_ID ($MIGRATED_COUNT/$TOTAL_SERVERS)"
migrate_server $SERVER_ID $DST_HV_ID &
if (( MIGRATED_COUNT % CONCURRENCY == 0 )); then
wait
fi
done
wait
else
DST_HV_ID=$1
SERVER_IDS=("${@:2}")
TOTAL_SERVERS=${#SERVER_IDS[@]}
MIGRATED_COUNT=0
for SERVER_ID in "${SERVER_IDS[@]}"; do
MIGRATED_COUNT=$((MIGRATED_COUNT + 1))
echo "Migrating server with ID: $SERVER_ID ($MIGRATED_COUNT/$TOTAL_SERVERS)"
migrate_server $SERVER_ID $DST_HV_ID &
if (( MIGRATED_COUNT % CONCURRENCY == 0 )) || (( MIGRATED_COUNT == TOTAL_SERVERS )); then
wait
fi
done
wait
fi