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