Do not error when a deleted resource is not found
[icn.git] / deploy / metal3 / scripts / 03_verify_deprovisioning.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
5
6 eval "$(go env)"
7
8 source $LIBDIR/env/lib/common.sh
9
10 declare -i timeout=60
11 declare -i interval=60
12
13 function check_deprovisioned {
14     declare -i prev_host_state=0
15     declare -i j=0
16     echo "Baremetal state: 1 means deprovisioned & 0 means not yet deprovisioned"
17     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
18         declare -i current_host_state=0
19         if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
20             state=$(kubectl get baremetalhosts $name -n metal3 -o json | jq -r '.status.provisioning.state')
21         else
22             # When the named BareMetalHost is not found, assume its deprovisioned
23             state="ready"
24         fi
25         echo "Baremetal host metal3 state - "$name" : "$state
26
27         if [ "$state" == "ready" ];then
28             current_host_state=1
29         fi
30
31         echo "Baremetal $name     current_host_state : "$current_host_state
32         echo "Previous Baremetals prev_host_state    : "$prev_host_state
33
34         if [ $j -eq 0 ]; then
35             prev_host_state=$current_host_state
36             ((j+=1))
37             continue
38         fi
39
40         if [ $current_host_state -eq 1 ] && [ $prev_host_state -eq 1 ]; then
41             prev_host_state=1
42         else
43             prev_host_state=0
44         fi
45
46         echo "All Baremetal hosts aggregated state - prev_host_state:"$prev_host_state
47         ((j+=1))
48     done
49     return $prev_host_state
50 }
51
52 function warm_up_time {
53     echo "Wait for 240s for all baremetal hosts to reboot and network is up"
54     sleep 4m
55 }
56
57 function wait_for_deprovisioned {
58     declare -i k=1
59     while ((timeout > 0)); do
60         echo "Try $k iteration : Wait for $interval seconds to check all bmh state"
61         sleep $interval
62         if ! list_nodes | check_deprovisioned; then
63             echo "All the Baremetal hosts are deprovisioned - success"
64             warm_up_time
65             exit 0
66         fi
67         ((timeout-=1))
68         ((k+=1))
69     done
70     exit 1
71 }
72
73
74 function verify_bm_hosts_cleanup {
75     wait_for_deprovisioned
76 }
77
78 verify_bm_hosts_cleanup