Capture creation of BMH resources into Helm chart
[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_DATA_DIR=${IRONIC_DATA_DIR:-"/opt/ironic"}
7 #IRONIC_PROVISIONING_INTERFACE is required to be provisioning, don't change it
8 IRONIC_INTERFACE=${IRONIC_INTERFACE:-}
9 IRONIC_PROVISIONING_INTERFACE=${IRONIC_PROVISIONING_INTERFACE:-"provisioning"}
10 IRONIC_IPMI_INTERFACE=${IRONIC_IPMI_INTERFACE:-}
11 IRONIC_PROVISIONING_INTERFACE_IP=${IRONIC_PROVISIONING_INTERFACE_IP:-"172.22.0.1"}
12 BM_IMAGE_URL=${BM_IMAGE_URL:-"https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"}
13 BM_IMAGE=${BM_IMAGE:-"bionic-server-cloudimg-amd64.img"}
14
15 #Baremetal operator repository URL
16 BMOREPO="${BMOREPO:-https://github.com/metal3-io/baremetal-operator.git}"
17 #Path to clone the baremetal operator repo
18 BMOPATH="/opt/src/github.com/metal3-io/baremetal-operator"
19 #Bare Metal Operator version to use
20 BMO_VERSION="capm3-v0.5.1"
21 #Discard existing baremetal operator repo directory
22 FORCE_REPO_UPDATE="${FORCE_REPO_UPDATE:-true}"
23
24 # The kustomize version to use
25 KUSTOMIZE_VERSION="v4.3.0"
26
27 #refered from onap
28 function call_api {
29     #Runs curl with passed flags and provides
30     #additional error handling and debug information
31
32     #Function outputs server response body
33     #and performs validation of http_code
34
35     local status
36     local curl_response_file="$(mktemp -p /tmp)"
37     local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
38     local command=(curl "${curl_common_flags[@]}" "$@")
39
40     echo "[INFO] Running '${command[@]}'" >&2
41     if ! status="$("${command[@]}")"; then
42         echo "[ERROR] Internal curl error! '$status'" >&2
43         cat "${curl_response_file}"
44         rm "${curl_response_file}"
45         return 2
46     else
47         echo "[INFO] Server replied with status: ${status}" >&2
48         cat "${curl_response_file}"
49         rm "${curl_response_file}"
50         if [[ "${status:0:1}" =~ [45] ]]; then
51             return 1
52         else
53             return 0
54         fi
55     fi
56 }
57
58 function list_nodes {
59     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
60
61     if [ ! -f "$NODES_FILE" ]; then
62         exit 1
63     fi
64
65     # The boot MAC address must be specified when a port is included
66     # in the IPMI driver address (i.e when using the VirtualBMC
67     # controller).  Note that the below is a bit of a hack as it only
68     # checks the first entry in NODES_FILE for the port.
69     if cat "$NODES_FILE" |
70             jq -r '.nodes[0].ipmi_driver_info.address' | grep -c ':[0-9]\+$' >/dev/null; then
71         BOOT_LINK=$(cat "$NODES_FILE" |
72                         jq -r '.nodes[0].net.links | map(.id=="provisioning_nic") | index(true)')
73         cat "$NODES_FILE" |
74             jq -r --argjson BOOT_LINK $BOOT_LINK '.nodes[] | [
75                .name,
76                .ipmi_driver_info.username,
77                .ipmi_driver_info.password,
78                .ipmi_driver_info.address,
79                .net.links[$BOOT_LINK].ethernet_mac_address,
80                .os.username,
81                .os.password,
82                .os.image_name
83                ] | @csv' |
84             sed 's/"//g'
85     else
86         cat "$NODES_FILE" |
87             jq -r '.nodes[] | [
88                .name,
89                .ipmi_driver_info.username,
90                .ipmi_driver_info.password,
91                .ipmi_driver_info.address,
92                "",
93                .os.username,
94                .os.password,
95                .os.image_name
96                ] | @csv' |
97             sed 's/"//g'
98     fi
99 }
100
101 # Returns "null" when the field is not present
102 function networkdata_networks_field {
103     name=$1
104     network=$2
105     field=$3
106     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
107     cat $NODES_FILE | jq -c -r --arg name "$name" --arg network "$network" --arg field "$field" '.nodes[] | select(.name==$name) | .net.networks[] | select(.id==$network).'${field}
108 }
109
110 # Returns "null" when the field is not present
111 function networkdata_links_field {
112     name=$1
113     link=$2
114     field=$3
115     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
116     cat $NODES_FILE | jq -c -r --arg name "$name" --arg link "$link" --arg field "$field" '.nodes[] | select(.name==$name) | .net.links[] | select(.id==$link).'${field}
117 }
118
119 function node_networkdata {
120     name=$1
121
122     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
123
124     if [ ! -f "$NODES_FILE" ]; then
125         exit 1
126     fi
127
128     printf "    networks:\n"
129     for network in $(cat $NODES_FILE | jq -r --arg name "$name" '.nodes[] | select(.name==$name) | .net.networks[].id'); do
130         link=$(networkdata_networks_field $name $network "link")
131         type=$(networkdata_networks_field $name $network "type")
132         mac=$(networkdata_links_field $name $link "ethernet_mac_address")
133
134         # Optional values
135         ip_address=$(networkdata_networks_field $name $network "ip_address")
136         gateway=$(networkdata_networks_field $name $network "gateway")
137         dns_nameservers=$(networkdata_networks_field $name $network "dns_nameservers")
138
139         printf "      ${network}:\n"
140         printf "        macAddress: ${mac}\n"
141         printf "        type: ${type}\n"
142         if [[ $ip_address != "null" ]]; then
143             printf "        ipAddress: ${ip_address}\n"
144         fi
145         if [[ $gateway != "null" ]]; then
146             printf "        gateway: ${gateway}\n"
147         fi
148         if [[ $dns_nameservers != "null" ]]; then
149             printf "        nameservers: ${dns_nameservers}\n"
150         fi
151     done
152 }
153
154 function wait_for {
155     local -r interval=${WAIT_FOR_INTERVAL:-30s}
156     local -r max_tries=${WAIT_FOR_TRIES:-20}
157     local try=0
158     until "$@"; do
159         echo "[${try}/${max_tries}] - Waiting ${interval} for $*"
160         sleep ${interval}
161         try=$((try+1))
162         if [[ ${try} -ge ${max_tries} ]]; then
163             return 1
164         fi
165     done
166 }
167
168 function clone_baremetal_operator_repository {
169     mkdir -p $(dirname ${BMOPATH})
170     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
171        rm -rf "${BMOPATH}"
172     fi
173     if [ ! -d "${BMOPATH}" ] ; then
174         pushd $(dirname ${BMOPATH})
175         git clone "${BMOREPO}"
176         popd
177     else
178        pushd "${BMOPATH}"
179        git fetch
180        popd
181     fi
182     pushd "${BMOPATH}"
183     git reset --hard "${BMO_VERSION}"
184     popd
185 }
186
187 function install_kustomize {
188     curl -sL "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2F${KUSTOMIZE_VERSION}/kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz" -o kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz
189     tar xzf kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz --no-same-owner
190     sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
191     rm kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz kustomize
192     kustomize version
193 }
194
195 function fetch_image {
196     if [[ "${BM_IMAGE_URL}" && "${BM_IMAGE}" ]]; then
197        mkdir -p "${IRONIC_DATA_DIR}/html/images"
198        pushd ${IRONIC_DATA_DIR}/html/images
199        local_checksum="0"
200        if [[ -f "${BM_IMAGE}" ]]; then
201            local_checksum=$(md5sum ${BM_IMAGE} | awk '{print $1}')
202        fi
203        remote_checksum=$(curl -sL "$(dirname ${BM_IMAGE_URL})/MD5SUMS" | grep ${BM_IMAGE} | awk '{print $1}')
204        if [[ ${local_checksum} != ${remote_checksum} ]]; then
205             curl -o ${BM_IMAGE} --insecure --compressed -O -L ${BM_IMAGE_URL}
206             md5sum ${BM_IMAGE} | awk '{print $1}' > ${BM_IMAGE}.md5sum
207        fi
208        popd
209     fi
210 }