2cd93d613c2cf1f455a718fa7a852af919578a2a
[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_PROVISIONING_INTERFACE_IP=${IRONIC_PROVISIONING_INTERFACE_IP:-"172.22.0.1"}
10 BM_IMAGE_URL=${BM_IMAGE_URL:-"https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"}
11 BM_IMAGE=${BM_IMAGE:-"focal-server-cloudimg-amd64.img"}
12
13 #Baremetal operator repository URL
14 BMOREPO="${BMOREPO:-https://github.com/metal3-io/baremetal-operator.git}"
15 #Path to clone the baremetal operator repo
16 BMOPATH="/opt/src/github.com/metal3-io/baremetal-operator"
17 #Bare Metal Operator version to use
18 #  If changing this, the value in deploy/ironic/icn/kustomization.yaml
19 #  must also be changed
20 BMO_VERSION="capm3-v0.5.4"
21
22 #KuD repository URL
23 KUDREPO="${KUDREPO:-https://github.com/onap/multicloud-k8s.git}"
24 #Path to clone the KuD repo
25 KUDPATH="/opt/src/github.com/onap/multicloud-k8s"
26 #KuD version to use
27 KUD_VERSION="8157bf63753839ce4e9006978816fad3f63ca2de"
28
29 #EMCO repository URL
30 EMCOREPO="${EMCOREPO:-https://gitlab.com/project-emco/core/emco-base.git}"
31 #Path to clone the EMCO repo
32 EMCOPATH="/opt/src/emco-base"
33 #EMCO version to use
34 EMCO_VERSION="v21.12"
35
36 #Discard existing repo directory
37 FORCE_REPO_UPDATE="${FORCE_REPO_UPDATE:-false}"
38
39 # The kustomize version to use
40 KUSTOMIZE_VERSION="v4.5.2"
41
42 #Cluster API version to use
43 CAPI_VERSION="v0.4.7"
44
45 #Cluster API version to use
46 CAPM3_VERSION="v0.5.4"
47
48 #The flux version to use
49 FLUX_VERSION="0.27.0"
50
51 #The sops version to use
52 SOPS_VERSION="v3.7.1"
53
54 #Cert-Manager version to use
55 CERT_MANAGER_VERSION="v1.7.1"
56
57 #CNI versions to use in cluster chart
58 CALICO_VERSION="v3.22.0"
59 FLANNEL_VERSION="v0.16.3"
60
61 #Kata version to use
62 KATA_VERSION="2.3.2"
63 KATA_WEBHOOK_VERSION="2.3.2"
64
65 #The kubectl version to install when KuD is not used to deploy the
66 #jump server K8s cluster
67 KUBECTL_VERSION="v1.20.7"
68
69 #The yq version to use
70 YQ_VERSION="v4.20.1"
71
72 #Istio repository URL
73 ISTIOREPO="${ISTIOREPO:-https://github.com/istio/istio.git}"
74 #Path to clone the Istio repo
75 ISTIOPATH="/opt/src/istio"
76 #Istio version to use
77 ISTIO_VERSION="1.10.3"
78
79 #Addon versions
80 CDI_VERSION="v1.44.1"
81 CPU_MANAGER_VERSION="v1.4.1"
82 KUBEVIRT_VERSION="v0.50.0"
83 MULTUS_VERSION="v3.8"
84 NODUS_VERSION="dd9985e5be010b764b324b57c1afe985a59abf68"
85 QAT_VERSION="v0.23.0"
86
87 #refered from onap
88 function call_api {
89     #Runs curl with passed flags and provides
90     #additional error handling and debug information
91
92     #Function outputs server response body
93     #and performs validation of http_code
94
95     local status
96     local curl_response_file="$(mktemp -p /tmp)"
97     local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
98     local command=(curl "${curl_common_flags[@]}" "$@")
99
100     echo "[INFO] Running '${command[@]}'" >&2
101     if ! status="$("${command[@]}")"; then
102         echo "[ERROR] Internal curl error! '$status'" >&2
103         cat "${curl_response_file}"
104         rm "${curl_response_file}"
105         return 2
106     else
107         echo "[INFO] Server replied with status: ${status}" >&2
108         cat "${curl_response_file}"
109         rm "${curl_response_file}"
110         if [[ "${status:0:1}" =~ [45] ]]; then
111             return 1
112         else
113             return 0
114         fi
115     fi
116 }
117
118 function list_nodes {
119     if [ ! -f "$NODES_FILE" ]; then
120         exit 1
121     fi
122
123     # The boot MAC address must be specified when a port is included
124     # in the IPMI driver address (i.e when using the VirtualBMC
125     # controller).  Note that the below is a bit of a hack as it only
126     # checks the first entry in NODES_FILE for the port.
127     if cat "$NODES_FILE" |
128             jq -r '.nodes[0].ipmi_driver_info.address' | grep -c ':[0-9]\+$' >/dev/null; then
129         BOOT_LINK=$(cat "$NODES_FILE" |
130                         jq -r '.nodes[0].net.links | map(.id=="provisioning_nic") | index(true)')
131         cat "$NODES_FILE" |
132             jq -r --argjson BOOT_LINK $BOOT_LINK '.nodes[] | [
133                .name,
134                .ipmi_driver_info.username,
135                .ipmi_driver_info.password,
136                .ipmi_driver_info.address,
137                .net.links[$BOOT_LINK].ethernet_mac_address,
138                .os.username,
139                .os.password,
140                .os.image_name
141                ] | @csv' |
142             sed 's/"//g'
143     else
144         cat "$NODES_FILE" |
145             jq -r '.nodes[] | [
146                .name,
147                .ipmi_driver_info.username,
148                .ipmi_driver_info.password,
149                .ipmi_driver_info.address,
150                "",
151                .os.username,
152                .os.password,
153                .os.image_name
154                ] | @csv' |
155             sed 's/"//g'
156     fi
157 }
158
159 function node_userdata {
160     name="$1"
161     username="$2"
162     password="$3"
163     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
164
165     # validate that the user isn't expecting the deprecated
166     # COMPUTE_NODE_PASSWORD to be used
167     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
168         cat <<EOF
169 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
170 Unset COMPUTE_NODE_PASSWORD and retry.
171 EOF
172         exit 1
173     fi
174
175     printf "userData:\n"
176     if [ -n "$username" ]; then
177         printf "  name: ${username}\n"
178     fi
179     if [ -n "$password" ]; then
180         passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
181         printf "  hashedPassword: ${passwd}\n"
182     fi
183
184     if [ -n "$COMPUTE_NODE_FQDN" ]; then
185         printf "  fqdn: ${COMPUTE_NODE_FQDN}\n"
186     fi
187
188     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
189         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
190     fi
191
192     printf "  sshAuthorizedKey: $(cat $HOME/.ssh/id_rsa.pub)\n"
193 }
194
195 # Returns "null" when the field is not present
196 function networkdata_networks_field {
197     name=$1
198     network=$2
199     field=$3
200     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}
201 }
202
203 # Returns "null" when the field is not present
204 function networkdata_links_field {
205     name=$1
206     link=$2
207     field=$3
208     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}
209 }
210
211 function node_networkdata {
212     name=$1
213
214     if [ ! -f "$NODES_FILE" ]; then
215         exit 1
216     fi
217
218     printf "networks:\n"
219     for network in $(cat $NODES_FILE | jq -r --arg name "$name" '.nodes[] | select(.name==$name) | .net.networks[].id'); do
220         link=$(networkdata_networks_field $name $network "link")
221         type=$(networkdata_networks_field $name $network "type")
222         mac=$(networkdata_links_field $name $link "ethernet_mac_address")
223
224         # Optional values
225         ip_address=$(networkdata_networks_field $name $network "ip_address")
226         gateway=$(networkdata_networks_field $name $network "gateway")
227         dns_nameservers=$(networkdata_networks_field $name $network "dns_nameservers")
228
229         printf "  ${network}:\n"
230         printf "    macAddress: ${mac}\n"
231         printf "    type: ${type}\n"
232         if [[ $ip_address != "null" ]]; then
233             printf "    ipAddress: ${ip_address}\n"
234         fi
235         if [[ $gateway != "null" ]]; then
236             printf "    gateway: ${gateway}\n"
237         fi
238         if [[ $dns_nameservers != "null" ]]; then
239             printf "    nameservers: ${dns_nameservers}\n"
240         fi
241     done
242 }
243
244 function wait_for {
245     local -r interval=${WAIT_FOR_INTERVAL:-30s}
246     local -r max_tries=${WAIT_FOR_TRIES:-20}
247     local try=0
248     until "$@"; do
249         echo "[${try}/${max_tries}] - Waiting ${interval} for $*"
250         sleep ${interval}
251         try=$((try+1))
252         if [[ ${try} -ge ${max_tries} ]]; then
253             return 1
254         fi
255     done
256 }
257
258 # This is intended to be used together with list_nodes in the
259 # following way:
260 #   list_nodes | while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do ...
261 function node_machine_values {
262     printf "machineName: ${name}\n"
263     printf "machineLabels:\n"
264     printf "  machine: ${name}\n"
265     printf "bmcUsername: ${ipmi_username}\n"
266     printf "bmcPassword: ${ipmi_password}\n"
267     printf "bmcAddress: ipmi://${ipmi_address}\n"
268     if [[ ! -z ${boot_mac} ]]; then
269         printf "bootMACAddress: ${boot_mac}\n"
270     fi
271     printf "imageName: ${BM_IMAGE}\n"
272     node_userdata ${name} ${os_username} ${os_password}
273     node_networkdata ${name}
274 }
275
276 function clone_repository {
277     local -r path=$1
278     local -r repo=$2
279     local -r version=$3
280     mkdir -p $(dirname ${path})
281     if [[ -d ${path} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
282        rm -rf "${path}"
283     fi
284     if [ ! -d "${path}" ] ; then
285         pushd $(dirname ${path})
286         git clone "${repo}"
287         popd
288     else
289        pushd "${path}"
290        git fetch
291        popd
292     fi
293     pushd "${path}"
294     git reset --hard "${version}"
295     popd
296 }
297
298 function clone_baremetal_operator_repository {
299     clone_repository ${BMOPATH} ${BMOREPO} ${BMO_VERSION}
300 }
301
302 function clone_kud_repository {
303     clone_repository ${KUDPATH} ${KUDREPO} ${KUD_VERSION}
304 }
305
306 function clone_emco_repository {
307     clone_repository ${EMCOPATH} ${EMCOREPO} ${EMCO_VERSION}
308 }
309
310 function clone_istio_repository {
311     clone_repository ${ISTIOPATH} ${ISTIOREPO} ${ISTIO_VERSION}
312 }
313
314 function fetch_image {
315     if [[ "${BM_IMAGE_URL}" && "${BM_IMAGE}" ]]; then
316        mkdir -p "${IRONIC_DATA_DIR}/html/images"
317        pushd ${IRONIC_DATA_DIR}/html/images
318        local_checksum="0"
319        if [[ -f "${BM_IMAGE}" ]]; then
320            local_checksum=$(md5sum ${BM_IMAGE} | awk '{print $1}')
321        fi
322        remote_checksum=$(curl -sL "$(dirname ${BM_IMAGE_URL})/MD5SUMS" | grep ${BM_IMAGE} | awk '{print $1}')
323        if [[ ${local_checksum} != ${remote_checksum} ]]; then
324             curl -o ${BM_IMAGE} --insecure --compressed -O -L ${BM_IMAGE_URL}
325             md5sum ${BM_IMAGE} | awk '{print $1}' > ${BM_IMAGE}.md5sum
326        fi
327        popd
328     fi
329 }