Add netperf in Kuberef gates
[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     scp -r -o StrictHostKeyChecking=no \
165     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
166     "$USERNAME@${vm_ip}:$PROJECT_ROOT"
167     if [[ "$DEPLOYMENT" != "full" ]]; then
168         scp -r -o StrictHostKeyChecking=no \
169         ~/.ssh/id_rsa \
170         "$USERNAME@${vm_ip}:.ssh/id_rsa"
171     fi
172 }
173
174 # Host Provisioning
175 provision_hosts() {
176     # shellcheck disable=SC2087
177     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
178 # Install and run cloud-infra
179 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
180     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
181     git clone https://gerrit.nordix.org/infra/engine.git
182     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
183     ${PROJECT_ROOT}/engine/engine
184 fi
185 cd ${PROJECT_ROOT}/engine/engine
186 ./deploy.sh -s ironic -d ${DISTRO} \
187 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
188 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
189 EOF
190 }
191
192 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
193 setup_network() {
194     # Set Upper limit of number nodes in RI2 cluster (starting from 0)
195     NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
196
197     for idx in $(seq 0 "$NODE_MAX_ID"); do
198         NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
199         # SSH to jumphost
200         # shellcheck disable=SC2087
201         ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
202 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
203     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
204 EOF
205     done
206 }
207
208 # k8s Provisioning (currently BMRA)
209 provision_k8s() {
210     ansible_cmd="/bin/bash -c '"
211     if [[ "$DEPLOYMENT" == "k8s" ]]; then
212         ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
213         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;"
214     fi
215     ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
216
217     # shellcheck disable=SC2087
218     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
219 # Install BMRA
220 if ! command -v docker; then
221     curl -fsSL https://get.docker.com/ | sh
222     printf "Waiting for docker service..."
223     until sudo docker info; do
224         printf "."
225         sleep 2
226     done
227 fi
228 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
229     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v21.03 ${PROJECT_ROOT}/container-experience-kits/
230     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/${BMRA_PROFILE}/group_vars ${PROJECT_ROOT}/container-experience-kits/
231 # NOTE The following condition/workaround will be removed once the reported issue https://github.com/intel/container-experience-kits/issues/68
232 # is fixed upstream
233     if [[ "$DEPLOYMENT" == "full" ]]; then
234        echo "- name: install Python packages
235   pip:
236     name:
237       - pip==9.0.3" >> ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
238     fi
239 fi
240 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
241     ${PROJECT_ROOT}/container-experience-kits/
242 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
243     ${PROJECT_ROOT}/container-experience-kits/group_vars/
244 cp ${PROJECT_ROOT}/${INSTALLER}/patched_cmk_build.yml \
245     ${PROJECT_ROOT}/container-experience-kits/roles/cmk_install/tasks/main.yml
246 cp ${PROJECT_ROOT}/${INSTALLER}/patched_vfio.yml \
247     ${PROJECT_ROOT}/container-experience-kits/roles/sriov_nic_init/tasks/bind_vf_driver.yml
248 cp ${PROJECT_ROOT}/${INSTALLER}/ansible.cfg \
249     ${PROJECT_ROOT}/container-experience-kits/ansible.cfg
250 cp ${PROJECT_ROOT}/${INSTALLER}/patched_rhel_packages.yml \
251     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
252 cp ${PROJECT_ROOT}/${INSTALLER}/patched_packages.yml \
253     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/main.yml
254
255 sudo docker run --rm \
256 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
257 -e PROFILE=${BMRA_PROFILE} \
258 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
259 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra21.03-install:centos \
260 ${ansible_cmd}
261 EOF
262 }
263
264 # Copy kubeconfig to the appropriate location needed by functest containers
265 copy_k8s_config() {
266 # TODO Use Kubespray variables in BMRA to simplify this
267     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
268     # shellcheck disable=SC2087
269     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
270 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
271 EOF
272
273 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
274 # Direct scp to the specified location doesn't work due to permission/ssh-keys
275     scp  -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
276     if [ -d "/home/opnfv/functest-kubernetes" ]; then
277         sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
278     fi
279 }
280
281 # Creates a python virtual environment
282 creates_virtualenv() {
283     if [  ! -d "$CURRENTPATH/.venv" ]; then
284         virtualenv .venv
285     fi
286     # shellcheck disable=SC1090
287     source "$CURRENTPATH/.venv/bin/activate"
288     pip install -r "$CURRENTPATH/requirements.txt"
289 }
290
291 # Executes a specific Ansible playbook
292 run_playbook() {
293     ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
294     if [ "${DEBUG:-false}" == "true" ]; then
295         ansible_cmd+=" -vvv"
296     fi
297     eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"
298 }