Update env variable extraction and VM creation steps
[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 # Create jumphost VM
24 create_jump() {
25 # Create VM image
26     sudo mkdir -p "/var/lib/libvirt/images/$VM_NAME"
27     sudo qemu-img create -f qcow2 \
28         -o backing_file=/var/lib/libvirt/images/ubuntu-18.04.qcow2 \
29         "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" 10G
30
31 # Create VM cloud-init configuration files
32     cat <<EOL > user-data
33     #cloud-config
34     users:
35       - name: $USERNAME
36         ssh-authorized-keys:
37           - $(cat "$HOME/.ssh/id_rsa.pub")
38         sudo: ['ALL=(ALL) NOPASSWD:ALL']
39         groups: sudo
40         shell: /bin/bash
41 EOL
42     cat <<EOL > meta-data
43     local-hostname: $VM_NAME
44 EOL
45
46 # Create VM
47     sudo genisoimage  -output "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso" \
48         -volid cidata -joliet -rock user-data meta-data
49
50     sudo virt-install --connect qemu:///system --name "$VM_NAME" \
51         --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
52         --disk path="/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2",format=qcow2 \
53         --disk "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso",device=cdrom \
54         --import --network network=default --network bridge="$BRIDGE",model=rtl8139 --noautoconsole
55     jumpbox_ip=$(get_vm_ip)
56     i=0
57     while [ -z "$jumpbox_ip" ]; do
58         sleep $((++i))
59         jumpbox_ip=$(get_vm_ip)
60     done
61     i=0
62     until nc -w5 -z "$jumpbox_ip" 22; do
63         sleep $((++i))
64     done
65 }
66
67 # Get jumphost VM IP
68 get_vm_ip() {
69     sudo virsh domifaddr "$VM_NAME" | awk 'FNR == 3 {gsub(/\/.*/, ""); print $4}'
70 }
71
72 # Setup PXE network
73 setup_PXE_network() {
74 # Extract configuration from PDF/IDF
75     PXE_IF=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_interface)
76     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config.oob.interface)
77     if [[ -z $PXE_IF || -z $PXE_IF_INDEX ]]; then
78         echo 'one or more variables in IDF are undefined'
79         exit 1
80     fi
81     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].address)
82     PXE_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].mac_address)
83     if [[ -z $PXE_IF_IP || -z $PXE_IF_MAC ]]; then
84         echo 'one or more variables in PDF are incorrect'
85         exit 1
86     fi
87     export NETMASK=255.255.255.0
88 # SSH to jumphost
89     # shellcheck disable=SC2087
90     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
91 sudo ifconfig $PXE_IF up
92 sudo ifconfig $PXE_IF $PXE_IF_IP netmask $NETMASK
93 sudo ifconfig $PXE_IF hw ether $PXE_IF_MAC
94 EOF
95 }
96
97 # Copy files needed by Infra engine & BMRA in the jumphost VM
98 copy_files_jump() {
99     scp -r -o StrictHostKeyChecking=no \
100     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
101     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
102 }
103
104 # Host Provisioning
105 provision_hosts() {
106     # shellcheck disable=SC2087
107     ssh -tT "$USERNAME"@"$(get_vm_ip)" << EOF
108 # Install and run cloud-infra
109 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
110     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
111     git clone https://gerrit.nordix.org/infra/engine.git
112     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
113     ${PROJECT_ROOT}/engine/engine
114 fi
115 cd ${PROJECT_ROOT}/engine/engine
116 ./deploy.sh -s ironic -d centos7 \
117 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
118 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
119 EOF
120 }
121
122 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
123 setup_network() {
124 # Extract IPs of provisioned nodes from PDF/IDF. When running this function standalone, ensure
125 # to set $PXE_IF_INDEX
126     MASTER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[0].interfaces.["$PXE_IF_INDEX"].address)
127     WORKER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[1].interfaces.["$PXE_IF_INDEX"].address)
128 # SSH to jumphost
129     # shellcheck disable=SC2087
130     ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
131 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
132     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
133 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
134     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
135 EOF
136 }
137
138 # k8s Provisioning (currently BMRA)
139 provision_k8s() {
140     # shellcheck disable=SC2087
141     ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
142 # Install BMRA
143 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
144     curl -fsSL https://get.docker.com/ | sh
145     printf "Waiting for docker service..."
146     until sudo docker info; do
147         printf "."
148         sleep 2
149     done
150     git clone https://github.com/intel/container-experience-kits.git
151     cd ${PROJECT_ROOT}/container-experience-kits
152     git checkout v1.4.1
153     git submodule update --init
154     cp -r examples/group_vars examples/host_vars .
155     cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
156     ${PROJECT_ROOT}/container-experience-kits/
157     cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
158     ${PROJECT_ROOT}/container-experience-kits/group_vars/
159     cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
160     ${PROJECT_ROOT}/container-experience-kits/host_vars/
161 fi
162 sudo service docker start
163 sudo docker run --rm \
164 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
165 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
166 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml
167 EOF
168 }