Separate tox dependencies
[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 libvirt is installed
54     #-------------------------------------------------------------------------------
55     if ! command -v virsh &> /dev/null; then
56         echo "ERROR : Libvirt not found. Please install."
57         exit 1
58     fi
59
60     #-------------------------------------------------------------------------------
61     # Check if user belongs to libvirt's group
62     #-------------------------------------------------------------------------------
63     libvirt_group="libvirt"
64     # shellcheck disable=SC1091
65     source /etc/os-release || source /usr/lib/os-release
66     if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
67         libvirt_group+="d"
68     fi
69     if ! groups | grep " $libvirt_group "; then
70         echo "ERROR : $(id -nu) user doesn't belong to $libvirt_group group."
71         exit 1
72     fi
73 }
74
75 # Get jumphost VM IP
76 get_host_pxe_ip() {
77     local PXE_NETWORK
78     local PXE_IF_INDEX
79     local PXE_IF_IP
80
81     host=$1
82     if [[ "$host" == "" ]]; then
83         echo "ERROR : get_ip - host parameter not provided"
84         exit 1
85     fi
86
87     PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml  engine.pxe_network)
88     if [[ "$PXE_NETWORK" == "" ]]; then
89         echo "ERROR : PXE network for jump VM not defined in IDF."
90         exit 1
91     fi
92
93     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
94     if [[ "$PXE_IF_INDEX" == "" ]]; then
95         echo "ERROR : Index of PXE interface not found in IDF."
96         exit 1
97     fi
98
99     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
100     if [[ "$PXE_IF_IP" == "" ]]; then
101         echo "ERROR : IP of PXE interface not found in PDF."
102         exit 1
103     fi
104     echo "$PXE_IF_IP"
105 }
106
107 get_vm_ip() {
108     ip=$(get_host_pxe_ip "jumphost")
109     echo "$ip"
110 }
111
112
113 # Copy files needed by Infra engine & BMRA in the jumphost VM
114 copy_files_jump() {
115     scp -r -o StrictHostKeyChecking=no \
116     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
117     "$USERNAME@$(get_vm_ip):$PROJECT_ROOT"
118 }
119
120 # Host Provisioning
121 provision_hosts() {
122     # shellcheck disable=SC2087
123     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
124 # Install and run cloud-infra
125 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
126     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
127     git clone https://gerrit.nordix.org/infra/engine.git
128     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
129     ${PROJECT_ROOT}/engine/engine
130 fi
131 cd ${PROJECT_ROOT}/engine/engine
132 ./deploy.sh -s ironic -d centos7 \
133 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
134 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
135 EOF
136 }
137
138 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
139 setup_network() {
140     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
141     WORKER_IP=$(get_host_pxe_ip "nodes[1]")
142 # SSH to jumphost
143     # shellcheck disable=SC2087
144     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
145 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
146     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
147 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
148     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
149 EOF
150 }
151
152 # k8s Provisioning (currently BMRA)
153 provision_k8s() {
154     # shellcheck disable=SC2087
155     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
156 # Install BMRA
157 if ! command -v docker; then
158     curl -fsSL https://get.docker.com/ | sh
159     printf "Waiting for docker service..."
160     until sudo docker info; do
161         printf "."
162         sleep 2
163     done
164 fi
165 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
166     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
167     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/group_vars ${PROJECT_ROOT}/container-experience-kits/
168 #TODO Remove this once the reported issue is fixed in the next BMRA Release
169     sed -i '/\openshift/a \    extra_args: --ignore-installed PyYAML' \
170         ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
171 fi
172 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
173     ${PROJECT_ROOT}/container-experience-kits/
174 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
175     ${PROJECT_ROOT}/container-experience-kits/group_vars/
176 sudo docker run --rm \
177 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
178 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
179 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
180 ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/cluster.yml
181 EOF
182 }