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