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