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