Extending PDF and IDF of Ericsson pod2 to all nodes
[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     sudo virt-customize -a "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" \
50         --root-password password:"$ROOT_PASSWORD"
51     sudo virt-install --connect qemu:///system --name "$VM_NAME" \
52         --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
53         --disk path="/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2",format=qcow2 \
54         --disk "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso",device=cdrom \
55         --import --network network=default --network bridge="$BRIDGE",model=rtl8139 --noautoconsole
56     jumpbox_ip=$(get_vm_ip)
57     i=0
58     while [ -z "$jumpbox_ip" ]; do
59         sleep $((++i))
60         jumpbox_ip=$(get_vm_ip)
61     done
62     i=0
63     until nc -w5 -z "$jumpbox_ip" 22; do
64         sleep $((++i))
65     done
66 }
67
68 # Get jumphost VM IP
69 get_vm_ip() {
70     sudo virsh domifaddr "$VM_NAME" | awk 'FNR == 3 {gsub(/\/.*/, ""); print $4}'
71 }
72
73 # Setup PXE network
74 setup_PXE_network() {
75 # Extract configuration from PDF/IDF
76     PXE_IF=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_interface)
77     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config.oob.interface)
78     if [[ -z $PXE_IF || -z $PXE_IF_INDEX ]]; then
79         echo 'one or more variables in IDF are undefined'
80         exit 1
81     fi
82     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].address)
83     PXE_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.interfaces.["$PXE_IF_INDEX"].mac_address)
84     if [[ -z $PXE_IF_IP || -z $PXE_IF_MAC ]]; then
85         echo 'one or more variables in PDF are incorrect'
86         exit 1
87     fi
88     export NETMASK=255.255.255.0
89 # SSH to jumphost
90     # shellcheck disable=SC2087
91     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
92 sudo ifconfig $PXE_IF up
93 sudo ifconfig $PXE_IF $PXE_IF_IP netmask $NETMASK
94 sudo ifconfig $PXE_IF hw ether $PXE_IF_MAC
95 EOF
96 }
97
98 # Copy files needed by Infra engine & BMRA in the jumphost VM
99 copy_files_jump() {
100     scp -r -o StrictHostKeyChecking=no \
101     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
102     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
103 }
104
105 # Host Provisioning
106 provision_hosts() {
107     # shellcheck disable=SC2087
108     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
109 # Install and run cloud-infra
110 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
111     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
112     git clone https://gerrit.nordix.org/infra/engine.git
113     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
114     ${PROJECT_ROOT}/engine/engine
115 fi
116 cd ${PROJECT_ROOT}/engine/engine
117 ./deploy.sh -s ironic -d centos7 \
118 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
119 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
120 EOF
121 }
122
123 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
124 setup_network() {
125 # Extract IPs of provisioned nodes from PDF/IDF. When running this function standalone, ensure
126 # to set $PXE_IF_INDEX
127     MASTER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[0].interfaces.["$PXE_IF_INDEX"].address)
128     WORKER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[1].interfaces.["$PXE_IF_INDEX"].address)
129 # SSH to jumphost
130     # shellcheck disable=SC2087
131     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
132 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
133     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
134 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
135     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
136 EOF
137 }
138
139 # k8s Provisioning (currently BMRA)
140 provision_k8s() {
141     # shellcheck disable=SC2087
142     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
143 # Install BMRA
144 if ! command -v docker; then
145     curl -fsSL https://get.docker.com/ | sh
146     printf "Waiting for docker service..."
147     until sudo docker info; do
148         printf "."
149         sleep 2
150     done
151 fi
152 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
153     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
154     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/group_vars examples/host_vars ${PROJECT_ROOT}/container-experience-kits/
155 #TODO Remove this once the reported issue is fixed in the next BMRA Release
156     sed -i '/\openshift/a \    extra_args: --ignore-installed PyYAML' \
157          ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
158 fi
159 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
160     ${PROJECT_ROOT}/container-experience-kits/
161 cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
162     ${PROJECT_ROOT}/container-experience-kits/group_vars/
163 cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
164     ${PROJECT_ROOT}/container-experience-kits/host_vars/
165 sudo docker run --rm \
166 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
167 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
168 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml
169 EOF
170 }