Merge "[COMMON] Add custom certs into AAF truststore"
[oom.git] / kubernetes / robot / instantiate-k8s.sh
1 #!/bin/bash
2 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
17
18 NAMESPACE=
19 FOLDER=
20 POLL=0
21
22 check_required_parameter () {
23   # arg1 = parameter
24   # arg2 = parameter name
25   if [ -z "$1" ]; then
26     echo "$2 was not was provided. This parameter is required."
27     exit 1
28   fi
29 }
30
31 check_optional_paramater () {
32   # arg1 = parameter
33   # arg2 = parameter name
34   if [ -z $1 ]; then
35     echo "$2"
36   else
37     echo "$1"
38   fi
39 }
40
41 while test $# -gt 0; do
42   case "$1" in
43     -h|--help)
44       echo "./instantiate-k8s.sh [options]"
45       echo " "
46       echo "required:"
47       echo "-n, --namespace <namespace>       namespace that robot pod is running under."
48       echo "-f, --folder <folder>             path to folder containing heat templates, preloads, and vnf-details.json."
49       echo " "
50       echo "additional options:"
51       echo "-p, --poll                        some cloud environments (like azure) have a short time out value when executing"
52       echo "                                  kubectl. If your shell exits before the testsuite finishes, using this option"
53       echo "                                  will poll the testsuite logs every 30 seconds until the test finishes."
54       echo " "
55       echo "This script executes the VNF instantiation robot testsuite."
56       echo "- It copies the VNF folder to the robot container that is part of the ONAP deployment."
57       echo "- It models, distributes, and instantiates a heat-based VNF."
58       echo "- It copies the logs to an output directory, and creates a tarball for upload to the OVP portal."
59       echo ""
60       exit 0
61       ;;
62     -n|--namespace)
63       shift
64       NAMESPACE=$1
65       shift
66       ;;
67     -f|--folder)
68       shift
69       FOLDER=$1
70       shift
71       ;;
72     -p|--poll)
73       shift
74       POLL=1
75       ;;
76     *)
77       echo "Unknown Argument $1. Try running with --help."
78       exit 0
79       ;;
80   esac
81 done
82
83 check_required_parameter "$NAMESPACE" "--namespace"
84 check_required_parameter "$FOLDER" "--folder"
85
86 TAG="instantiate_vnf_ovp"
87
88 if [ ! -d "$FOLDER" ]; then
89   echo "VNF folder $FOLDER does not exist, exiting."
90   exit 1
91 fi
92
93 BUILDNUM="$$"
94 OUTPUT_DIRECTORY=/tmp/vnfdata.${BUILDNUM}
95
96 set -x
97
98 POD=$(kubectl --namespace $NAMESPACE get pods | sed 's/ .*//'| grep robot)
99 export GLOBAL_BUILD_NUMBER=$(kubectl --namespace $NAMESPACE exec  ${POD}  -- bash -c "ls -1q /share/logs/ | wc -l")
100 TAGS="-i $TAG"
101 ETEHOME=/var/opt/ONAP
102 OUTPUT_FOLDER=$(printf %04d $GLOBAL_BUILD_NUMBER)_ete_instantiate_vnf
103 DISPLAY_NUM=$(($GLOBAL_BUILD_NUMBER + 90))
104 VARIABLEFILES="-V /share/config/robot_properties.py"
105 VARIABLES="$VARIABLES -v GLOBAL_BUILD_NUMBER:${BUILDNUM}"
106
107 echo "Copying the VNF folder into robot pod..."
108 kubectl --namespace $NAMESPACE cp $FOLDER ${POD}:/tmp/vnfdata.${BUILDNUM}
109
110
111 echo "Executing instantiation..."
112
113 if [ $POLL = 1 ]; then
114   kubectl --namespace $NAMESPACE exec ${POD} -- bash -c "${ETEHOME}/runTags.sh ${VARIABLEFILES} ${VARIABLES} -d /share/logs/${OUTPUT_FOLDER} ${TAGS} --listener ${ETEHOME}/testsuite/eteutils/robotframework-onap/listeners/OVPListener.py --display $DISPLAY_NUM > /tmp/vnf_instantiation.$BUILDNUM.log 2>&1 &"
115
116   pid=`kubectl --namespace $NAMESPACE exec ${POD} -- bash -c "pgrep runTags.sh -n"`
117
118   if [ -z "$pid" ]; then
119     echo "robot testsuite unable to start"
120     exit 1
121   fi
122
123   kubectl --namespace $NAMESPACE exec ${POD} -- bash -c "while ps -p \"$pid\" --no-headers | grep -v defunct; do echo \$'\n\n'; echo \"Testsuite still running \"\`date\`; echo \"LOG FILE: \"; tail -10 /tmp/vnf_instantiation.$BUILDNUM.log; sleep 30; done"
124
125 else
126   kubectl --namespace $NAMESPACE exec ${POD} -- bash -c "${ETEHOME}/runTags.sh ${VARIABLEFILES} ${VARIABLES} -d /share/logs/${OUTPUT_FOLDER} ${TAGS} --listener ${ETEHOME}/testsuite/eteutils/robotframework-onap/listeners/OVPListener.py --display $DISPLAY_NUM"
127 fi
128
129 set +x
130
131 echo "testsuite has finished"
132
133 mkdir -p "$OUTPUT_DIRECTORY"
134 echo "Copying Results from pod..."
135
136 kubectl --namespace $NAMESPACE cp ${POD}:share/logs/$OUTPUT_FOLDER/summary/report.json "$OUTPUT_DIRECTORY"/report.json
137 kubectl --namespace $NAMESPACE cp ${POD}:share/logs/$OUTPUT_FOLDER/summary/stack_report.json "$OUTPUT_DIRECTORY"/stack_report.json
138 kubectl --namespace $NAMESPACE cp ${POD}:share/logs/$OUTPUT_FOLDER/summary/results.json "$OUTPUT_DIRECTORY"/results.json
139 kubectl --namespace $NAMESPACE cp ${POD}:share/logs/$OUTPUT_FOLDER/log.html "$OUTPUT_DIRECTORY"/log.html
140
141 pushd .
142
143 # echo -e "import hashlib\nwith open(\"README.md\", \"r\") as f: bytes = f.read()\nreadable_hash = hashlib.sha256(bytes).hexdigest()\nprint(readable_hash)" | python
144
145 cd "$OUTPUT_DIRECTORY"
146 tar -czvf vnf_heat_results.tar.gz *
147
148 popd
149
150 echo "VNF test results: $OUTPUT_DIRECTORY/vnf_heat_results.tar.gz"