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