88d6c097ae236db5aa5ad7abdb9ba9f5f99c7ea7
[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     # Check for DEPLOYMENT type
35     #-------------------------------------------------------------------------------
36     DEPLOYMENT=${DEPLOYMENT:-full}
37     if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
38         error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
39     fi
40
41     #-------------------------------------------------------------------------------
42     # We shouldn't be running as root
43     #-------------------------------------------------------------------------------
44     if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
45         error "This script must not be run as root! Please switch to a regular user before running the script."
46     fi
47
48     #-------------------------------------------------------------------------------
49     # Check for passwordless sudo
50     #-------------------------------------------------------------------------------
51     if ! sudo -n "true"; then
52         error "passwordless sudo is needed for '$(id -nu)' user."
53     fi
54
55     #-------------------------------------------------------------------------------
56     # Check if SSH key exists
57     #-------------------------------------------------------------------------------
58     if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
59         error "You must have SSH keypair in order to run this script!"
60     fi
61
62     #-------------------------------------------------------------------------------
63     # We are using sudo so we need to make sure that env_reset is not present
64     #-------------------------------------------------------------------------------
65     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
66
67     #-------------------------------------------------------------------------------
68     # Check if some tools are installed
69     #-------------------------------------------------------------------------------
70     for tool in ansible yq virsh jq; do
71         if ! command -v "$tool" &> /dev/null; then
72             error "$tool not found. Please install."
73         fi
74     done
75
76     #-------------------------------------------------------------------------------
77     # Check if user belongs to libvirt's group
78     #-------------------------------------------------------------------------------
79     libvirt_group="libvirt"
80     # shellcheck disable=SC1091
81     source /etc/os-release || source /usr/lib/os-release
82     if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
83         libvirt_group+="d"
84     fi
85     if ! groups | grep "$libvirt_group"; then
86         error "$(id -nu) user doesn't belong to $libvirt_group group."
87     fi
88 }
89
90 # Get jumphost VM PXE IP
91 get_host_pxe_ip() {
92     local PXE_NETWORK
93     local PXE_IF_INDEX
94     local PXE_IF_IP
95
96     host=$1
97     assert_non_empty "$host" "get_ip - host parameter not provided"
98
99     PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_network)
100     assert_non_empty "$PXE_NETWORK" "PXE network for jump VM not defined in IDF."
101
102     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
103     assert_non_empty "$PXE_IF_INDEX" "Index of PXE interface not found in IDF."
104
105     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
106     assert_non_empty "$PXE_IF_IP" "IP of PXE interface not found in PDF."
107
108     echo "$PXE_IF_IP"
109 }
110
111 # Get public MAC for VM
112 get_host_pub_mac() {
113     local PUB_NETWORK
114     local PUB_IF_INDEX
115     local PUB_IF_MAC
116
117     host=$1
118     assert_non_empty "$host" "get_mac - host parameter not provided"
119
120     PUB_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml  engine.public_network)
121     assert_non_empty "$PUB_NETWORK" "Public network for jump VM not defined in IDF."
122
123     PUB_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PUB_NETWORK".interface)
124     assert_non_empty "$PUB_IF_INDEX" "Index of public interface not found in IDF."
125
126     PUB_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PUB_IF_INDEX"].mac_address)
127     assert_non_empty "$PUB_IF_MAC" "MAC of public interface not found in PDF."
128     echo "$PUB_IF_MAC"
129 }
130
131 # Get jumphost VM IP
132 get_vm_ip() {
133     if [[ "$DEPLOYMENT" == "full" ]]; then
134         ip=$(get_host_pxe_ip "jumphost")
135     else
136         mac=$(get_host_pub_mac "jumphost")
137         JUMPHOST_NAME=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.name)
138         ipblock=$(virsh domifaddr "$JUMPHOST_NAME" --full | grep "$mac" | awk '{print $4}' | tail -n 1)
139         assert_non_empty "$ipblock" "IP subnet for VM not available."
140         ip="${ipblock%/*}"
141     fi
142     echo "$ip"
143 }
144
145 # Copy files needed by Infra engine & BMRA in the jumphost VM
146 copy_files_jump() {
147     vm_ip="$(get_vm_ip)"
148     scp -r -o StrictHostKeyChecking=no \
149     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
150     "$USERNAME@${vm_ip}:$PROJECT_ROOT"
151     if [[ "$DEPLOYMENT" != "full" ]]; then
152         scp -r -o StrictHostKeyChecking=no \
153         ~/.ssh/id_rsa \
154         "$USERNAME@${vm_ip}:.ssh/id_rsa"
155     fi
156 }
157
158 # Host Provisioning
159 provision_hosts() {
160     # shellcheck disable=SC2087
161     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
162 # Install and run cloud-infra
163 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
164     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
165     git clone https://gerrit.nordix.org/infra/engine.git
166     cp $PROJECT_ROOT/$VENDOR/{pdf.yaml,idf.yaml} \
167     ${PROJECT_ROOT}/engine/engine
168 fi
169 cd ${PROJECT_ROOT}/engine/engine
170 ./deploy.sh -s ironic -d centos7 \
171 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
172 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
173 EOF
174 }
175
176 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
177 setup_network() {
178     # Set Upper limit of number nodes in RI2 cluster (starting from 0)
179     NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
180
181     for idx in $(seq 0 "$NODE_MAX_ID"); do
182         NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
183         # SSH to jumphost
184         # shellcheck disable=SC2087
185         ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
186 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
187     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
188 EOF
189     done
190 }
191
192 # k8s Provisioning (currently BMRA)
193 provision_k8s() {
194     ansible_cmd="/bin/bash -c '"
195     if [[ "$DEPLOYMENT" == "k8s" ]]; then
196         ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
197         ansible_cmd+="pip install --upgrade pip==9.0.3; pip install ansible==2.9.6;"
198     fi
199     ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/k8s/patch_kubespray.yml; ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
200
201     # shellcheck disable=SC2087
202     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
203 # Install BMRA
204 if ! command -v docker; then
205     curl -fsSL https://get.docker.com/ | sh
206     printf "Waiting for docker service..."
207     until sudo docker info; do
208         printf "."
209         sleep 2
210     done
211 fi
212 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
213     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v2.0.0 ${PROJECT_ROOT}/container-experience-kits/
214     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/${BMRA_PROFILE}/group_vars ${PROJECT_ROOT}/container-experience-kits/
215 #TODO Remove this once the reported issue is fixed in the next BMRA Release
216     if [[ "$DEPLOYMENT" == "full" ]]; then
217         sed -i '/\openshift/a \    extra_args: --ignore-installed PyYAML' \
218         ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
219     fi
220 fi
221 cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
222     ${PROJECT_ROOT}/container-experience-kits/
223 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
224     ${PROJECT_ROOT}/container-experience-kits/group_vars/
225 sudo docker run --rm \
226 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
227 -e PROFILE=${BMRA_PROFILE} \
228 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
229 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \
230 ${ansible_cmd}
231 EOF
232 }
233
234 # Copy kubeconfig to the appropriate location needed by functest containers
235 copy_k8s_config() {
236 # TODO Use Kubespray variables in BMRA to simplify this
237     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
238     # shellcheck disable=SC2087
239     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
240 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
241 EOF
242
243 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
244 # Direct scp to the specified location doesn't work due to permission/ssh-keys
245     scp  -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
246     if [ -d "/home/opnfv/functest-kubernetes" ]; then
247         sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
248     fi
249 }
250
251 # Executes a specific Ansible playbook
252 run_playbook() {
253     ansible_cmd="$(command -v ansible-playbook)"
254     ansible_cmd+=" -i $CURRENTPATH/inventory/localhost.ini"
255     ansible_cmd+=" -e ansible_python_interpreter=$(command -v python)"
256     if [ "${DEBUG:-false}" == "true" ]; then
257         ansible_cmd+=" -vvv"
258     fi
259     eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"
260 }