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