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