Fixing broken copy of BMRA host_vars
[kuberef.git] / functions.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c)
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 # Clean up
12 clean_up() {
13     if sudo virsh list --all | grep " ${VM_NAME} .*running" ; then
14         sudo virsh destroy "$VM_NAME"
15     fi
16     if sudo virsh list --all | grep " ${VM_NAME} " ; then
17         sudo virsh undefine "$VM_NAME"
18     fi
19     sudo rm -rf "/var/lib/libvirt/images/$VM_NAME"
20     sleep 5
21 }
22
23
24 check_prerequisites() {
25     echo "Info  : Check prerequisites"
26
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."
33         exit 1
34     fi
35
36     #-------------------------------------------------------------------------------
37     # Check for passwordless sudo
38     #-------------------------------------------------------------------------------
39     if ! sudo -n "true"; then
40         echo "ERROR : passwordless sudo is needed for '$(id -nu)' user."
41         exit 1
42     fi
43
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!"
49         exit 1
50     fi
51
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
56
57     #-------------------------------------------------------------------------------
58     # Check if Ansible is installed
59     #-------------------------------------------------------------------------------
60     if ! command -v ansible &> /dev/null; then
61         echo "ERROR : Ansible not found. Please install."
62         exit 1
63     fi
64
65     #-------------------------------------------------------------------------------
66     # Check is libvirt is installed
67     #-------------------------------------------------------------------------------
68     if ! command -v virsh &> /dev/null; then
69         echo "ERROR : Libvirt not found. Please install."
70         exit 1
71     fi
72 }
73
74
75 # Create jumphost VM
76 create_jump() {
77 # Create VM image
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
82
83 # Create VM cloud-init configuration files
84     cat <<EOL > user-data
85     #cloud-config
86     users:
87       - name: $USERNAME
88         ssh-authorized-keys:
89           - $(cat "$HOME/.ssh/id_rsa.pub")
90         sudo: ['ALL=(ALL) NOPASSWD:ALL']
91         groups: sudo
92         shell: /bin/bash
93 EOL
94     cat <<EOL > meta-data
95     local-hostname: $VM_NAME
96 EOL
97
98 # Create VM
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)
109     i=0
110     while [ -z "$jumpbox_ip" ]; do
111         sleep $((++i))
112         jumpbox_ip=$(get_vm_ip)
113     done
114     i=0
115     until nc -w5 -z "$jumpbox_ip" 22; do
116         sleep $((++i))
117     done
118 }
119
120 # Get jumphost VM IP
121 get_vm_ip() {
122     sudo virsh domifaddr "$VM_NAME" | awk 'FNR == 3 {gsub(/\/.*/, ""); print $4}'
123 }
124
125 # Setup PXE network
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'
132         exit 1
133     fi
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'
138         exit 1
139     fi
140     export NETMASK=255.255.255.0
141 # SSH to jumphost
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
147 EOF
148 }
149
150 # Copy files needed by Infra engine & BMRA in the jumphost VM
151 copy_files_jump() {
152     scp -r -o StrictHostKeyChecking=no \
153     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
154     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
155 }
156
157 # Host Provisioning
158 provision_hosts() {
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
167 fi
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
172 EOF
173 }
174
175 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
176 setup_network() {
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)
181 # SSH to jumphost
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
188 EOF
189 }
190
191 # k8s Provisioning (currently BMRA)
192 provision_k8s() {
193     # shellcheck disable=SC2087
194     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
195 # Install BMRA
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
200         printf "."
201         sleep 2
202     done
203 fi
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
210 fi
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
221 EOF
222 }