3 # SPDX-FileCopyrightText: 2021 Ericsson AB and others
5 # SPDX-License-Identifier: Apache-2.0
12 _print_msg "ERROR" "$1"
17 echo "$(date +%H:%M:%S) - $1: $2"
25 if [ "${DEBUG:-false}" == "true" ]; then
29 check_prerequisites() {
30 info "Check prerequisites"
32 #-------------------------------------------------------------------------------
33 # Check for DEPLOYMENT type
34 #-------------------------------------------------------------------------------
35 if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
36 error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
39 #-------------------------------------------------------------------------------
40 # We shouldn't be running as root
41 #-------------------------------------------------------------------------------
42 if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
43 error "This script must not be run as root! Please switch to a regular user before running the script."
46 #-------------------------------------------------------------------------------
47 # Check for passwordless sudo
48 #-------------------------------------------------------------------------------
49 if ! sudo -n "true"; then
50 error "passwordless sudo is needed for '$(id -nu)' user."
53 #-------------------------------------------------------------------------------
54 # Check if SSH key exists
55 #-------------------------------------------------------------------------------
56 if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
57 error "You must have SSH keypair in order to run this script!"
60 #-------------------------------------------------------------------------------
61 # We are using sudo so we need to make sure that env_reset is not present
62 #-------------------------------------------------------------------------------
63 sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
65 #-------------------------------------------------------------------------------
66 # Check if Python Virtual Environment is installed
67 #-------------------------------------------------------------------------------
68 if ! command -v virtualenv &> /dev/null; then
69 error "VirtualEnv not found. Please install."
72 #-------------------------------------------------------------------------------
73 # Check if PIP Installs Packages is installed
74 #-------------------------------------------------------------------------------
75 if ! command -v pip &> /dev/null; then
76 error "PIP not found. Please install."
79 #-------------------------------------------------------------------------------
80 # Check is libvirt is installed
81 #-------------------------------------------------------------------------------
82 for tool in ansible yq virsh jq; do
83 if ! command -v "$tool" &> /dev/null; then
84 error "$tool not found. Please install."
88 #-------------------------------------------------------------------------------
89 # Check if user belongs to libvirt's group
90 #-------------------------------------------------------------------------------
91 libvirt_group="libvirt"
92 # shellcheck disable=SC1091
93 source /etc/os-release || source /usr/lib/os-release
94 if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
97 if ! groups | grep "$libvirt_group"; then
98 error "$(id -nu) user doesn't belong to $libvirt_group group."
102 # Get jumphost VM PXE IP
109 assert_non_empty "$host" "get_ip - host parameter not provided"
111 PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_network)
112 assert_non_empty "$PXE_NETWORK" "PXE network for jump VM not defined in IDF."
114 PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
115 assert_non_empty "$PXE_IF_INDEX" "Index of PXE interface not found in IDF."
117 PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
118 assert_non_empty "$PXE_IF_IP" "IP of PXE interface not found in PDF."
123 # Get public MAC for VM
130 assert_non_empty "$host" "get_mac - host parameter not provided"
132 PUB_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.public_network)
133 assert_non_empty "$PUB_NETWORK" "Public network for jump VM not defined in IDF."
135 PUB_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PUB_NETWORK".interface)
136 assert_non_empty "$PUB_IF_INDEX" "Index of public interface not found in IDF."
138 PUB_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PUB_IF_INDEX"].mac_address)
139 assert_non_empty "$PUB_IF_MAC" "MAC of public interface not found in PDF."
145 if [[ "$DEPLOYMENT" == "full" ]]; then
146 ip=$(get_host_pxe_ip "jumphost")
148 mac=$(get_host_pub_mac "jumphost")
149 JUMPHOST_NAME=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.name)
150 ipblock=$(virsh domifaddr "$JUMPHOST_NAME" --full | grep "$mac" | awk '{print $4}' | tail -n 1)
151 assert_non_empty "$ipblock" "IP subnet for VM not available."
157 # Copy files needed by Infra engine & BMRA in the jumphost VM
160 docker_config="/opt/kuberef/docker_config"
161 scp -r -o StrictHostKeyChecking=no \
162 "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
163 "$USERNAME@${vm_ip}:$PROJECT_ROOT"
164 if [[ "$DEPLOYMENT" != "full" ]]; then
165 scp -r -o StrictHostKeyChecking=no \
167 "$USERNAME@${vm_ip}:.ssh/id_rsa"
169 if [ -f "$docker_config" ]; then
170 scp -r -o StrictHostKeyChecking=no \
171 "$docker_config" "$USERNAME@${vm_ip}:$PROJECT_ROOT"
177 # shellcheck disable=SC2087
178 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
179 # Install and run cloud-infra
180 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
181 ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
182 git clone https://gerrit.nordix.org/infra/engine.git
183 cd ${PROJECT_ROOT}/engine/engine && git checkout ${ENGINE_COMMIT_ID}
184 cp ${PROJECT_ROOT}/${VENDOR}/{pdf.yaml,idf.yaml} \
185 ${PROJECT_ROOT}/engine/engine
187 ${PROJECT_ROOT}/engine/engine/deploy.sh -s ironic -d ${DISTRO} \
188 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
189 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
193 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
195 # Set Upper limit of number nodes in RI2 cluster (starting from 0)
196 NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
198 for idx in $(seq 0 "$NODE_MAX_ID"); do
199 NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
201 # shellcheck disable=SC2087
202 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
203 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
204 'bash -s' < ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
209 # k8s Provisioning (currently BMRA)
211 ansible_cmd="/bin/bash -c '"
212 if [[ "$DEPLOYMENT" == "k8s" ]]; then
213 ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
214 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;"
216 ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
218 # shellcheck disable=SC2087
219 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
221 if ! command -v docker; then
222 curl -fsSL https://get.docker.com/ | sh
223 printf "Waiting for docker service..."
224 until sudo docker info; do
229 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
230 git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v21.03 ${PROJECT_ROOT}/container-experience-kits/
231 cp -r ${PROJECT_ROOT}/container-experience-kits/examples/${BMRA_PROFILE}/group_vars ${PROJECT_ROOT}/container-experience-kits/
233 if [ -f "${PROJECT_ROOT}/docker_config" ]; then
234 cp ${PROJECT_ROOT}/docker_config \
235 ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials/vars/main.yml
236 cp -r ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials \
237 ${PROJECT_ROOT}/container-experience-kits/roles/
238 cp ${PROJECT_ROOT}/${INSTALLER}/patched_k8s.yml \
239 ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/k8s.yml
241 cp ${PROJECT_ROOT}/${INSTALLER}/{inventory.ini,ansible.cfg} \
242 ${PROJECT_ROOT}/container-experience-kits/
243 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
244 ${PROJECT_ROOT}/container-experience-kits/group_vars/
245 cp ${PROJECT_ROOT}/${INSTALLER}/patched_cmk_build.yml \
246 ${PROJECT_ROOT}/container-experience-kits/roles/cmk_install/tasks/main.yml
247 cp ${PROJECT_ROOT}/${INSTALLER}/patched_vfio.yml \
248 ${PROJECT_ROOT}/container-experience-kits/roles/sriov_nic_init/tasks/bind_vf_driver.yml
249 cp ${PROJECT_ROOT}/${INSTALLER}/patched_rhel_packages.yml \
250 ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
251 cp ${PROJECT_ROOT}/${INSTALLER}/patched_packages.yml \
252 ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/main.yml
253 cp ${PROJECT_ROOT}/${INSTALLER}/patched_kubespray_requirements.txt \
254 ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/kubespray/requirements.txt
256 sudo docker run --rm \
257 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
258 -e PROFILE=${BMRA_PROFILE} \
259 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
260 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra21.03-install:centos \
265 # Copy kubeconfig to the appropriate location needed by functest containers
267 # TODO Use Kubespray variables in BMRA to simplify this
268 MASTER_IP=$(get_host_pxe_ip "nodes[0]")
269 # shellcheck disable=SC2087
270 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
271 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
274 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
275 # Direct scp to the specified location doesn't work due to permission/ssh-keys
276 scp -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
277 if [ -d "/home/opnfv/functest-kubernetes" ]; then
278 sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
282 # Creates a python virtual environment
283 creates_virtualenv() {
284 if [ ! -d "$CURRENTPATH/.venv" ]; then
287 # shellcheck disable=SC1090
288 source "$CURRENTPATH/.venv/bin/activate"
289 pip install -r "$CURRENTPATH/requirements.txt"
292 # Executes a specific Ansible playbook
294 ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
295 if [ "${DEBUG:-false}" == "true" ]; then
298 eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"