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