blob: cd437fbeba00b88404a0e19399f92cd867a9aa12 [file] [log] [blame]
khenb95fe9a2016-10-05 11:15:25 -07001#!/bin/bash
2
3if [[ -z "$KAFKA_PORT" ]]; then
4 export KAFKA_PORT=9092
5fi
6if [[ -z "$KAFKA_ADVERTISED_PORT" ]]; then
7 export KAFKA_ADVERTISED_PORT=$(docker port `hostname` $KAFKA_PORT | sed -r "s/.*:(.*)/\1/g")
8fi
9if [[ -z "$KAFKA_BROKER_ID" ]]; then
10 # By default auto allocate broker ID
11 export KAFKA_BROKER_ID=-1
12fi
13if [[ -z "$KAFKA_LOG_DIRS" ]]; then
14 export KAFKA_LOG_DIRS="/kafka/kafka-logs-$HOSTNAME"
15fi
16if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then
17 export KAFKA_ZOOKEEPER_CONNECT=$(env | grep ZK.*PORT_2181_TCP= | sed -e 's|.*tcp://||' | paste -sd ,)
18fi
19
20if [[ -n "$KAFKA_HEAP_OPTS" ]]; then
21 sed -r -i "s/(export KAFKA_HEAP_OPTS)=\"(.*)\"/\1=\"$KAFKA_HEAP_OPTS\"/g" $KAFKA_HOME/bin/kafka-server-start.sh
22 unset KAFKA_HEAP_OPTS
23fi
24
25if [[ -z "$KAFKA_ADVERTISED_HOST_NAME" && -n "$HOSTNAME_COMMAND" ]]; then
26 export KAFKA_ADVERTISED_HOST_NAME=$(eval $HOSTNAME_COMMAND)
27fi
28
29for VAR in `env`
30do
31 if [[ $VAR =~ ^KAFKA_ && ! $VAR =~ ^KAFKA_HOME ]]; then
32 kafka_name=`echo "$VAR" | sed -r "s/KAFKA_(.*)=.*/\1/g" | tr '[:upper:]' '[:lower:]' | tr _ .`
33 env_var=`echo "$VAR" | sed -r "s/(.*)=.*/\1/g"`
34 if egrep -q "(^|^#)$kafka_name=" $KAFKA_HOME/config/server.properties; then
35 sed -r -i "s@(^|^#)($kafka_name)=(.*)@\2=${!env_var}@g" $KAFKA_HOME/config/server.properties #note that no config values may contain an '@' char
36 else
37 echo "$kafka_name=${!env_var}" >> $KAFKA_HOME/config/server.properties
38 fi
39 fi
40done
41
42if [[ -n "$CUSTOM_INIT_SCRIPT" ]] ; then
43 eval $CUSTOM_INIT_SCRIPT
44fi
45
46
47KAFKA_PID=0
48
49# see https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.bh35ir4u5
50term_handler() {
51 echo 'Stopping Kafka....'
52 if [ $KAFKA_PID -ne 0 ]; then
53 kill -s TERM "$KAFKA_PID"
54 wait "$KAFKA_PID"
55 fi
56 echo 'Kafka stopped.'
57 exit
58}
59
60
61# Capture kill requests to stop properly
62trap "term_handler" SIGHUP SIGINT SIGTERM
63create-topics.sh &
64$KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties &
65KAFKA_PID=$!
66
67wait