Merge "iec: Add robot parsing for SEBA tests"
[ci-management.git] / jjb / shell / run_bluval.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2019 ENEA and others.
4 # valentin.radulescu@enea.com
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 set -e
11 set -o errexit
12 set -o pipefail
13 export PATH=$PATH:/home/jenkins/.local/bin
14
15 cwd=$(pwd)
16 current_user=$(whoami)
17 is_optional="false"
18 pull="false"
19
20 info ()  {
21     logger -s -t "run_blu_val.info" "$*"
22 }
23
24 has_substring() {
25     [[ $1 =~ $2 ]]
26 }
27
28 change_res_owner() {
29 # change owner of results created by root in container
30     if [ -d "$results_dir" ]
31     then
32         sudo chown -R "$current_user" "$results_dir"
33     fi
34 }
35
36 usage() {
37     echo "usage: $0" >&2
38     echo "[-n <blueprint_name>">&2
39     echo "[-b <blueprint_yaml> blueprint definition">&2
40     echo "[-k <k8s_config_dir> k8s config dir">&2
41     echo "[-j <cluster_master_ip> cluster master IP">&2
42     echo "[-u <ssh_user> ssh user">&2
43     echo "[-s <ssh_key>] path to ssh key">&2
44     echo "[-c <custmom_var_file> ] path to variables yaml file">&2
45     echo "[-l <layer> ] blueprint layer">&2
46     echo "[-o ] run optional tests">&2
47     echo "[-v <version> ] version">&2
48 }
49
50 verify_connectivity() {
51     local ip=$1
52     info "Verifying connectivity to $ip..."
53     # shellcheck disable=SC2034
54     for i in $(seq 0 10); do
55         if ping -c 1 -W 1 "$ip" > /dev/null; then
56             info "$ip is reachable!"
57             return 0
58         fi
59         sleep 1
60     done
61     error "Can not talk to $ip."
62 }
63
64 error () {
65     logger -s -t "run_blu_val.error" "$*"
66     exit 1
67 }
68
69 # Get options from shell
70 while getopts "j:k:u:s:b:l:r:n:opv:" optchar; do
71     case "${optchar}" in
72         j) cluster_master_ip=${OPTARG} ;;
73         k) k8s_config_dir=${OPTARG} ;;
74         s) ssh_key=${OPTARG} ;;
75         b) blueprint_yaml=${OPTARG} ;;
76         l) blueprint_layer=${OPTARG} ;;
77         n) blueprint_name=${OPTARG} ;;
78         u) sh_user=${OPTARG} ;;
79         o) is_optional="true"  ;;
80         p) pull="true"  ;;
81         v) version=${OPTARG} ;;
82         *) echo "Non-option argument: '-${OPTARG}'" >&2
83            usage
84            exit 2
85            ;;
86     esac
87 done
88
89 # Blueprint name is mandatory
90 blueprint_name=${blueprint_name:-$BLUEPRINT}
91 if [ -z "$blueprint_name" ]
92 then
93     usage
94     error "Please specify blueprint name. "
95 fi
96
97 # Use cwd/kube for k8s config
98 input="$cwd/kube"
99
100 # Initialize ssh key used
101 ssh_key=${ssh_key:-$CLUSTER_SSH_KEY}
102 # K8s config directory
103 k8s_config_dir=${k8s_config_dir:-$input}
104 mkdir -p "$k8s_config_dir"
105
106 # Testing configuration
107 version=${version:-$VERSION}
108 results_dir=$cwd/results
109 cluster_master_ip=${cluster_master_ip:-$CLUSTER_MASTER_IP}
110 ssh_user=${sh_user:-$CLUSTER_SSH_USER}
111 blueprint_layer=${blueprint_layer:-$LAYER}
112
113 if [ "$blueprint_layer" == "k8s" ] || [ -z "$blueprint_layer" ]
114 then
115     if [ -z "$cluster_master_ip" ]
116     then
117         usage
118         error "Please provide valid IP address to access the k8s cluster."
119     fi
120     verify_connectivity "${cluster_master_ip}"
121     if [[ -n $CLUSTER_SSH_PASSWORD ]]
122     then
123         sshpass -p "${CLUSTER_SSH_PASSWORD}" scp -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -r\
124              "${ssh_user}@${cluster_master_ip}:~/.kube/*" "$k8s_config_dir"
125     else
126         scp -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -i"$ssh_key" -r\
127             "${ssh_user}"@"${cluster_master_ip}":~/.kube/* "$k8s_config_dir"
128     fi
129 fi
130
131 if [[ -n $blueprint_yaml ]]
132 then
133     cp "$blueprint_yaml" ./bluval/
134 fi
135
136 volumes_path="$cwd/bluval/volumes.yaml"
137 # update information in volumes yaml
138 sed -i \
139     -e "/ssh_key_dir/{n; s@local: ''@local: '$cwd/ssh_key_dir'@}" \
140     -e "/kube_config_dir/{n; s@local: ''@local: '$k8s_config_dir'@}" \
141     -e "/custom_variables_file/{n; s@local: ''@local: '$cwd/tests/variables.yaml'@}" \
142     -e "/blueprint_dir/{n; s@local: ''@local: '$cwd/bluval/'@}" \
143     -e "/results_dir/{n; s@local: ''@local: '$results_dir'@}" \
144     "$volumes_path"
145
146 # create ssh_key_dir
147 mkdir -p "$cwd/ssh_key_dir"
148
149 # copy ssh_key in ssh_key_dir
150 cp "$ssh_key" "$cwd/ssh_key_dir/id_rsa"
151
152 variables_path="$cwd/tests/variables.yaml"
153 # update information in variables yaml
154 sed -i \
155     -e "s@host: [0-9]*.[0-9]*.[0-9]*.[0-9]*@host: $cluster_master_ip@" \
156     -e "s@username: [A-Za-z0-9_]* @username: $ssh_user@" \
157     "$variables_path"
158
159 if [[ -n $blueprint_layer ]]
160 then
161     options="-l$blueprint_layer"
162 fi
163 if [ "$is_optional" == "true" ] || [ "$OPTIONAL" == "yes" ]
164 then
165     options+=" -o"
166 fi
167 if [ "$pull" == "true" ] || [ "$PULL" == "yes" ]
168 then
169     options+=" -p"
170 fi
171
172 set +e
173 if python3 --version > /dev/null; then
174     # shellcheck disable=SC2086
175     python3 bluval/blucon.py $options "$blueprint_name"
176 else
177     # shellcheck disable=SC2086
178     VALIDATION_DIR="$WORKSPACE" RESULTS_DIR="$WORKSPACE/results" \
179         bluval/blucon.sh $options "$blueprint_name"
180 fi
181
182 # even if the script fails we need to change the owner of results
183 # shellcheck disable=SC2181
184 if [ $? -ne 0 ]; then
185     change_res_owner
186     error "Bluval validation FAIL "
187 fi
188 set -e
189
190 change_res_owner
191 if has_substring "$NODE_NAME" "snd-"
192 then
193     echo "In sandbox the logs are not pushed"
194 else
195     TIMESTAMP=$(date +'%Y%m%d-%H%M%S')
196     NEXUS_URL=https://nexus.akraino.org/
197     NEXUS_PATH="${LAB_SILO}/bluval_results/${blueprint_name}/${VERSION}/${TIMESTAMP}"
198     zip -r results.zip ./results
199     lftools deploy nexus-zip "$NEXUS_URL" logs "$NEXUS_PATH" results.zip
200     rm results.zip
201 fi
202
203 rm -f ~/.netrc