87047ad46181934e6363838d61127f76c4cac18f
[dmaap/messagerouter/messageservice.git] / src / main / resources / docker-compose / start-kafka.sh
1 #!/bin/bash
2 #*******************************************************************************
3 #  ============LICENSE_START=======================================================
4 #  org.onap.dmaap
5 #  ================================================================================
6 #  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 #  ================================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
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 #  ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 #  
22 #*******************************************************************************
23
24 if [[ -z "$KAFKA_PORT" ]]; then
25     export KAFKA_PORT=9092
26 fi
27 if [[ -z "$KAFKA_ADVERTISED_PORT" ]]; then
28     export KAFKA_ADVERTISED_PORT=$(docker port `hostname` $KAFKA_PORT | sed -r "s/.*:(.*)/\1/g")
29 fi
30 if [[ -z "$KAFKA_BROKER_ID" ]]; then
31     # By default auto allocate broker ID
32     export KAFKA_BROKER_ID=1
33 fi
34 if [[ -z "$KAFKA_LOG_DIRS" ]]; then
35     export KAFKA_LOG_DIRS="/kafka/kafka-logs-$HOSTNAME"
36 fi
37 if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then
38     export KAFKA_ZOOKEEPER_CONNECT=$(env | grep ZK.*PORT_2181_TCP= | sed -e 's|.*tcp://||' | paste -sd ,)
39 fi
40
41 if [[ -n "$KAFKA_HEAP_OPTS" ]]; then
42     sed -r -i "s/(export KAFKA_HEAP_OPTS)=\"(.*)\"/\1=\"$KAFKA_HEAP_OPTS\"/g" $KAFKA_HOME/bin/kafka-server-start.sh
43     unset KAFKA_HEAP_OPTS
44 fi
45
46 if [[ -z "$KAFKA_ADVERTISED_HOST_NAME" && -n "$HOSTNAME_COMMAND" ]]; then
47     export KAFKA_ADVERTISED_HOST_NAME=$(eval $HOSTNAME_COMMAND)
48 fi
49
50 for VAR in `env`
51 do
52   if [[ $VAR =~ ^KAFKA_ && ! $VAR =~ ^KAFKA_HOME ]]; then
53     kafka_name=`echo "$VAR" | sed -r "s/KAFKA_(.*)=.*/\1/g" | tr '[:upper:]' '[:lower:]' | tr _ .`
54     env_var=`echo "$VAR" | sed -r "s/(.*)=.*/\1/g"`
55     if egrep -q "(^|^#)$kafka_name=" $KAFKA_HOME/config/server.properties; then
56         sed -r -i "s@(^|^#)($kafka_name)=(.*)@\2=${!env_var}@g" $KAFKA_HOME/config/server.properties #note that no config values may contain an '@' char
57     else
58         echo "$kafka_name=${!env_var}" >> $KAFKA_HOME/config/server.properties
59     fi
60   fi
61 done
62
63 if [[ -n "$CUSTOM_INIT_SCRIPT" ]] ; then
64   eval $CUSTOM_INIT_SCRIPT
65 fi
66
67
68 KAFKA_PID=0
69
70 # see https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.bh35ir4u5
71 term_handler() {
72   echo 'Stopping Kafka....'
73   if [ $KAFKA_PID -ne 0 ]; then
74     kill -s TERM "$KAFKA_PID"
75     wait "$KAFKA_PID"
76   fi
77   echo 'Kafka stopped.'
78   exit
79 }
80
81
82 # Capture kill requests to stop properly
83 trap "term_handler" SIGHUP SIGINT SIGTERM
84 create-topics.sh &
85 $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties &
86 KAFKA_PID=$!
87
88 wait "$KAFKA_PID"