Added support for Docker Hub credentials (CI)
[kuberef.git] / functions.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) Ericsson AB and others
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 info() {
12     _print_msg "INFO" "$1"
13 }
14
15 error() {
16     _print_msg "ERROR" "$1"
17     exit 1
18 }
19
20 _print_msg() {
21     echo "$(date +%H:%M:%S) - $1: $2"
22 }
23
24 assert_non_empty() {
25     if [ -z "$1" ]; then
26         error "$2"
27     fi
28 }
29 if [ "${DEBUG:-false}" == "true" ]; then
30     set -o xtrace
31 fi
32
33 check_prerequisites() {
34     info "Check prerequisites"
35
36     #-------------------------------------------------------------------------------
37     # Check for DEPLOYMENT type
38     #-------------------------------------------------------------------------------
39     if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
40         error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
41     fi
42
43     #-------------------------------------------------------------------------------
44     # We shouldn't be running as root
45     #-------------------------------------------------------------------------------
46     if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
47         error "This script must not be run as root! Please switch to a regular user before running the script."
48     fi
49
50     #-------------------------------------------------------------------------------
51     # Check for passwordless sudo
52     #-------------------------------------------------------------------------------
53     if ! sudo -n "true"; then
54         error "passwordless sudo is needed for '$(id -nu)' user."
55     fi
56
57     #-------------------------------------------------------------------------------
58     # Check if SSH key exists
59     #-------------------------------------------------------------------------------
60     if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
61         error "You must have SSH keypair in order to run this script!"
62     fi
63
64     #-------------------------------------------------------------------------------
65     # We are using sudo so we need to make sure that env_reset is not present
66     #-------------------------------------------------------------------------------
67     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
68
69     #-------------------------------------------------------------------------------
70     # Check if Python Virtual Environment is installed
71     #-------------------------------------------------------------------------------
72     if ! command -v virtualenv &> /dev/null; then
73         error "VirtualEnv not found. Please install."
74     fi
75
76     #-------------------------------------------------------------------------------
77     # Check if PIP Installs Packages is installed
78     #-------------------------------------------------------------------------------
79     if ! command -v pip &> /dev/null; then
80         error "PIP not found. Please install."
81     fi
82
83     #-------------------------------------------------------------------------------
84     # Check is libvirt is installed
85     #-------------------------------------------------------------------------------
86     for tool in ansible yq virsh jq; do
87         if ! command -v "$tool" &> /dev/null; then
88             error "$tool not found. Please install."
89         fi
90     done
91
92     #-------------------------------------------------------------------------------
93     # Check if user belongs to libvirt's group
94     #-------------------------------------------------------------------------------
95     libvirt_group="libvirt"
96     # shellcheck disable=SC1091
97     source /etc/os-release || source /usr/lib/os-release
98     if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
99         libvirt_group+="d"
100     fi
101     if ! groups | grep "$libvirt_group"; then
102         error "$(id -nu) user doesn't belong to $libvirt_group group."
103     fi
104 }
105
106 # Get jumphost VM PXE IP
107 get_host_pxe_ip() {
108     local PXE_NETWORK
109     local PXE_IF_INDEX
110     local PXE_IF_IP
111
112     host=$1
113     assert_non_empty "$host" "get_ip - host parameter not provided"
114
115     PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_network)
116     assert_non_empty "$PXE_NETWORK" "PXE network for jump VM not defined in IDF."
117
118     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
119     assert_non_empty "$PXE_IF_INDEX" "Index of PXE interface not found in IDF."
120
121     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
122     assert_non_empty "$PXE_IF_IP" "IP of PXE interface not found in PDF."
123
124     echo "$PXE_IF_IP"
125 }
126
127 # Get public MAC for VM
128 get_host_pub_mac() {
129     local PUB_NETWORK
130     local PUB_IF_INDEX
131     local PUB_IF_MAC
132
133     host=$1
134     assert_non_empty "$host" "get_mac - host parameter not provided"
135
136     PUB_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml  engine.public_network)
137     assert_non_empty "$PUB_NETWORK" "Public network for jump VM not defined in IDF."
138
139     PUB_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PUB_NETWORK".interface)
140     assert_non_empty "$PUB_IF_INDEX" "Index of public interface not found in IDF."
141
142     PUB_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PUB_IF_INDEX"].mac_address)
143     assert_non_empty "$PUB_IF_MAC" "MAC of public interface not found in PDF."
144     echo "$PUB_IF_MAC"
145 }
146
147 # Get jumphost VM IP
148 get_vm_ip() {
149     if [[ "$DEPLOYMENT" == "full" ]]; then
150         ip=$(get_host_pxe_ip "jumphost")
151     else
152         mac=$(get_host_pub_mac "jumphost")
153         JUMPHOST_NAME=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.name)
154         ipblock=$(virsh domifaddr "$JUMPHOST_NAME" --full | grep "$mac" | awk '{print $4}' | tail -n 1)
155         assert_non_empty "$ipblock" "IP subnet for VM not available."
156         ip="${ipblock%/*}"
157     fi
158     echo "$ip"
159 }
160
161 # Copy files needed by Infra engine & BMRA in the jumphost VM
162 copy_files_jump() {
163     vm_ip="$(get_vm_ip)"
164     docker_config="/opt/kuberef/docker_config"
165     scp -r -o StrictHostKeyChecking=no \
166     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
167     "$USERNAME@${vm_ip}:$PROJECT_ROOT"
168     if [[ "$DEPLOYMENT" != "full" ]]; then
169         scp -r -o StrictHostKeyChecking=no \
170         ~/.ssh/id_rsa \
171         "$USERNAME@${vm_ip}:.ssh/id_rsa"
172     fi
173     if [ -f "$docker_config" ]; then
174         scp -r -o StrictHostKeyChecking=no \
175         "$docker_config" "$USERNAME@${vm_ip}:$PROJECT_ROOT"
176     fi
177 }
178
179 # Host Provisioning
180 provision_hosts() {
181     # shellcheck disable=SC2087
182     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
183 # Install and run cloud-infra
184 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
185     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
186     git clone https://gerrit.nordix.org/infra/engine.git
187     cd ${PROJECT_ROOT}/engine/engine && git checkout ${ENGINE_COMMIT_ID}
188     cp ${PROJECT_ROOT}/${VENDOR}/{pdf.yaml,idf.yaml} \
189     ${PROJECT_ROOT}/engine/engine
190 fi
191 ${PROJECT_ROOT}/engine/engine/deploy.sh -s ironic -d ${DISTRO} \
192 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
193 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
194 EOF
195 }
196
197 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
198 setup_network() {
199     # Set Upper limit of number nodes in RI2 cluster (starting from 0)
200     NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
201
202     for idx in $(seq 0 "$NODE_MAX_ID"); do
203         NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
204         # SSH to jumphost
205         # shellcheck disable=SC2087
206         ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
207 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
208     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
209 EOF
210     done
211 }
212
213 # k8s Provisioning (currently BMRA)
214 provision_k8s() {
215     ansible_cmd="/bin/bash -c '"
216     if [[ "$DEPLOYMENT" == "k8s" ]]; then
217         ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
218         ansible_cmd+="yum -y remove python-netaddr; pip install --upgrade pip; pip install ansible==2.9.17; ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/k8s/patch_kubespray.yml;"
219     fi
220     ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
221
222     # shellcheck disable=SC2087
223     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
224 # Install BMRA
225 if ! command -v docker; then
226     curl -fsSL https://get.docker.com/ | sh
227     printf "Waiting for docker service..."
228     until sudo docker info; do
229         printf "."
230         sleep 2
231     done
232 fi
233 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
234     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v21.03 ${PROJECT_ROOT}/container-experience-kits/
235     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/${BMRA_PROFILE}/group_vars ${PROJECT_ROOT}/container-experience-kits/
236 fi
237 if [ -f "${PROJECT_ROOT}/docker_config" ]; then
238     cp ${PROJECT_ROOT}/docker_config \
239         ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials/vars/main.yml
240     cp -r ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials \
241         ${PROJECT_ROOT}/container-experience-kits/roles/
242     cp ${PROJECT_ROOT}/${INSTALLER}/patched_k8s.yml \
243         ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/k8s.yml
244 fi
245 cp ${PROJECT_ROOT}/${INSTALLER}/{inventory.ini,ansible.cfg} \
246     ${PROJECT_ROOT}/container-experience-kits/
247 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
248     ${PROJECT_ROOT}/container-experience-kits/group_vars/
249 cp ${PROJECT_ROOT}/${INSTALLER}/patched_cmk_build.yml \
250     ${PROJECT_ROOT}/container-experience-kits/roles/cmk_install/tasks/main.yml
251 cp ${PROJECT_ROOT}/${INSTALLER}/patched_vfio.yml \
252     ${PROJECT_ROOT}/container-experience-kits/roles/sriov_nic_init/tasks/bind_vf_driver.yml
253 cp ${PROJECT_ROOT}/${INSTALLER}/patched_rhel_packages.yml \
254     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
255 cp ${PROJECT_ROOT}/${INSTALLER}/patched_packages.yml \
256     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/main.yml
257 cp ${PROJECT_ROOT}/${INSTALLER}/patched_kubespray_requirements.txt \
258     ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/kubespray/requirements.txt
259
260 sudo docker run --rm \
261 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
262 -e PROFILE=${BMRA_PROFILE} \
263 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
264 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra21.03-install:centos \
265 ${ansible_cmd}
266 EOF
267 }
268
269 # Copy kubeconfig to the appropriate location needed by functest containers
270 copy_k8s_config() {
271 # TODO Use Kubespray variables in BMRA to simplify this
272     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
273     # shellcheck disable=SC2087
274     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
275 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
276 EOF
277
278 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
279 # Direct scp to the specified location doesn't work due to permission/ssh-keys
280     scp  -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
281     if [ -d "/home/opnfv/functest-kubernetes" ]; then
282         sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
283     fi
284 }
285
286 # Creates a python virtual environment
287 creates_virtualenv() {
288     if [  ! -d "$CURRENTPATH/.venv" ]; then
289         virtualenv .venv
290     fi
291     # shellcheck disable=SC1090
292     source "$CURRENTPATH/.venv/bin/activate"
293     pip install -r "$CURRENTPATH/requirements.txt"
294 }
295
296 # Executes a specific Ansible playbook
297 run_playbook() {
298     ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
299     if [ "${DEBUG:-false}" == "true" ]; then
300         ansible_cmd+=" -vvv"
301     fi
302     eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"
303 }