f0e1213a582e135fad89a5945ae79e6e4da08e71
[kuberef.git] / functions.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) Ericsson AB and others
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 check_prerequisites() {
12     echo "Info  : Check prerequisites"
13
14     #-------------------------------------------------------------------------------
15     # We shouldn't be running as root
16     #-------------------------------------------------------------------------------
17     if [[ "$(whoami)" == "root" ]]; then
18         echo "ERROR : This script must not be run as root!"
19         echo "        Please switch to a regular user before running the script."
20         exit 1
21     fi
22
23     #-------------------------------------------------------------------------------
24     # Check for passwordless sudo
25     #-------------------------------------------------------------------------------
26     if ! sudo -n "true"; then
27         echo "ERROR : passwordless sudo is needed for '$(id -nu)' user."
28         exit 1
29     fi
30
31     #-------------------------------------------------------------------------------
32     # Check if SSH key exists
33     #-------------------------------------------------------------------------------
34     if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
35         echo "ERROR : You must have SSH keypair in order to run this script!"
36         exit 1
37     fi
38
39     #-------------------------------------------------------------------------------
40     # We are using sudo so we need to make sure that env_reset is not present
41     #-------------------------------------------------------------------------------
42     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
43
44     #-------------------------------------------------------------------------------
45     # Check if Ansible is installed
46     #-------------------------------------------------------------------------------
47     if ! command -v ansible &> /dev/null; then
48         echo "ERROR : Ansible not found. Please install."
49         exit 1
50     fi
51
52     #-------------------------------------------------------------------------------
53     # Check is yq is installed
54     #-------------------------------------------------------------------------------
55     if ! command -v yq &> /dev/null; then
56         echo "ERROR : yq not found. Please install."
57         exit 1
58     fi
59
60     #-------------------------------------------------------------------------------
61     # Check is libvirt is installed
62     #-------------------------------------------------------------------------------
63     if ! command -v virsh &> /dev/null; then
64         echo "ERROR : Libvirt not found. Please install."
65         exit 1
66     fi
67
68     #-------------------------------------------------------------------------------
69     # Check if user belongs to libvirt's group
70     #-------------------------------------------------------------------------------
71     libvirt_group="libvirt"
72     # shellcheck disable=SC1091
73     source /etc/os-release || source /usr/lib/os-release
74     if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
75         libvirt_group+="d"
76     fi
77     if ! groups | grep " $libvirt_group "; then
78         echo "ERROR : $(id -nu) user doesn't belong to $libvirt_group group."
79         exit 1
80     fi
81 }
82
83 # Get jumphost VM IP
84 get_host_pxe_ip() {
85     local PXE_NETWORK
86     local PXE_IF_INDEX
87     local PXE_IF_IP
88
89     host=$1
90     if [[ "$host" == "" ]]; then
91         echo "ERROR : get_ip - host parameter not provided"
92         exit 1
93     fi
94
95     PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml  engine.pxe_network)
96     if [[ "$PXE_NETWORK" == "" ]]; then
97         echo "ERROR : PXE network for jump VM not defined in IDF."
98         exit 1
99     fi
100
101     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
102     if [[ "$PXE_IF_INDEX" == "" ]]; then
103         echo "ERROR : Index of PXE interface not found in IDF."
104         exit 1
105     fi
106
107     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
108     if [[ "$PXE_IF_IP" == "" ]]; then
109         echo "ERROR : IP of PXE interface not found in PDF."
110         exit 1
111     fi
112     echo "$PXE_IF_IP"
113 }
114
115 get_vm_ip() {
116     ip=$(get_host_pxe_ip "jumphost")
117     echo "$ip"
118 }
119
120
121 # Copy files needed by Infra engine & BMRA in the jumphost VM
122 copy_files_jump() {
123     scp -r -o StrictHostKeyChecking=no \
124     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
125     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
126 }
127
128 # Host Provisioning
129 provision_hosts() {
130     # shellcheck disable=SC2087
131     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
132 # Install and run cloud-infra
133 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
134     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
135     git clone https://gerrit.nordix.org/infra/engine.git
136     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
137     ${PROJECT_ROOT}/engine/engine
138 fi
139 cd ${PROJECT_ROOT}/engine/engine
140 ./deploy.sh -s ironic -d centos7 \
141 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
142 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
143 EOF
144 }
145
146 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
147 setup_network() {
148     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
149     WORKER_IP=$(get_host_pxe_ip "nodes[1]")
150 # SSH to jumphost
151     # shellcheck disable=SC2087
152     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
153 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
154     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
155 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
156     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
157 EOF
158 }
159
160 # k8s Provisioning (currently BMRA)
161 provision_k8s() {
162     # shellcheck disable=SC2087
163     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
164 # Install BMRA
165 if ! command -v docker; then
166     curl -fsSL https://get.docker.com/ | sh
167     printf "Waiting for docker service..."
168     until sudo docker info; do
169         printf "."
170         sleep 2
171     done
172 fi
173 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
174     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
175     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/group_vars ${PROJECT_ROOT}/container-experience-kits/
176 #TODO Remove this once the reported issue is fixed in the next BMRA Release
177     sed -i '/\openshift/a \    extra_args: --ignore-installed PyYAML' \
178         ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
179 fi
180 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
181     ${PROJECT_ROOT}/container-experience-kits/
182 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
183     ${PROJECT_ROOT}/container-experience-kits/group_vars/
184 sudo docker run --rm \
185 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
186 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
187 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
188 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml
189 EOF
190 }