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