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"
30 check_prerequisites() {
31 info "Check prerequisites"
33 #-------------------------------------------------------------------------------
34 # Check for DEPLOYMENT type
35 #-------------------------------------------------------------------------------
36 DEPLOYMENT=${DEPLOYMENT:-full}
37 if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
38 error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
41 #-------------------------------------------------------------------------------
42 # We shouldn't be running as root
43 #-------------------------------------------------------------------------------
44 if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
45 error "This script must not be run as root! Please switch to a regular user before running the script."
48 #-------------------------------------------------------------------------------
49 # Check for passwordless sudo
50 #-------------------------------------------------------------------------------
51 if ! sudo -n "true"; then
52 error "passwordless sudo is needed for '$(id -nu)' user."
55 #-------------------------------------------------------------------------------
56 # Check if SSH key exists
57 #-------------------------------------------------------------------------------
58 if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
59 error "You must have SSH keypair in order to run this script!"
62 #-------------------------------------------------------------------------------
63 # We are using sudo so we need to make sure that env_reset is not present
64 #-------------------------------------------------------------------------------
65 sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
67 #-------------------------------------------------------------------------------
68 # Check if some tools are installed
69 #-------------------------------------------------------------------------------
70 for tool in ansible yq virsh jq; do
71 if ! command -v "$tool" &> /dev/null; then
72 error "$tool not found. Please install."
76 #-------------------------------------------------------------------------------
77 # Check if user belongs to libvirt's group
78 #-------------------------------------------------------------------------------
79 libvirt_group="libvirt"
80 # shellcheck disable=SC1091
81 source /etc/os-release || source /usr/lib/os-release
82 if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
85 if ! groups | grep "$libvirt_group"; then
86 error "$(id -nu) user doesn't belong to $libvirt_group group."
90 # Get jumphost VM PXE IP
97 assert_non_empty "$host" "get_ip - host parameter not provided"
99 PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_network)
100 assert_non_empty "$PXE_NETWORK" "PXE network for jump VM not defined in IDF."
102 PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
103 assert_non_empty "$PXE_IF_INDEX" "Index of PXE interface not found in IDF."
105 PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
106 assert_non_empty "$PXE_IF_IP" "IP of PXE interface not found in PDF."
111 # Get public MAC for VM
118 assert_non_empty "$host" "get_mac - host parameter not provided"
120 PUB_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.public_network)
121 assert_non_empty "$PUB_NETWORK" "Public network for jump VM not defined in IDF."
123 PUB_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PUB_NETWORK".interface)
124 assert_non_empty "$PUB_IF_INDEX" "Index of public interface not found in IDF."
126 PUB_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PUB_IF_INDEX"].mac_address)
127 assert_non_empty "$PUB_IF_MAC" "MAC of public interface not found in PDF."
133 if [[ "$DEPLOYMENT" == "full" ]]; then
134 ip=$(get_host_pxe_ip "jumphost")
136 mac=$(get_host_pub_mac "jumphost")
137 JUMPHOST_NAME=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.name)
138 ipblock=$(virsh domifaddr "$JUMPHOST_NAME" --full | grep "$mac" | awk '{print $4}' | tail -n 1)
139 assert_non_empty "$ipblock" "IP subnet for VM not available."
145 # Copy files needed by Infra engine & BMRA in the jumphost VM
148 scp -r -o StrictHostKeyChecking=no \
149 "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
150 "$USERNAME@${vm_ip}:$PROJECT_ROOT"
151 if [[ "$DEPLOYMENT" != "full" ]]; then
152 scp -r -o StrictHostKeyChecking=no \
154 "$USERNAME@${vm_ip}:.ssh/id_rsa"
160 # shellcheck disable=SC2087
161 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
162 # Install and run cloud-infra
163 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
164 ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
165 git clone https://gerrit.nordix.org/infra/engine.git
166 cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
167 ${PROJECT_ROOT}/engine/engine
169 cd ${PROJECT_ROOT}/engine/engine
170 ./deploy.sh -s ironic -d centos7 \
171 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
172 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
176 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
178 # Set Upper limit of number nodes in RI2 cluster (starting from 0)
179 NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
181 for idx in $(seq 0 "$NODE_MAX_ID"); do
182 NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
184 # shellcheck disable=SC2087
185 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
186 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
187 'bash -s' < ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
192 # Get IPs of target nodes (used for installing dependencies)
194 yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes[*].interfaces[*].address
197 # k8s Provisioning (currently BMRA)
199 ansible_cmd='/bin/bash -c "'
200 if [[ "$DEPLOYMENT" == "k8s" ]]; then
201 ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
202 ansible_cmd+='pip install --upgrade pip==9.0.3; yum -y remove python-netaddr; pip install netaddr==0.7.19; '
204 ansible_cmd+='ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml"'
206 # shellcheck disable=SC2087
207 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
209 if ! command -v docker; then
210 curl -fsSL https://get.docker.com/ | sh
211 printf "Waiting for docker service..."
212 until sudo docker info; do
217 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
218 git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
219 cp -r ${PROJECT_ROOT}/container-experience-kits/examples/group_vars ${PROJECT_ROOT}/container-experience-kits/
220 #TODO Remove this once the reported issue is fixed in the next BMRA Release
221 if [[ "$DEPLOYMENT" == "full" ]]; then
222 sed -i '/\openshift/a \ extra_args: --ignore-installed PyYAML' \
223 ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
226 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
227 ${PROJECT_ROOT}/container-experience-kits/
228 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
229 ${PROJECT_ROOT}/container-experience-kits/group_vars/
230 sudo docker run --rm \
231 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
232 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
233 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
238 # Copy kubeconfig to the appropriate location needed by functest containers
240 # TODO Use Kubespray variables in BMRA to simplify this
241 MASTER_IP=$(get_host_pxe_ip "nodes[0]")
242 # shellcheck disable=SC2087
243 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
244 scp -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
247 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
248 # Direct scp to the specified location doesn't work due to permission/ssh-keys
249 scp "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
250 sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
253 # Executes a specific Ansible playbook
255 ansible_cmd="$(command -v ansible-playbook)"
256 ansible_cmd+=" -i $CURRENTPATH/inventory/localhost.ini"
257 ansible_cmd+=" -e ansible_python_interpreter=$(command -v python)"
258 if [ "${DEBUG:-false}" == "true" ]; then
261 eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"