2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
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 ##############################################################################
13 if sudo virsh list --all | grep " ${VM_NAME} .*running" ; then
14 sudo virsh destroy "$VM_NAME"
16 if sudo virsh list --all | grep " ${VM_NAME} " ; then
17 sudo virsh undefine "$VM_NAME"
19 sudo rm -rf "/var/lib/libvirt/images/$VM_NAME"
24 check_prerequisites() {
25 echo "Info : Check prerequisites"
27 #-------------------------------------------------------------------------------
28 # We shouldn't be running as root
29 #-------------------------------------------------------------------------------
30 if [[ "$(whoami)" == "root" ]]; then
31 echo "ERROR : This script must not be run as root!"
32 echo " Please switch to a regular user before running the script."
36 #-------------------------------------------------------------------------------
37 # Check for passwordless sudo
38 #-------------------------------------------------------------------------------
39 if ! sudo -n "true"; then
40 echo "ERROR : passwordless sudo is needed for '$(id -nu)' user."
44 #-------------------------------------------------------------------------------
45 # Check if SSH key exists
46 #-------------------------------------------------------------------------------
47 if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
48 echo "ERROR : You must have SSH keypair in order to run this script!"
52 #-------------------------------------------------------------------------------
53 # We are using sudo so we need to make sure that env_reset is not present
54 #-------------------------------------------------------------------------------
55 sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
57 #-------------------------------------------------------------------------------
58 # Check if Ansible is installed
59 #-------------------------------------------------------------------------------
60 if ! command -v ansible &> /dev/null; then
61 echo "ERROR : Ansible not found. Please install."
65 #-------------------------------------------------------------------------------
66 # Check is libvirt is installed
67 #-------------------------------------------------------------------------------
68 if ! command -v virsh &> /dev/null; then
69 echo "ERROR : Libvirt not found. Please install."
78 sudo mkdir -p "/var/lib/libvirt/images/$VM_NAME"
79 sudo qemu-img create -f qcow2 \
80 -o backing_file=/var/lib/libvirt/images/ubuntu-18.04.qcow2 \
81 "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" 10G
83 # Create VM cloud-init configuration files
89 - $(cat "$HOME/.ssh/id_rsa.pub")
90 sudo: ['ALL=(ALL) NOPASSWD:ALL']
95 local-hostname: $VM_NAME
99 sudo genisoimage -output "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso" \
100 -volid cidata -joliet -rock user-data meta-data
101 sudo virt-customize -a "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" \
102 --root-password password:"$ROOT_PASSWORD"
103 sudo virt-install --connect qemu:///system --name "$VM_NAME" \
104 --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
105 --disk path="/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2",format=qcow2 \
106 --disk "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso",device=cdrom \
107 --import --network network=default --network bridge="$BRIDGE",model=rtl8139 --noautoconsole
108 jumpbox_ip=$(get_vm_ip)
110 while [ -z "$jumpbox_ip" ]; do
112 jumpbox_ip=$(get_vm_ip)
115 until nc -w5 -z "$jumpbox_ip" 22; do
122 sudo virsh domifaddr "$VM_NAME" | awk 'FNR == 3 {gsub(/\/.*/, ""); print $4}'
126 setup_PXE_network() {
127 # Extract configuration from PDF/IDF
128 PXE_IF=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_interface)
129 PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config.oob.interface)
130 if [[ -z $PXE_IF || -z $PXE_IF_INDEX ]]; then
131 echo 'one or more variables in IDF are undefined'
134 PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].address)
135 PXE_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].mac_address)
136 if [[ -z $PXE_IF_IP || -z $PXE_IF_MAC ]]; then
137 echo 'one or more variables in PDF are incorrect'
140 export NETMASK=255.255.255.0
142 # shellcheck disable=SC2087
143 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
144 sudo ifconfig $PXE_IF up
145 sudo ifconfig $PXE_IF $PXE_IF_IP netmask $NETMASK
146 sudo ifconfig $PXE_IF hw ether $PXE_IF_MAC
150 # Copy files needed by Infra engine & BMRA in the jumphost VM
152 scp -r -o StrictHostKeyChecking=no \
153 "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
154 "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
159 # shellcheck disable=SC2087
160 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
161 # Install and run cloud-infra
162 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
163 ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
164 git clone https://gerrit.nordix.org/infra/engine.git
165 cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
166 ${PROJECT_ROOT}/engine/engine
168 cd ${PROJECT_ROOT}/engine/engine
169 ./deploy.sh -s ironic -d centos7 \
170 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
171 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
175 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
177 # Extract IPs of provisioned nodes from PDF/IDF. When running this function standalone, ensure
178 # to set $PXE_IF_INDEX
179 MASTER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[0].interfaces.["$PXE_IF_INDEX"].address)
180 WORKER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[1].interfaces.["$PXE_IF_INDEX"].address)
182 # shellcheck disable=SC2087
183 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
184 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
185 'bash -s' < ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
186 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
187 'bash -s' < ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
191 # k8s Provisioning (currently BMRA)
193 # shellcheck disable=SC2087
194 ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
196 if ! command -v docker; then
197 curl -fsSL https://get.docker.com/ | sh
198 printf "Waiting for docker service..."
199 until sudo docker info; do
204 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
205 git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
206 cp -r ${PROJECT_ROOT}/container-experience-kits/examples/{group_vars,host_vars} ${PROJECT_ROOT}/container-experience-kits/
207 #TODO Remove this once the reported issue is fixed in the next BMRA Release
208 sed -i '/\openshift/a \ extra_args: --ignore-installed PyYAML' \
209 ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
211 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
212 ${PROJECT_ROOT}/container-experience-kits/
213 cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
214 ${PROJECT_ROOT}/container-experience-kits/group_vars/
215 cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
216 ${PROJECT_ROOT}/container-experience-kits/host_vars/
217 sudo docker run --rm \
218 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
219 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
220 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml