129a3a60ed09ac9622c9c40af7c75062e9a0f363
[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.sh "$VM_NAME"
26     jumpbox_ip=$(get_vm_ip)
27     i=0
28     while [ -z $jumpbox_ip ]; do
29         sleep $((++i))
30         jumpbox_ip=$(get_vm_ip)
31     done
32     i=0
33     until nc -w5 -z $jumpbox_ip 22; do
34         sleep $((++i))
35     done
36 }
37
38 # Get jumphost VM IP
39 get_vm_ip() {
40     sudo virsh domifaddr "$VM_NAME" | awk 'FNR == 3 {gsub(/\/.*/, ""); print $4}'
41 }
42
43 # Setup PXE network
44 setup_PXE_network() {
45     # shellcheck disable=SC2087
46     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
47 sudo ifconfig $PXE_IF up
48 sudo ifconfig $PXE_IF $PXE_IF_IP netmask $NETMASK
49 sudo ifconfig $PXE_IF hw ether $PXE_IF_MAC
50 EOF
51 }
52
53 # Copy files needed by Infra engine & BMRA in the jumphost VM
54 copy_files_jump() {
55     scp -r -o StrictHostKeyChecking=no \
56     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
57     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
58 }
59
60 # Host Provisioning
61 provision_hosts() {
62     # shellcheck disable=SC2087
63     ssh -tT "$USERNAME"@"$(get_vm_ip)" << EOF
64 # Install and run cloud-infra
65 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
66     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
67     git clone https://gerrit.nordix.org/infra/engine.git
68     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
69     ${PROJECT_ROOT}/engine/engine
70 fi
71 cd ${PROJECT_ROOT}/engine/engine
72 ./deploy.sh -s ironic -d centos7 \
73 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
74 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
75 EOF
76 }
77
78 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
79 setup_network() {
80     # shellcheck disable=SC2087
81     ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
82 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
83     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
84 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
85     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
86 EOF
87 }
88
89 # k8s Provisioning (currently BMRA)
90 provision_k8s() {
91     # shellcheck disable=SC2087
92     ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
93 # Install BMRA
94 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
95     curl -fsSL https://get.docker.com/ | sh
96     printf "Waiting for docker service..."
97     until sudo docker info; do
98         printf "."
99         sleep 2
100     done
101     git clone https://github.com/intel/container-experience-kits.git
102     cd ${PROJECT_ROOT}/container-experience-kits
103     git checkout v1.4.1
104     git submodule update --init
105     cp -r examples/group_vars examples/host_vars .
106     cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
107     ${PROJECT_ROOT}/container-experience-kits/
108     cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
109     ${PROJECT_ROOT}/container-experience-kits/group_vars/
110     cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
111     ${PROJECT_ROOT}/container-experience-kits/host_vars/
112 fi
113 sudo service docker start
114 sudo docker run --rm \
115 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
116 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
117 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml
118 EOF
119 }