forked from jhaynie/github-webhook-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-kafka.sh
executable file
·119 lines (95 loc) · 3.5 KB
/
start-kafka.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
if [[ -z "$KAFKA_PORT" ]]; then
export KAFKA_PORT=9092
fi
# if [[ -z "$KAFKA_ADVERTISED_PORT" ]]; then
# export KAFKA_ADVERTISED_PORT=$(docker port `hostname` $KAFKA_PORT | sed -r "s/.*:(.*)/\1/g")
# fi
if [[ -z "$KAFKA_BROKER_ID" ]]; then
# By default auto allocate broker ID
export KAFKA_BROKER_ID=-1
fi
if [[ -z "$KAFKA_LOG_DIRS" ]]; then
export KAFKA_LOG_DIRS="/kafka/kafka-logs-$HOSTNAME"
fi
# if [[ -z "$KAFKA_ZOOKEEPER_CONNECT" ]]; then
# export KAFKA_ZOOKEEPER_CONNECT=$(env | grep ZK.*PORT_2181_TCP= | sed -e 's|.*tcp://||' | paste -sd ,)
# fi
if [[ -n "$KAFKA_HEAP_OPTS" ]]; then
sed -r -i "s/(export KAFKA_HEAP_OPTS)=\"(.*)\"/\1=\"$KAFKA_HEAP_OPTS\"/g" $KAFKA_HOME/bin/kafka-server-start.sh
unset KAFKA_HEAP_OPTS
fi
if [[ ! -z "$ADV_HOST" ]]; then
export KAFKA_ADVERTISED_HOST_NAME=$ADV_HOST
fi
if [[ -z "$KAFKA_ADVERTISED_HOST_NAME" && -n "$HOSTNAME_COMMAND" ]]; then
export KAFKA_ADVERTISED_HOST_NAME=$(eval $HOSTNAME_COMMAND)
fi
# Allow specification of log retention policies
if [ ! -z "$LOG_RETENTION_HOURS" ]; then
echo "log retention hours: $LOG_RETENTION_HOURS"
sed -r -i "s/(log.retention.hours)=(.*)/\1=$LOG_RETENTION_HOURS/g" $KAFKA_HOME/config/server.properties
fi
if [ ! -z "$LOG_RETENTION_BYTES" ]; then
echo "log retention bytes: $LOG_RETENTION_BYTES"
sed -r -i "s/#(log.retention.bytes)=(.*)/\1=$LOG_RETENTION_BYTES/g" $KAFKA_HOME/config/server.properties
fi
# Enable/disable auto creation of topics
if [ ! -z "$AUTO_CREATE_TOPICS" ]; then
echo "auto.create.topics.enable: $AUTO_CREATE_TOPICS"
echo "auto.create.topics.enable=$AUTO_CREATE_TOPICS" >> $KAFKA_HOME/config/server.properties
fi
if [ ! -z "$KAFKA_STANDALONE" ]; then
echo "offsets.topic.replication.factor=1" >> $KAFKA_HOME/config/server.properties
fi
for VAR in `env`
do
if [[ $VAR =~ ^KAFKA_ && ! $VAR =~ ^KAFKA_HOME ]]; then
kafka_name=`echo "$VAR" | sed -r "s/KAFKA_(.*)=.*/\1/g" | tr '[:upper:]' '[:lower:]' | tr _ .`
env_var=`echo "$VAR" | sed -r "s/(.*)=.*/\1/g"`
if egrep -q "(^|^#)$kafka_name=" $KAFKA_HOME/config/server.properties; then
sed -r -i "s@(^|^#)($kafka_name)=(.*)@\2=${!env_var}@g" $KAFKA_HOME/config/server.properties #note that no config values may contain an '@' char
else
echo "$kafka_name=${!env_var}" >> $KAFKA_HOME/config/server.properties
fi
fi
done
# Generate the config only if it doesn't exist
if [ ! -f "$ZOO_CONF_DIR/zoo.cfg" ]; then
CONFIG="$ZOO_CONF_DIR/zoo.cfg"
echo "clientPort=$ZOO_PORT" >> "$CONFIG"
echo "dataDir=$ZOO_DATA_DIR" >> "$CONFIG"
echo "dataLogDir=$ZOO_DATA_LOG_DIR" >> "$CONFIG"
if [ ! -z "$KAFKA_ADVERTISED_HOST_NAME" ]; then
echo "advertised.host.name=$KAFKA_ADVERTISED_HOST_NAME" >> "$CONFIG"
fi
echo 'tickTime=2000' >> "$CONFIG"
echo 'initLimit=5' >> "$CONFIG"
echo 'syncLimit=2' >> "$CONFIG"
for server in $ZOO_SERVERS; do
echo "$server" >> "$CONFIG"
done
fi
# Write myid only if it doesn't exist
if [ ! -f "$ZOO_DATA_DIR/myid" ]; then
echo "${ZOO_MY_ID:-1}" > "$ZOO_DATA_DIR/myid"
fi
if [[ -n "$CUSTOM_INIT_SCRIPT" ]] ; then
eval $CUSTOM_INIT_SCRIPT
fi
KAFKA_PID=0
# see https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.bh35ir4u5
term_handler() {
echo 'Stopping Kafka....'
if [ $KAFKA_PID -ne 0 ]; then
kill -s TERM "$KAFKA_PID"
wait "$KAFKA_PID"
fi
echo 'Kafka stopped.'
exit
}
# Capture kill requests to stop properly
trap "term_handler" SIGHUP SIGINT SIGTERM
supervisord -n &
KAFKA_PID=$!
wait