Install Cluster API into jump server
[icn.git] / env / lib / common.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 DOWNLOAD_PATH=${DOWNLOAD_PATH:-/opt/icn}
5
6 IRONIC_DATA_DIR=${IRONIC_DATA_DIR:-"/opt/ironic"}
7 #IRONIC_PROVISIONING_INTERFACE is required to be provisioning, don't change it
8 IRONIC_INTERFACE=${IRONIC_INTERFACE:-}
9 IRONIC_PROVISIONING_INTERFACE=${IRONIC_PROVISIONING_INTERFACE:-"provisioning"}
10 IRONIC_IPMI_INTERFACE=${IRONIC_IPMI_INTERFACE:-}
11 IRONIC_PROVISIONING_INTERFACE_IP=${IRONIC_PROVISIONING_INTERFACE_IP:-"172.22.0.1"}
12 BM_IMAGE_URL=${BM_IMAGE_URL:-"https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"}
13 BM_IMAGE=${BM_IMAGE:-"bionic-server-cloudimg-amd64.img"}
14
15 #Baremetal operator repository URL
16 BMOREPO="${BMOREPO:-https://github.com/metal3-io/baremetal-operator.git}"
17 #Path to clone the baremetal operator repo
18 BMOPATH="/opt/src/github.com/metal3-io/baremetal-operator"
19 #Bare Metal Operator version to use
20 BMO_VERSION="capm3-v0.5.1"
21 #Discard existing baremetal operator repo directory
22 FORCE_REPO_UPDATE="${FORCE_REPO_UPDATE:-true}"
23
24 # The kustomize version to use
25 KUSTOMIZE_VERSION="v4.3.0"
26
27 #Cluster API version to use
28 CAPI_VERSION="v0.4.3"
29
30 #refered from onap
31 function call_api {
32     #Runs curl with passed flags and provides
33     #additional error handling and debug information
34
35     #Function outputs server response body
36     #and performs validation of http_code
37
38     local status
39     local curl_response_file="$(mktemp -p /tmp)"
40     local curl_common_flags=(-s -w "%{http_code}" -o "${curl_response_file}")
41     local command=(curl "${curl_common_flags[@]}" "$@")
42
43     echo "[INFO] Running '${command[@]}'" >&2
44     if ! status="$("${command[@]}")"; then
45         echo "[ERROR] Internal curl error! '$status'" >&2
46         cat "${curl_response_file}"
47         rm "${curl_response_file}"
48         return 2
49     else
50         echo "[INFO] Server replied with status: ${status}" >&2
51         cat "${curl_response_file}"
52         rm "${curl_response_file}"
53         if [[ "${status:0:1}" =~ [45] ]]; then
54             return 1
55         else
56             return 0
57         fi
58     fi
59 }
60
61 function list_nodes {
62     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
63
64     if [ ! -f "$NODES_FILE" ]; then
65         exit 1
66     fi
67
68     # The boot MAC address must be specified when a port is included
69     # in the IPMI driver address (i.e when using the VirtualBMC
70     # controller).  Note that the below is a bit of a hack as it only
71     # checks the first entry in NODES_FILE for the port.
72     if cat "$NODES_FILE" |
73             jq -r '.nodes[0].ipmi_driver_info.address' | grep -c ':[0-9]\+$' >/dev/null; then
74         BOOT_LINK=$(cat "$NODES_FILE" |
75                         jq -r '.nodes[0].net.links | map(.id=="provisioning_nic") | index(true)')
76         cat "$NODES_FILE" |
77             jq -r --argjson BOOT_LINK $BOOT_LINK '.nodes[] | [
78                .name,
79                .ipmi_driver_info.username,
80                .ipmi_driver_info.password,
81                .ipmi_driver_info.address,
82                .net.links[$BOOT_LINK].ethernet_mac_address,
83                .os.username,
84                .os.password,
85                .os.image_name
86                ] | @csv' |
87             sed 's/"//g'
88     else
89         cat "$NODES_FILE" |
90             jq -r '.nodes[] | [
91                .name,
92                .ipmi_driver_info.username,
93                .ipmi_driver_info.password,
94                .ipmi_driver_info.address,
95                "",
96                .os.username,
97                .os.password,
98                .os.image_name
99                ] | @csv' |
100             sed 's/"//g'
101     fi
102 }
103
104 # Returns "null" when the field is not present
105 function networkdata_networks_field {
106     name=$1
107     network=$2
108     field=$3
109     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
110     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}
111 }
112
113 # Returns "null" when the field is not present
114 function networkdata_links_field {
115     name=$1
116     link=$2
117     field=$3
118     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
119     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}
120 }
121
122 function node_networkdata {
123     name=$1
124
125     NODES_FILE="${IRONIC_DATA_DIR}/nodes.json"
126
127     if [ ! -f "$NODES_FILE" ]; then
128         exit 1
129     fi
130
131     printf "    networks:\n"
132     for network in $(cat $NODES_FILE | jq -r --arg name "$name" '.nodes[] | select(.name==$name) | .net.networks[].id'); do
133         link=$(networkdata_networks_field $name $network "link")
134         type=$(networkdata_networks_field $name $network "type")
135         mac=$(networkdata_links_field $name $link "ethernet_mac_address")
136
137         # Optional values
138         ip_address=$(networkdata_networks_field $name $network "ip_address")
139         gateway=$(networkdata_networks_field $name $network "gateway")
140         dns_nameservers=$(networkdata_networks_field $name $network "dns_nameservers")
141
142         printf "      ${network}:\n"
143         printf "        macAddress: ${mac}\n"
144         printf "        type: ${type}\n"
145         if [[ $ip_address != "null" ]]; then
146             printf "        ipAddress: ${ip_address}\n"
147         fi
148         if [[ $gateway != "null" ]]; then
149             printf "        gateway: ${gateway}\n"
150         fi
151         if [[ $dns_nameservers != "null" ]]; then
152             printf "        nameservers: ${dns_nameservers}\n"
153         fi
154     done
155 }
156
157 function wait_for {
158     local -r interval=${WAIT_FOR_INTERVAL:-30s}
159     local -r max_tries=${WAIT_FOR_TRIES:-20}
160     local try=0
161     until "$@"; do
162         echo "[${try}/${max_tries}] - Waiting ${interval} for $*"
163         sleep ${interval}
164         try=$((try+1))
165         if [[ ${try} -ge ${max_tries} ]]; then
166             return 1
167         fi
168     done
169 }
170
171 function clone_baremetal_operator_repository {
172     mkdir -p $(dirname ${BMOPATH})
173     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
174        rm -rf "${BMOPATH}"
175     fi
176     if [ ! -d "${BMOPATH}" ] ; then
177         pushd $(dirname ${BMOPATH})
178         git clone "${BMOREPO}"
179         popd
180     else
181        pushd "${BMOPATH}"
182        git fetch
183        popd
184     fi
185     pushd "${BMOPATH}"
186     git reset --hard "${BMO_VERSION}"
187     popd
188 }
189
190 function install_kustomize {
191     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
192     tar xzf kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz --no-same-owner
193     sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
194     rm kustomize_${KUSTOMIZE_VERSION}_linux_amd64.tar.gz kustomize
195     kustomize version
196 }
197
198 function install_clusterctl {
199     curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/${CAPI_VERSION}/clusterctl-linux-amd64 -o clusterctl
200     sudo install -o root -g root -m 0755 clusterctl /usr/local/bin/clusterctl
201     rm clusterctl
202     clusterctl version
203 }
204
205 function fetch_image {
206     if [[ "${BM_IMAGE_URL}" && "${BM_IMAGE}" ]]; then
207        mkdir -p "${IRONIC_DATA_DIR}/html/images"
208        pushd ${IRONIC_DATA_DIR}/html/images
209        local_checksum="0"
210        if [[ -f "${BM_IMAGE}" ]]; then
211            local_checksum=$(md5sum ${BM_IMAGE} | awk '{print $1}')
212        fi
213        remote_checksum=$(curl -sL "$(dirname ${BM_IMAGE_URL})/MD5SUMS" | grep ${BM_IMAGE} | awk '{print $1}')
214        if [[ ${local_checksum} != ${remote_checksum} ]]; then
215             curl -o ${BM_IMAGE} --insecure --compressed -O -L ${BM_IMAGE_URL}
216             md5sum ${BM_IMAGE} | awk '{print $1}' > ${BM_IMAGE}.md5sum
217        fi
218        popd
219     fi
220 }