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