-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-resource.sh
70 lines (65 loc) · 2.44 KB
/
process-resource.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
run_timestamp=$(date +%s)
function process_resource() {
failoverIpCommand="$1"
targetServerIpCommand="$2"
dryRun="$3"
resourceName="$4"
echo "Processing resource $resourceName"
failoverIp=$(bash -c "$failoverIpCommand")
if [[ $? -ne 0 ]]; then
echo "$1 failed for $resourceName"
return 1
else
if [[ $failoverIp =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Got failover IP $failoverIp for $resourceName"
else
echo "$failoverIpCommand returns invalid id $failoverIp for $resourceName"
return 2
fi
fi
targetServerIp=$(bash -c "$targetServerIpCommand")
if [[ $? -ne 0 ]]; then
echo "$targetServerIpCommand failed for $resourceName"
return 3
else
if [[ $targetServerIp =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Got target IP $targetServerIp for $resourceName"
else
echo "$targetServerIpCommand returns invalid target IP $targetServerIp for $resourceName"
return 4
fi
fi
if [ "$dryRun" = true ]; then
echo "This is dry run - skip IP sync"
return 0
fi
currentFailoverStateFile=${TMPDIR%/}/fip/$run_timestamp
if [ -f "$currentFailoverStateFile" ]; then
echo "Reuse failover state from $currentFailoverStateFile"
else
echo "Fetch actual failover state into $currentFailoverStateFile."
curl -sS -f -u "$ROBOT_API_USERNAME:$ROBOT_API_PASSWORD" https://robot-ws.your-server.de/failover > "$currentFailoverStateFile"
if [[ $? -ne 0 ]]; then
echo "FATAL: unable to fetch current failover state from Robot API."
exit 1
fi
fi
currentFailoverTarget=$(jq -r ".[] | select(.failover.ip==\"${failoverIp}\").failover.active_server_ip" "$currentFailoverStateFile")
rm "$currentFailoverStateFile"
echo "$currentFailoverTarget"
if [ -z "$currentFailoverTarget" ]; then
echo "Failover IP $failoverIp not available on current account"
return 5
elif [ "$currentFailoverTarget" = "$targetServerIp" ]; then
echo "Failover IP $failoverIp already routed to configured target $targetServerIp"
return 0
else
echo "Failover IP $failoverIp needs to be routed to $targetServerIp instead of current $currentFailoverTarget"
fi
curl -sS -f -u "$ROBOT_API_USERNAME:$ROBOT_API_PASSWORD" https://robot-ws.your-server.de/failover/"$failoverIp" -d active_server_ip="$targetServerIp"
if [[ $? -ne 0 ]]; then
echo "Resource $resourceName processing failed"
else
echo "Resource $resourceName processed"
fi
}