Add log parser support
[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
14 cwd=$(pwd)
15 current_user=$(whoami)
16 is_optional="false"
17
18 info ()  {
19     logger -s -t "run_blu_val.info" "$*"
20 }
21
22 change_res_owner() {
23 # change owner of results created by root in container
24     if [ -d "$results_dir" ]
25     then
26         sudo chown -R "$current_user" "$results_dir"
27     fi
28 }
29
30 usage() {
31     echo "usage: $0" >&2
32     echo "[-n <blueprint_name>">&2
33     echo "[-r <results_dir> results dir">&2
34     echo "[-b <blueprint_yaml> blueprint definition">&2
35     echo "[-k <k8s_config_dir> k8s config dir">&2
36     echo "[-j <k8s_master> k8s master">&2
37     echo "[-u <ssh_user> ssh user">&2
38     echo "[-s <ssh_key>] path to ssh key">&2
39     echo "[-c <custmom_var_file> ] path to variables yaml file">&2
40     echo "[-l <layer> ] blueprint layer">&2
41     echo "[-o ] run optional tests">&2
42     echo "[-v <version> ] version">&2
43 }
44
45 verify_connectivity() {
46     local ip=$1
47     info "Verifying connectivity to $ip..."
48     for i in $(seq 0 10); do
49         if ping -c 1 -W 1 "$ip" > /dev/null; then
50             info "$ip is reachable!"
51             return 0
52         fi
53         sleep 1
54     done
55     error "Can not talk to $ip."
56 }
57
58 error () {
59     logger -s -t "run_blu_val.error" "$*"
60     exit 1
61 }
62
63 # Get options from shell
64 while getopts "j:k:u:s:b:l:r:n:ov:" optchar; do
65     case "${optchar}" in
66         j) k8s_master=${OPTARG} ;;
67         k) k8s_config_dir=${OPTARG} ;;
68         s) ssh_key=${OPTARG} ;;
69         b) blueprint_yaml=${OPTARG} ;;
70         l) blueprint_layer=${OPTARG} ;;
71         r) results_dir=${OPTARG} ;;
72         n) blueprint_name=${OPTARG} ;;
73         u) sh_user=${OPTARG} ;;
74         o) is_optional="true"  ;;
75         v) version=${OPTARG} ;;
76         *) echo "Non-option argument: '-${OPTARG}'" >&2
77            usage
78            exit 2
79            ;;
80     esac
81 done
82
83 # Blueprint name is mandatory
84 blueprint_name=${blueprint_name:-$BLUEPRINT}
85 if [ -z "$blueprint_name" ]
86 then
87     usage
88     error "Please specify blueprint name. "
89 fi
90
91 # Use cwd/kube for k8s config
92 input="$cwd/kube"
93
94 # Initialize ssh key used
95 ssh_key=${ssh_key:-$K8S_SSH_KEY}
96 # K8s config directory
97 k8s_config_dir=${k8s_config_dir:-$input}
98 mkdir -p "$k8s_config_dir"
99
100 # Testing configuration
101 version=${version:-$VERSION}
102 results_dir=${results_dir:-$cwd/results}
103 k8s_master=${k8s_master:-$K8S_MASTER_IP}
104 ssh_user=${sh_user:-$K8S_SSH_USER}
105 blueprint_layer=${blueprint_layer:-$LAYER}
106
107 # If blueprint layer is not defined use k8s by default
108 if [ "$blueprint_layer" == "k8s" ]
109 then
110     if [ -z "$k8s_master" ]
111     then
112         usage
113         error "Please provide valid k8s IP address."
114     fi
115     verify_connectivity "${k8s_master}"
116     if [[ -n $K8S_SSH_PASSWORD ]]
117     then
118         sshpass -p "${K8S_SSH_PASSWORD}" scp -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -r\
119              "${ssh_user}@${k8s_master}:~/.kube/*" "$k8s_config_dir"
120     else
121         scp -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -i"$ssh_key" -r\
122             "${ssh_user}"@"${k8s_master}":~/.kube/* "$k8s_config_dir"
123     fi
124 fi
125
126 if [[ -n $blueprint_yaml ]]
127 then
128     cp "$blueprint_yaml" ./bluval/
129 fi
130
131 volumes_path="$cwd/bluval/volumes.yaml"
132 #update information in volumes yaml
133 sed -i -e "/kube_config_dir/{n; s@local: ''@local: '$k8s_config_dir'@}" -e "/blueprint_dir/{n; s@local: ''@local: '$cwd/bluval/'@}" -e "/results_dir/{n; s@local: ''@local: '$results_dir'@}" "$volumes_path"
134
135 if [[ -n $blueprint_layer ]]
136 then
137     options="-l$blueprint_layer"
138 fi
139 if [ "$is_optional" == "true" ] || [ "$OPTIONAL" == "yes" ]
140 then
141     options+=" -o"
142 fi
143
144 printf 'ok / PASS /\nerror / FAIL /\n' > ./bluval/rules.txt
145
146 set +e
147 # even if the script fails we need to change the owner of results
148 # shellcheck disable=SC2086
149 python3 bluval/blucon.py $options "$blueprint_name"
150
151 if [ $? -ne 0 ]; then
152     change_res_owner
153     error "Bluval validation failed!"
154 fi
155
156 set -e
157
158 change_res_owner