su622b | f5b6899 | 2018-04-23 12:02:24 -0400 | [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 | export KAFKA_BROKER_ID=1 |
| 13 | fi |
| 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 |
| 18 | if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then |
| 19 | export KAFKA_ZOOKEEPER_CONNECT=$(env | grep ZK.*PORT_2181_TCP= | sed -e 's|.*tcp://||' | paste -sd ,) |
| 20 | fi |
| 21 | |
| 22 | if [[ -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 |
| 25 | fi |
| 26 | |
| 27 | if [[ -z "$KAFKA_ADVERTISED_HOST_NAME" && -n "$HOSTNAME_COMMAND" ]]; then |
| 28 | export KAFKA_ADVERTISED_HOST_NAME=$(eval $HOSTNAME_COMMAND) |
| 29 | fi |
| 30 | |
| 31 | for VAR in `env` |
| 32 | do |
| 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 |
| 42 | done |
| 43 | |
| 44 | if [[ -n "$CUSTOM_INIT_SCRIPT" ]] ; then |
| 45 | eval $CUSTOM_INIT_SCRIPT |
| 46 | fi |
| 47 | |
| 48 | |
| 49 | KAFKA_PID=0 |
| 50 | |
| 51 | # see https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.bh35ir4u5 |
| 52 | term_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 |
| 64 | trap "term_handler" SIGHUP SIGINT SIGTERM |
| 65 | create-topics.sh & |
| 66 | $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties & |
| 67 | KAFKA_PID=$! |
| 68 | |
| 69 | wait "$KAFKA_PID" |