khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | if [[ -z "$KAFKA_PORT" ]]; then |
| 4 | export KAFKA_PORT=9092 |
| 5 | fi |
| 6 | if [[ -z "$KAFKA_ADVERTISED_PORT" ]]; then |
| 7 | export KAFKA_ADVERTISED_PORT=$(docker port `hostname` $KAFKA_PORT | sed -r "s/.*:(.*)/\1/g") |
| 8 | fi |
| 9 | if [[ -z "$KAFKA_BROKER_ID" ]]; then |
| 10 | # By default auto allocate broker ID |
| 11 | export KAFKA_BROKER_ID=-1 |
| 12 | fi |
| 13 | if [[ -z "$KAFKA_LOG_DIRS" ]]; then |
| 14 | export KAFKA_LOG_DIRS="/kafka/kafka-logs-$HOSTNAME" |
| 15 | fi |
| 16 | if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then |
| 17 | export KAFKA_ZOOKEEPER_CONNECT=$(env | grep ZK.*PORT_2181_TCP= | sed -e 's|.*tcp://||' | paste -sd ,) |
| 18 | fi |
| 19 | |
| 20 | if [[ -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 |
| 23 | fi |
| 24 | |
| 25 | if [[ -z "$KAFKA_ADVERTISED_HOST_NAME" && -n "$HOSTNAME_COMMAND" ]]; then |
| 26 | export KAFKA_ADVERTISED_HOST_NAME=$(eval $HOSTNAME_COMMAND) |
| 27 | fi |
| 28 | |
| 29 | for VAR in `env` |
| 30 | do |
| 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 |
| 40 | done |
| 41 | |
| 42 | if [[ -n "$CUSTOM_INIT_SCRIPT" ]] ; then |
| 43 | eval $CUSTOM_INIT_SCRIPT |
| 44 | fi |
| 45 | |
| 46 | |
| 47 | KAFKA_PID=0 |
| 48 | |
| 49 | # see https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.bh35ir4u5 |
| 50 | term_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 |
| 62 | trap "term_handler" SIGHUP SIGINT SIGTERM |
| 63 | create-topics.sh & |
| 64 | $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties & |
| 65 | KAFKA_PID=$! |
| 66 | |
| 67 | wait |