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