Move wait_for to common.sh
[icn.git] / env / lib / common.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 DOWNLOAD_PATH=${DOWNLOAD_PATH:-/opt/icn}
5
6 #Ironic variables
7 IRONIC_IMAGE=${IRONIC_IMAGE:-"integratedcloudnative/ironic:v1.0-icn"}
8 IRONIC_INSPECTOR_IMAGE=${IRONIC_INSPECTOR_IMAGE:-"integratedcloudnative/ironic-inspector:v1.0-icn"}
9 IRONIC_BAREMETAL_IMAGE=${IRONIC_BAREMETAL_IMAGE:-"integratedcloudnative/baremetal-operator:v2.0-icn"}
10 IPA_DOWNLOADER_IMAGE=${IPA_DOWNLOADER_IMAGE:-"integratedcloudnative/ironic-ipa-downloader:v1.0-icn"}
11
12 IRONIC_DATA_DIR=${IRONIC_DATA_DIR:-"/opt/ironic"}
13 #IRONIC_PROVISIONING_INTERFACE is required to be provisioning, don't change it
14 IRONIC_INTERFACE=${IRONIC_INTERFACE:-}
15 IRONIC_PROVISIONING_INTERFACE=${IRONIC_PROVISIONING_INTERFACE:-"provisioning"}
16 IRONIC_IPMI_INTERFACE=${IRONIC_IPMI_INTERFACE:-}
17 IRONIC_PROVISIONING_INTERFACE_IP=${IRONIC_PROVISIONING_INTERFACE_IP:-"172.22.0.1"}
18 BM_IMAGE_URL=${BM_IMAGE_URL:-"https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"}
19 BM_IMAGE=${BM_IMAGE:-"bionic-server-cloudimg-amd64.img"}
20
21 #Baremetal operator repository URL
22 BMOREPO="${BMOREPO:-https://github.com/metal3-io/baremetal-operator.git}"
23 #Baremetal operator repository branch to checkout
24 BMOBRANCH="${BMOBRANCH:-10eb5aa3e614d0fdc6315026ebab061cbae6b929}"
25 #Discard existing baremetal operator repo directory
26 FORCE_REPO_UPDATE="${FORCE_REPO_UPDATE:-true}"
27
28 #refered from onap
29 function call_api {
30     #Runs curl with passed flags and provides
31     #additional error handling and debug information
32
33     #Function outputs server response body
34     #and performs validation of http_code
35
36     local status
37     local curl_response_file="$(mktemp -p /tmp)"
38     local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
39     local command=(curl "${curl_common_flags[@]}" "$@")
40
41     echo "[INFO] Running '${command[@]}'" >&2
42     if ! status="$("${command[@]}")"; then
43         echo "[ERROR] Internal curl error! '$status'" >&2
44         cat "${curl_response_file}"
45         rm "${curl_response_file}"
46         return 2
47     else
48         echo "[INFO] Server replied with status: ${status}" >&2
49         cat "${curl_response_file}"
50         rm "${curl_response_file}"
51         if [[ "${status:0:1}" =~ [45] ]]; then
52             return 1
53         else
54             return 0
55         fi
56     fi
57 }
58
59 function list_nodes {
60     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
61
62     if [ ! -f "$NODES_FILE" ]; then
63         exit 1
64     fi
65
66     # The boot MAC address must be specified when a port is included
67     # in the IPMI driver address (i.e when using the VirtualBMC
68     # controller).  Note that the below is a bit of a hack as it only
69     # checks the first entry in NODES_FILE for the port.
70     if cat "$NODES_FILE" |
71             jq -r '.nodes[0].ipmi_driver_info.address' | grep -c ':[0-9]\+$' >/dev/null; then
72         BOOT_LINK=$(cat "$NODES_FILE" |
73                         jq -r '.nodes[0].net.links | map(.id=="provisioning_nic") | index(true)')
74         cat "$NODES_FILE" |
75             jq -r --argjson BOOT_LINK $BOOT_LINK '.nodes[] | [
76                .name,
77                .ipmi_driver_info.username,
78                .ipmi_driver_info.password,
79                .ipmi_driver_info.address,
80                .net.links[$BOOT_LINK].ethernet_mac_address,
81                .os.username,
82                .os.password,
83                .os.image_name
84                ] | @csv' |
85             sed 's/"//g'
86     else
87         cat "$NODES_FILE" |
88             jq -r '.nodes[] | [
89                .name,
90                .ipmi_driver_info.username,
91                .ipmi_driver_info.password,
92                .ipmi_driver_info.address,
93                "",
94                .os.username,
95                .os.password,
96                .os.image_name
97                ] | @csv' |
98             sed 's/"//g'
99     fi
100 }
101
102 function node_networkdata {
103     name=$1
104
105     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
106
107     if [ ! -f "$NODES_FILE" ]; then
108         exit 1
109     fi
110
111     cat $NODES_FILE  | jq -r --arg name "$name" '.nodes[] | select(.name==$name) | .net'
112 }
113
114 function wait_for {
115     local -r interval=${WAIT_FOR_INTERVAL:-30s}
116     local -r max_tries=${WAIT_FOR_TRIES:-20}
117     local try=0
118     until "$@"; do
119         echo "[${try}/${max_tries}] - Waiting ${interval} for $*"
120         sleep ${interval}
121         try=$((try+1))
122         if [[ ${try} -ge ${max_tries} ]]; then
123             return 1
124         fi
125     done
126 }