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 ##############################################################################
12 _print_msg "INFO" "$1"
16 _print_msg "ERROR" "$1"
21 echo "$(date +%H:%M:%S) - $1: $2"
29 if [ "${DEBUG:-false}" == "true" ]; then
33 check_prerequisites() {
34 info "Check prerequisites"
36 #-------------------------------------------------------------------------------
37 # Check for DEPLOYMENT type
38 #-------------------------------------------------------------------------------
39 if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
40 error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
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."
50 #-------------------------------------------------------------------------------
51 # Check for passwordless sudo
52 #-------------------------------------------------------------------------------
53 if ! sudo -n "true"; then
54 error "passwordless sudo is needed for '$(id -nu)' user."
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!"
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
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."
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."
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."
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
101 if ! groups | grep "$libvirt_group"; then
102 error "$(id -nu) user doesn't belong to $libvirt_group group."
106 # Get jumphost VM PXE IP
113 assert_non_empty "$host" "get_ip - host parameter not provided"
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."
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."
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."
127 # Get public MAC for VM
134 assert_non_empty "$host" "get_mac - host parameter not provided"
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."
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."
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."
149 if [[ "$DEPLOYMENT" == "full" ]]; then
150 ip=$(get_host_pxe_ip "jumphost")
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."
161 # Copy files needed by Infra engine & BMRA in the jumphost VM
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 \
170 "$USERNAME@${vm_ip}:.ssh/id_rsa"
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
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
192 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
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))
197 for idx in $(seq 0 "$NODE_MAX_ID"); do
198 NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
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
208 # k8s Provisioning (currently BMRA)
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;"
215 ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
217 # shellcheck disable=SC2087
218 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
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
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
233 if [[ "$DEPLOYMENT" == "full" ]]; then
234 echo "- name: install Python packages
237 - pip==9.0.3" >> ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
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
253 sudo docker run --rm \
254 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
255 -e PROFILE=${BMRA_PROFILE} \
256 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
257 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra2.0-install:centos \
262 # Copy kubeconfig to the appropriate location needed by functest containers
264 # TODO Use Kubespray variables in BMRA to simplify this
265 MASTER_IP=$(get_host_pxe_ip "nodes[0]")
266 # shellcheck disable=SC2087
267 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
268 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
271 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
272 # Direct scp to the specified location doesn't work due to permission/ssh-keys
273 scp -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
274 if [ -d "/home/opnfv/functest-kubernetes" ]; then
275 sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
279 # Creates a python virtual environment
280 creates_virtualenv() {
281 if [ ! -d "$CURRENTPATH/.venv" ]; then
284 # shellcheck disable=SC1090
285 source "$CURRENTPATH/.venv/bin/activate"
286 pip install -r "$CURRENTPATH/requirements.txt"
289 # Executes a specific Ansible playbook
291 ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
292 if [ "${DEBUG:-false}" == "true" ]; then
295 eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"