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