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