Remove sig_network_serial
[kuberef.git] / functions.sh
1 #!/bin/bash
2
3 # SPDX-FileCopyrightText: 2021 Ericsson AB and others
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 info() {
8     _print_msg "INFO" "$1"
9 }
10
11 error() {
12     _print_msg "ERROR" "$1"
13     exit 1
14 }
15
16 _print_msg() {
17     echo "$(date +%H:%M:%S) - $1: $2"
18 }
19
20 assert_non_empty() {
21     if [ -z "$1" ]; then
22         error "$2"
23     fi
24 }
25 if [ "${DEBUG:-false}" == "true" ]; then
26     set -o xtrace
27 fi
28
29 check_prerequisites() {
30     info "Check prerequisites"
31
32     #-------------------------------------------------------------------------------
33     # Check for DEPLOYMENT type
34     #-------------------------------------------------------------------------------
35     if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
36         error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
37     fi
38
39     #-------------------------------------------------------------------------------
40     # We shouldn't be running as root
41     #-------------------------------------------------------------------------------
42     if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
43         error "This script must not be run as root! Please switch to a regular user before running the script."
44     fi
45
46     #-------------------------------------------------------------------------------
47     # Check for passwordless sudo
48     #-------------------------------------------------------------------------------
49     if ! sudo -n "true"; then
50         error "passwordless sudo is needed for '$(id -nu)' user."
51     fi
52
53     #-------------------------------------------------------------------------------
54     # Check if SSH key exists
55     #-------------------------------------------------------------------------------
56     if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
57         error "You must have SSH keypair in order to run this script!"
58     fi
59
60     #-------------------------------------------------------------------------------
61     # We are using sudo so we need to make sure that env_reset is not present
62     #-------------------------------------------------------------------------------
63     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
64
65     #-------------------------------------------------------------------------------
66     # Check if Python Virtual Environment is installed
67     #-------------------------------------------------------------------------------
68     if ! command -v virtualenv &> /dev/null; then
69         error "VirtualEnv not found. Please install."
70     fi
71
72     #-------------------------------------------------------------------------------
73     # Check if PIP Installs Packages is installed
74     #-------------------------------------------------------------------------------
75     if ! command -v pip &> /dev/null; then
76         error "PIP not found. Please install."
77     fi
78
79     #-------------------------------------------------------------------------------
80     # Check is libvirt is installed
81     #-------------------------------------------------------------------------------
82     for tool in ansible yq virsh jq; do
83         if ! command -v "$tool" &> /dev/null; then
84             error "$tool not found. Please install."
85         fi
86     done
87
88     #-------------------------------------------------------------------------------
89     # Check if user belongs to libvirt's group
90     #-------------------------------------------------------------------------------
91     libvirt_group="libvirt"
92     # shellcheck disable=SC1091
93     source /etc/os-release || source /usr/lib/os-release
94     if [ "${ID,,}" == "ubuntu" ] && [ "$VERSION_ID" == "16.04" ]; then
95         libvirt_group+="d"
96     fi
97     if ! groups | grep "$libvirt_group"; then
98         error "$(id -nu) user doesn't belong to $libvirt_group group."
99     fi
100 }
101
102 # Get jumphost VM PXE IP
103 get_host_pxe_ip() {
104     local PXE_NETWORK
105     local PXE_IF_INDEX
106     local PXE_IF_IP
107
108     host=$1
109     assert_non_empty "$host" "get_ip - host parameter not provided"
110
111     PXE_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml engine.pxe_network)
112     assert_non_empty "$PXE_NETWORK" "PXE network for jump VM not defined in IDF."
113
114     PXE_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PXE_NETWORK".interface)
115     assert_non_empty "$PXE_IF_INDEX" "Index of PXE interface not found in IDF."
116
117     PXE_IF_IP=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PXE_IF_INDEX"].address)
118     assert_non_empty "$PXE_IF_IP" "IP of PXE interface not found in PDF."
119
120     echo "$PXE_IF_IP"
121 }
122
123 # Get public MAC for VM
124 get_host_pub_mac() {
125     local PUB_NETWORK
126     local PUB_IF_INDEX
127     local PUB_IF_MAC
128
129     host=$1
130     assert_non_empty "$host" "get_mac - host parameter not provided"
131
132     PUB_NETWORK=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml  engine.public_network)
133     assert_non_empty "$PUB_NETWORK" "Public network for jump VM not defined in IDF."
134
135     PUB_IF_INDEX=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/idf.yaml idf.net_config."$PUB_NETWORK".interface)
136     assert_non_empty "$PUB_IF_INDEX" "Index of public interface not found in IDF."
137
138     PUB_IF_MAC=$(yq r "$CURRENTPATH"/hw_config/"${VENDOR}"/pdf.yaml "$host".interfaces["$PUB_IF_INDEX"].mac_address)
139     assert_non_empty "$PUB_IF_MAC" "MAC of public interface not found in PDF."
140     echo "$PUB_IF_MAC"
141 }
142
143 # Get jumphost VM IP
144 get_vm_ip() {
145     if [[ "$DEPLOYMENT" == "full" ]]; then
146         ip=$(get_host_pxe_ip "jumphost")
147     else
148         mac=$(get_host_pub_mac "jumphost")
149         JUMPHOST_NAME=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml jumphost.name)
150         ipblock=$(virsh domifaddr "$JUMPHOST_NAME" --full | grep "$mac" | awk '{print $4}' | tail -n 1)
151         assert_non_empty "$ipblock" "IP subnet for VM not available."
152         ip="${ipblock%/*}"
153     fi
154     echo "$ip"
155 }
156
157 # Copy files needed by Infra engine & BMRA in the jumphost VM
158 copy_files_jump() {
159     vm_ip="$(get_vm_ip)"
160     docker_config="/opt/kuberef/docker_config"
161     scp -r -o StrictHostKeyChecking=no \
162     "$CURRENTPATH"/{hw_config/"$VENDOR"/,sw_config/"$INSTALLER"/} \
163     "$USERNAME@${vm_ip}:$PROJECT_ROOT"
164     if [[ "$DEPLOYMENT" != "full" ]]; then
165         scp -r -o StrictHostKeyChecking=no \
166         ~/.ssh/id_rsa \
167         "$USERNAME@${vm_ip}:.ssh/id_rsa"
168     fi
169     if [ -f "$docker_config" ]; then
170         scp -r -o StrictHostKeyChecking=no \
171         "$docker_config" "$USERNAME@${vm_ip}:$PROJECT_ROOT"
172     fi
173 }
174
175 # Host Provisioning
176 provision_hosts() {
177     # shellcheck disable=SC2087
178     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
179 # Install and run cloud-infra
180 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
181     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
182     git clone https://gerrit.nordix.org/infra/engine.git
183     cd ${PROJECT_ROOT}/engine/engine && git checkout ${ENGINE_COMMIT_ID}
184     cp ${PROJECT_ROOT}/${VENDOR}/{pdf.yaml,idf.yaml} \
185     ${PROJECT_ROOT}/engine/engine
186 fi
187 ${PROJECT_ROOT}/engine/engine/deploy.sh -s ironic -d ${DISTRO} \
188 -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml \
189 -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml
190 EOF
191 }
192
193 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
194 setup_network() {
195     # Set Upper limit of number nodes in RI2 cluster (starting from 0)
196     NODE_MAX_ID=$(($(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/idf.yaml --length idf.kubespray.hostnames)-1))
197
198     for idx in $(seq 0 "$NODE_MAX_ID"); do
199         NODE_IP=$(get_host_pxe_ip "nodes[${idx}]")
200         # SSH to jumphost
201         # shellcheck disable=SC2087
202         ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
203 ssh -o StrictHostKeyChecking=no root@${NODE_IP} \
204     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
205 EOF
206     done
207 }
208
209 # k8s Provisioning (currently BMRA)
210 provision_k8s() {
211     ansible_cmd="/bin/bash -c '"
212     if [[ "$DEPLOYMENT" == "k8s" ]]; then
213         ansible-playbook -i "$CURRENTPATH"/sw_config/bmra/inventory.ini "$CURRENTPATH"/playbooks/pre-install.yaml
214         ansible_cmd+="yum -y remove python-netaddr; pip install --upgrade pip; pip install ansible==2.9.17; ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/k8s/patch_kubespray.yml;"
215     fi
216     ansible_cmd+="ansible-playbook -i /bmra/inventory.ini /bmra/playbooks/${BMRA_PROFILE}.yml'"
217
218     # shellcheck disable=SC2087
219     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
220 # Install BMRA
221 if ! command -v docker; then
222     curl -fsSL https://get.docker.com/ | sh
223     printf "Waiting for docker service..."
224     until sudo docker info; do
225         printf "."
226         sleep 2
227     done
228 fi
229 if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
230     git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v21.03 ${PROJECT_ROOT}/container-experience-kits/
231     cp -r ${PROJECT_ROOT}/container-experience-kits/examples/${BMRA_PROFILE}/group_vars ${PROJECT_ROOT}/container-experience-kits/
232 fi
233 if [ -f "${PROJECT_ROOT}/docker_config" ]; then
234     cp ${PROJECT_ROOT}/docker_config \
235         ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials/vars/main.yml
236     cp -r ${PROJECT_ROOT}/${INSTALLER}/dockerhub_credentials \
237         ${PROJECT_ROOT}/container-experience-kits/roles/
238     cp ${PROJECT_ROOT}/${INSTALLER}/patched_k8s.yml \
239         ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/k8s.yml
240 fi
241 cp ${PROJECT_ROOT}/${INSTALLER}/{inventory.ini,ansible.cfg} \
242     ${PROJECT_ROOT}/container-experience-kits/
243 cp ${PROJECT_ROOT}/${INSTALLER}/{all.yml,kube-node.yml} \
244     ${PROJECT_ROOT}/container-experience-kits/group_vars/
245 cp ${PROJECT_ROOT}/${INSTALLER}/patched_cmk_build.yml \
246     ${PROJECT_ROOT}/container-experience-kits/roles/cmk_install/tasks/main.yml
247 cp ${PROJECT_ROOT}/${INSTALLER}/patched_vfio.yml \
248     ${PROJECT_ROOT}/container-experience-kits/roles/sriov_nic_init/tasks/bind_vf_driver.yml
249 cp ${PROJECT_ROOT}/${INSTALLER}/patched_rhel_packages.yml \
250     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/rhel.yml
251 cp ${PROJECT_ROOT}/${INSTALLER}/patched_packages.yml \
252     ${PROJECT_ROOT}/container-experience-kits/roles/bootstrap/install_packages/tasks/main.yml
253 cp ${PROJECT_ROOT}/${INSTALLER}/patched_kubespray_requirements.txt \
254     ${PROJECT_ROOT}/container-experience-kits/playbooks/k8s/kubespray/requirements.txt
255
256 sudo docker run --rm \
257 -e ANSIBLE_CONFIG=/bmra/ansible.cfg \
258 -e PROFILE=${BMRA_PROFILE} \
259 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
260 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra21.03-install:centos \
261 ${ansible_cmd}
262 EOF
263 }
264
265 # Copy kubeconfig to the appropriate location needed by functest containers
266 copy_k8s_config() {
267 # TODO Use Kubespray variables in BMRA to simplify this
268     MASTER_IP=$(get_host_pxe_ip "nodes[0]")
269     # shellcheck disable=SC2087
270     ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
271 scp -o StrictHostKeyChecking=no -q root@$MASTER_IP:/root/.kube/config ${PROJECT_ROOT}/kubeconfig
272 EOF
273
274 # Copy kubeconfig from Jump VM to appropriate location in Jump Host
275 # Direct scp to the specified location doesn't work due to permission/ssh-keys
276     scp  -o StrictHostKeyChecking=no "$USERNAME"@"$(get_vm_ip)":"${PROJECT_ROOT}"/kubeconfig kubeconfig
277     if [ -d "/home/opnfv/functest-kubernetes" ]; then
278         sudo cp kubeconfig /home/opnfv/functest-kubernetes/config
279     fi
280 }
281
282 # Creates a python virtual environment
283 creates_virtualenv() {
284     if [  ! -d "$CURRENTPATH/.venv" ]; then
285         virtualenv .venv
286     fi
287     # shellcheck disable=SC1090
288     source "$CURRENTPATH/.venv/bin/activate"
289     pip install -r "$CURRENTPATH/requirements.txt"
290 }
291
292 # Executes a specific Ansible playbook
293 run_playbook() {
294     ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
295     if [ "${DEBUG:-false}" == "true" ]; then
296         ansible_cmd+=" -vvv"
297     fi
298     eval "$ansible_cmd $CURRENTPATH/playbooks/${1}.yaml"
299 }