0fb916f060b389b67953e4ef8358efd7b0d9c6d3
[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     echo "${GREEN}Environment ready!${NO_COLOR}"
67
68 }
69
70
71 function redeployPod(){
72     echo "Redeploying pod"
73     kubectl scale --replicas=0 deploy ${HVVES_CONTAINER_NAME} -n ${ONAP_NAMESPACE}
74     waitForPodTermination
75     kubectl scale --replicas=1 deploy ${HVVES_CONTAINER_NAME} -n ${ONAP_NAMESPACE}
76     sleep 10s
77
78     waitForPodRedeployment
79
80     updateHvVesImage
81
82     waitForHvVesRunning
83 }
84
85 function waitForPodTermination(){
86     echo "Waiting for pod termination..."
87
88     while [ "${HVVES_POD_NAME}" != "" ] ; do
89       HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
90       sleep 1s
91     done
92
93     echo "Pod terminated"
94 }
95
96 function updateHvVesImage() {
97     echo "Updating HV-VES image"
98     kubectl patch pod ${HVVES_POD_NAME} -n ${ONAP_NAMESPACE} --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"'${HV_VES_IMAGE}'"}]'
99 }
100
101 function deleteKafkaTopic() {
102     echo "Deleting Kafka topic"
103     kubectl exec -it ${KAFKA_ROUTER_0_POD_NAME} -n ${ONAP_NAMESPACE} -- ${KAFKA_TOPIC_RESET_CMD} | eval $HIDE_OUTPUT
104 }
105
106 function waitForPodRedeployment(){
107     HVVES_POD_NAME=""
108     echo "Waiting for pod redeploy..."
109
110     while [ "${HVVES_POD_NAME}" = "" ] ; do
111       HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
112       sleep 1s
113     done
114 }
115
116 function waitForHvVesRunning(){
117     echo "Waiting for HV-VES running..."
118     POD_STATUS="";
119     while [ "${POD_STATUS}" != "2/2" ]; do
120       POD_STATUS=$(kubectl get pod ${HVVES_POD_NAME} -n ${ONAP_NAMESPACE} --no-headers=true | awk '{print $2}')
121       sleep 1s
122     done
123 }
124
125 function loadHvVesPodName(){
126     HVVES_POD_NAME=$(kubectl -n ${ONAP_NAMESPACE} get pods --no-headers=true -o custom-columns=:metadata.name | grep hv-ves-collector)
127 }
128
129
130 if [[ $# -eq 0 ]]; then
131     rebootEnvironment
132 else
133     for arg
134     do
135         case ${arg} in
136             --verbose|-v)
137               VERBOSE=true
138               rebootEnvironment
139               ;;
140             help)
141               usage
142               ;;
143             *)
144               echo "Unknown action: ${arg}" >&2
145               usage
146               ;;
147         esac
148     done
149 fi