|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2016 Spotify |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Original: https://github.com/spotify/docker-kafka |
| 18 | + |
| 19 | + |
| 20 | +# Optional ENV variables: |
| 21 | +# * ADVERTISED_HOST: the external ip for the container, e.g. `docker-machine ip \`docker-machine active\`` |
| 22 | +# * ADVERTISED_PORT: the external port for Kafka, e.g. 9092 |
| 23 | +# * ZK_CHROOT: the zookeeper chroot that's used by Kafka (without / prefix), e.g. "kafka" |
| 24 | +# * LOG_RETENTION_HOURS: the minimum age of a log file in hours to be eligible for deletion (default is 168, for 1 week) |
| 25 | +# * LOG_RETENTION_BYTES: configure the size at which segments are pruned from the log, (default is 1073741824, for 1GB) |
| 26 | +# * NUM_PARTITIONS: configure the default number of log partitions per topic |
| 27 | +# * AUTO_CREATE_TOPICS: whether to autocreate topics |
| 28 | + |
| 29 | +# Configure advertised host/port if we run in helios |
| 30 | +if [ ! -z "$HELIOS_PORT_kafka" ]; then |
| 31 | + ADVERTISED_HOST=`echo $HELIOS_PORT_kafka | cut -d':' -f 1 | xargs -n 1 dig +short | tail -n 1` |
| 32 | + ADVERTISED_PORT=`echo $HELIOS_PORT_kafka | cut -d':' -f 2` |
| 33 | +fi |
| 34 | + |
| 35 | +# Set the external host and port |
| 36 | +if [ ! -z "$ADVERTISED_HOST" ]; then |
| 37 | + echo "advertised host: $ADVERTISED_HOST" |
| 38 | + if grep -q "^advertised.host.name" $KAFKA_HOME/config/server.properties; then |
| 39 | + sed -r -i "s/#(advertised.host.name)=(.*)/\1=$ADVERTISED_HOST/g" $KAFKA_HOME/config/server.properties |
| 40 | + else |
| 41 | + echo "\nadvertised.host.name=$ADVERTISED_HOST" >> $KAFKA_HOME/config/server.properties |
| 42 | + fi |
| 43 | +fi |
| 44 | +if [ ! -z "$ADVERTISED_PORT" ]; then |
| 45 | + echo "advertised port: $ADVERTISED_PORT" |
| 46 | + if grep -q "^advertised.port" $KAFKA_HOME/config/server.properties; then |
| 47 | + sed -r -i "s/#(advertised.port)=(.*)/\1=$ADVERTISED_PORT/g" $KAFKA_HOME/config/server.properties |
| 48 | + else |
| 49 | + echo "\nadvertised.port=$ADVERTISED_PORT" >> $KAFKA_HOME/config/server.properties |
| 50 | + fi |
| 51 | +fi |
| 52 | + |
| 53 | +# Set the zookeeper chroot |
| 54 | +if [ ! -z "$ZK_CHROOT" ]; then |
| 55 | + # wait for zookeeper to start up |
| 56 | + until /usr/share/zookeeper/bin/zkServer.sh status; do |
| 57 | + sleep 0.1 |
| 58 | + done |
| 59 | + |
| 60 | + # create the chroot node |
| 61 | + echo "create /$ZK_CHROOT \"\"" | /usr/share/zookeeper/bin/zkCli.sh || { |
| 62 | + echo "can't create chroot in zookeeper, exit" |
| 63 | + exit 1 |
| 64 | + } |
| 65 | + |
| 66 | + # configure kafka |
| 67 | + sed -r -i "s/(zookeeper.connect)=(.*)/\1=localhost:2181\/$ZK_CHROOT/g" $KAFKA_HOME/config/server.properties |
| 68 | +fi |
| 69 | + |
| 70 | +# Allow specification of log retention policies |
| 71 | +if [ ! -z "$LOG_RETENTION_HOURS" ]; then |
| 72 | + echo "log retention hours: $LOG_RETENTION_HOURS" |
| 73 | + sed -r -i "s/(log.retention.hours)=(.*)/\1=$LOG_RETENTION_HOURS/g" $KAFKA_HOME/config/server.properties |
| 74 | +fi |
| 75 | +if [ ! -z "$LOG_RETENTION_BYTES" ]; then |
| 76 | + echo "log retention bytes: $LOG_RETENTION_BYTES" |
| 77 | + sed -r -i "s/#(log.retention.bytes)=(.*)/\1=$LOG_RETENTION_BYTES/g" $KAFKA_HOME/config/server.properties |
| 78 | +fi |
| 79 | + |
| 80 | +# Configure the default number of log partitions per topic |
| 81 | +if [ ! -z "$NUM_PARTITIONS" ]; then |
| 82 | + echo "default number of partition: $NUM_PARTITIONS" |
| 83 | + sed -r -i "s/(num.partitions)=(.*)/\1=$NUM_PARTITIONS/g" $KAFKA_HOME/config/server.properties |
| 84 | +fi |
| 85 | + |
| 86 | +# Enable/disable auto creation of topics |
| 87 | +if [ ! -z "$AUTO_CREATE_TOPICS" ]; then |
| 88 | + echo "auto.create.topics.enable: $AUTO_CREATE_TOPICS" |
| 89 | + echo "/nauto.create.topics.enable=$AUTO_CREATE_TOPICS" >> $KAFKA_HOME/config/server.properties |
| 90 | +fi |
| 91 | + |
| 92 | +# Run Kafka |
| 93 | +$KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties |
0 commit comments