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