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