Merge "Upgrade ironic, baremetal-operator, cluster-api"
[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.3.0"
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.25.3"
50
51 #The sops version to use
52 SOPS_VERSION="v3.7.1"
53
54 #refered from onap
55 function call_api {
56     #Runs curl with passed flags and provides
57     #additional error handling and debug information
58
59     #Function outputs server response body
60     #and performs validation of http_code
61
62     local status
63     local curl_response_file="$(mktemp -p /tmp)"
64     local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
65     local command=(curl "${curl_common_flags[@]}" "$@")
66
67     echo "[INFO] Running '${command[@]}'" >&2
68     if ! status="$("${command[@]}")"; then
69         echo "[ERROR] Internal curl error! '$status'" >&2
70         cat "${curl_response_file}"
71         rm "${curl_response_file}"
72         return 2
73     else
74         echo "[INFO] Server replied with status: ${status}" >&2
75         cat "${curl_response_file}"
76         rm "${curl_response_file}"
77         if [[ "${status:0:1}" =~ [45] ]]; then
78             return 1
79         else
80             return 0
81         fi
82     fi
83 }
84
85 function list_nodes {
86     if [ ! -f "$NODES_FILE" ]; then
87         exit 1
88     fi
89
90     # The boot MAC address must be specified when a port is included
91     # in the IPMI driver address (i.e when using the VirtualBMC
92     # controller).  Note that the below is a bit of a hack as it only
93     # checks the first entry in NODES_FILE for the port.
94     if cat "$NODES_FILE" |
95             jq -r '.nodes[0].ipmi_driver_info.address' | grep -c ':[0-9]\+$' >/dev/null; then
96         BOOT_LINK=$(cat "$NODES_FILE" |
97                         jq -r '.nodes[0].net.links | map(.id=="provisioning_nic") | index(true)')
98         cat "$NODES_FILE" |
99             jq -r --argjson BOOT_LINK $BOOT_LINK '.nodes[] | [
100                .name,
101                .ipmi_driver_info.username,
102                .ipmi_driver_info.password,
103                .ipmi_driver_info.address,
104                .net.links[$BOOT_LINK].ethernet_mac_address,
105                .os.username,
106                .os.password,
107                .os.image_name
108                ] | @csv' |
109             sed 's/"//g'
110     else
111         cat "$NODES_FILE" |
112             jq -r '.nodes[] | [
113                .name,
114                .ipmi_driver_info.username,
115                .ipmi_driver_info.password,
116                .ipmi_driver_info.address,
117                "",
118                .os.username,
119                .os.password,
120                .os.image_name
121                ] | @csv' |
122             sed 's/"//g'
123     fi
124 }
125
126 function node_userdata {
127     name="$1"
128     username="$2"
129     password="$3"
130     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
131
132     # validate that the user isn't expecting the deprecated
133     # COMPUTE_NODE_PASSWORD to be used
134     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
135         cat <<EOF
136 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
137 Unset COMPUTE_NODE_PASSWORD and retry.
138 EOF
139         exit 1
140     fi
141
142     printf "userData:\n"
143     if [ -n "$username" ]; then
144         printf "  name: ${username}\n"
145     fi
146     if [ -n "$password" ]; then
147         passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
148         printf "  hashedPassword: ${passwd}\n"
149     fi
150
151     if [ -n "$COMPUTE_NODE_FQDN" ]; then
152         printf "  fqdn: ${COMPUTE_NODE_FQDN}\n"
153     fi
154
155     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
156         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
157     fi
158
159     printf "  sshAuthorizedKey: $(cat $HOME/.ssh/id_rsa.pub)\n"
160 }
161
162 # Returns "null" when the field is not present
163 function networkdata_networks_field {
164     name=$1
165     network=$2
166     field=$3
167     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}
168 }
169
170 # Returns "null" when the field is not present
171 function networkdata_links_field {
172     name=$1
173     link=$2
174     field=$3
175     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}
176 }
177
178 function node_networkdata {
179     name=$1
180
181     if [ ! -f "$NODES_FILE" ]; then
182         exit 1
183     fi
184
185     printf "networks:\n"
186     for network in $(cat $NODES_FILE | jq -r --arg name "$name" '.nodes[] | select(.name==$name) | .net.networks[].id'); do
187         link=$(networkdata_networks_field $name $network "link")
188         type=$(networkdata_networks_field $name $network "type")
189         mac=$(networkdata_links_field $name $link "ethernet_mac_address")
190
191         # Optional values
192         ip_address=$(networkdata_networks_field $name $network "ip_address")
193         gateway=$(networkdata_networks_field $name $network "gateway")
194         dns_nameservers=$(networkdata_networks_field $name $network "dns_nameservers")
195
196         printf "  ${network}:\n"
197         printf "    macAddress: ${mac}\n"
198         printf "    type: ${type}\n"
199         if [[ $ip_address != "null" ]]; then
200             printf "    ipAddress: ${ip_address}\n"
201         fi
202         if [[ $gateway != "null" ]]; then
203             printf "    gateway: ${gateway}\n"
204         fi
205         if [[ $dns_nameservers != "null" ]]; then
206             printf "    nameservers: ${dns_nameservers}\n"
207         fi
208     done
209 }
210
211 function wait_for {
212     local -r interval=${WAIT_FOR_INTERVAL:-30s}
213     local -r max_tries=${WAIT_FOR_TRIES:-20}
214     local try=0
215     until "$@"; do
216         echo "[${try}/${max_tries}] - Waiting ${interval} for $*"
217         sleep ${interval}
218         try=$((try+1))
219         if [[ ${try} -ge ${max_tries} ]]; then
220             return 1
221         fi
222     done
223 }
224
225 # This is intended to be used together with list_nodes in the
226 # following way:
227 #   list_nodes | while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do ...
228 function node_machine_values {
229     printf "machineName: ${name}\n"
230     printf "machineLabels:\n"
231     printf "  machine: ${name}\n"
232     printf "bmcUsername: ${ipmi_username}\n"
233     printf "bmcPassword: ${ipmi_password}\n"
234     printf "bmcAddress: ipmi://${ipmi_address}\n"
235     if [[ ! -z ${boot_mac} ]]; then
236         printf "bootMACAddress: ${boot_mac}\n"
237     fi
238     printf "imageName: ${BM_IMAGE}\n"
239     node_userdata ${name} ${os_username} ${os_password}
240     node_networkdata ${name}
241 }
242
243 function clone_repository {
244     local -r path=$1
245     local -r repo=$2
246     local -r version=$3
247     mkdir -p $(dirname ${path})
248     if [[ -d ${path} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
249        rm -rf "${path}"
250     fi
251     if [ ! -d "${path}" ] ; then
252         pushd $(dirname ${path})
253         git clone "${repo}"
254         popd
255     else
256        pushd "${path}"
257        git fetch
258        popd
259     fi
260     pushd "${path}"
261     git reset --hard "${version}"
262     popd
263 }
264
265 function clone_baremetal_operator_repository {
266     clone_repository ${BMOPATH} ${BMOREPO} ${BMO_VERSION}
267 }
268
269 function clone_kud_repository {
270     clone_repository ${KUDPATH} ${KUDREPO} ${KUD_VERSION}
271 }
272
273 function clone_emco_repository {
274     clone_repository ${EMCOPATH} ${EMCOREPO} ${EMCO_VERSION}
275 }
276
277 function fetch_image {
278     if [[ "${BM_IMAGE_URL}" && "${BM_IMAGE}" ]]; then
279        mkdir -p "${IRONIC_DATA_DIR}/html/images"
280        pushd ${IRONIC_DATA_DIR}/html/images
281        local_checksum="0"
282        if [[ -f "${BM_IMAGE}" ]]; then
283            local_checksum=$(md5sum ${BM_IMAGE} | awk '{print $1}')
284        fi
285        remote_checksum=$(curl -sL "$(dirname ${BM_IMAGE_URL})/MD5SUMS" | grep ${BM_IMAGE} | awk '{print $1}')
286        if [[ ${local_checksum} != ${remote_checksum} ]]; then
287             curl -o ${BM_IMAGE} --insecure --compressed -O -L ${BM_IMAGE_URL}
288             md5sum ${BM_IMAGE} | awk '{print $1}' > ${BM_IMAGE}.md5sum
289        fi
290        popd
291     fi
292 }