Refactor performance scripts
[dcaegen2/collectors/hv-ves.git] / tools / performance / cloud / reboot-test-environment.sh
1 #!/usr/bin/env bash
2 # ============LICENSE_START=======================================================
3 # dcaegen2-collectors-veshv
4 # ================================================================================
5 # Copyright (C) 2019 NOKIA
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19
20 ONAP_NAMESPACE=onap
21 HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
22 HVVES_CONTAINER_NAME=dep-dcae-hv-ves-collector
23 HV_VES_IMAGE="nexus3.onap.org:10001/onap/org.onap.dcaegen2.collectors.hv-ves.hv-collector-main:latest"
24 KAFKA_ROUTER_0_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep router-kafka-0)
25 KAFKA_TOPIC_RESET_CMD='kafka-topics --delete --zookeeper message-router-zookeeper:2181 --topic HV_VES_PERF3GPP'
26 HIDE_OUTPUT='grep abc | grep 123'
27 YELLOW=$(tput setaf 3)
28 GREEN=$(tput setaf 2)
29 CYAN=$(tput setaf 6)
30 NO_COLOR=$(tput sgr0)
31 VERBOSE="false"
32
33 function formatOutput(){
34     read line
35     echo "${YELLOW}$line${NO_COLOR}"
36     while read line; do
37       if [ ${VERBOSE} == "true" ]; then
38         echo "    $line"
39       fi
40     done
41 }
42
43 function usage() {
44     echo ""
45     echo "Reboot test environment for performance tests"
46     echo "Usage $0"
47     echo "  -v  --verbose : reboot the test environment verbosely"
48     echo "  help          : print usage"
49     echo "Example invocation:"
50     echo "./reboot-test-environment.sh"
51     echo "./reboot-test-environment.sh --verbose"
52     echo ""
53 }
54
55 function rebootEnvironment(){
56     echo "${CYAN}Rebooting test environment${NO_COLOR}"
57
58     ./cloud-based-performance-test.sh clean | formatOutput
59
60     redeployPod | formatOutput
61
62     deleteKafkaTopic | formatOutput
63
64     ./cloud-based-performance-test.sh setup | formatOutput
65
66     ./cloud-based-performance-test.sh stop | formatOutput
67
68     ./cloud-based-performance-test.sh reset_producers | formatOutput
69
70     echo "${GREEN}Environment ready!${NO_COLOR}"
71
72 }
73
74
75 function redeployPod(){
76     echo "Redeploying pod"
77     kubectl scale --replicas=0 deploy ${HVVES_CONTAINER_NAME} -n ${ONAP_NAMESPACE}
78     waitForPodTermination
79     kubectl scale --replicas=1 deploy ${HVVES_CONTAINER_NAME} -n ${ONAP_NAMESPACE}
80     sleep 10s
81
82     waitForPodRedeployment
83
84     updateHvVesImage
85
86     waitForHvVesRunning
87 }
88
89 function waitForPodTermination(){
90     echo "Waiting for pod termination..."
91
92     while [ "${HVVES_POD_NAME}" != "" ] ; do
93       HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
94       sleep 1s
95     done
96
97     echo "Pod terminated"
98 }
99
100 function updateHvVesImage() {
101     echo "Updating HV-VES image"
102     kubectl patch pod ${HVVES_POD_NAME} -n ${ONAP_NAMESPACE} --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"'${HV_VES_IMAGE}'"}]'
103 }
104
105 function deleteKafkaTopic() {
106     echo "Deleting Kafka topic"
107     kubectl exec -it ${KAFKA_ROUTER_0_POD_NAME} -n ${ONAP_NAMESPACE} -- ${KAFKA_TOPIC_RESET_CMD} | eval $HIDE_OUTPUT
108 }
109
110 function waitForPodRedeployment(){
111     HVVES_POD_NAME=""
112     echo "Waiting for pod redeploy..."
113
114     while [ "${HVVES_POD_NAME}" = "" ] ; do
115       HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
116       sleep 1s
117     done
118 }
119
120 function waitForHvVesRunning(){
121     echo "Waiting for HV-VES running..."
122     POD_STATUS="";
123     while [ "${POD_STATUS}" != "2/2" ]; do
124       POD_STATUS=$(kubectl get pod ${HVVES_POD_NAME} -n ${ONAP_NAMESPACE} --no-headers=true | awk '{print $2}')
125       sleep 1s
126     done
127 }
128
129 function loadHvVesPodName(){
130     HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
131 }
132
133
134 if [[ $# -eq 0 ]]; then
135     rebootEnvironment
136 else
137     for arg
138     do
139         case ${arg} in
140             --verbose|-v)
141               VERBOSE=true
142               rebootEnvironment
143               ;;
144             help)
145               usage
146               ;;
147             *)
148               echo "Unknown action: ${arg}" >&2
149               usage
150               ;;
151         esac
152     done
153 fi