-
Notifications
You must be signed in to change notification settings - Fork 3
/
zkGenConfig.sh
136 lines (111 loc) · 4.98 KB
/
zkGenConfig.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
# Environment variables that have been set during image build
ZK_USER=${ZK_USER:-"zk"}
ZK_HOME=${ZK_HOME:-"/zk"}
ZK_DATA_DIR=${ZK_DATA_DIR:-"/zk_data"}
ZK_DATALOG_DIR=${ZK_DATALOG_DIR:-"/zk_datalog"}
ZK_LOG_DIR=${ZK_LOG_DIR:-"/zk_log"}
# Additional environment variables required to execute startup scripts
ZK_REPLICAS=${ZK_REPLICAS:-"1"}
ZK_LOG_LEVEL=${ZK_LOG_LEVEL:-"INFO"}
ZK_CONF_DIR=${ZK_CONF_DIR:-"$ZK_HOME/conf"}
ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181}
ZK_SERVER_PORT=${ZK_SERVER_PORT:-2888}
ZK_ELECTION_PORT=${ZK_ELECTION_PORT:-3888}
ZK_TICK_TIME=${ZK_TICK_TIME:-2000}
ZK_INIT_LIMIT=${ZK_INIT_LIMIT:-10}
ZK_SYNC_LIMIT=${ZK_SYNC_LIMIT:-5}
ZK_HEAP_SIZE=${ZK_HEAP_SIZE:-128M}
ZK_MAX_CLIENT_CNXNS=${ZK_MAX_CLIENT_CNXNS:-60}
ZK_MIN_SESSION_TIMEOUT=${ZK_MIN_SESSION_TIMEOUT:- $((ZK_TICK_TIME*2))}
ZK_MAX_SESSION_TIMEOUT=${ZK_MAX_SESSION_TIMEOUT:- $((ZK_TICK_TIME*20))}
ZK_SNAP_RETAIN_COUNT=${ZK_SNAP_RETAIN_COUNT:-3}
ZK_PURGE_INTERVAL=${ZK_PURGE_INTERVAL:-0}
ZK_PEER_TYPE=${ZK_PEER_TYPE:-"participant"}
ZK_DATADIR_AUTOCREATE=${ZK_DATADIR_AUTOCREATE:-"false"}
ZK_STATIC_CONFIG="$ZK_CONF_DIR/zoo.cfg"
ZK_DYNAMIC_CONFIG="$ZK_CONF_DIR/zoo.cfg.dynamic"
ID_FILE="$ZK_DATA_DIR/myid"
LOGGER_PROPS="$ZK_CONF_DIR/log4j.properties"
JAVA_ENV_FILE="$ZK_CONF_DIR/java.env"
HOST=$(hostname -s)
DOMAIN=$(hostname -d)
function print_servers() {
for (( i=0; i<$ZK_REPLICAS; i++ ))
do
echo "server.$i=$NAME-$((i)).$DOMAIN:$ZK_SERVER_PORT:$ZK_ELECTION_PORT:$ZK_PEER_TYPE;$ZK_CLIENT_PORT"
done
}
function validate_env() {
echo "Validating environment"
if [ -z $ZK_REPLICAS ]; then
echo "ZK_REPLICAS is a mandatory environment variable"
exit 1
fi
if [[ $HOST =~ ^([A-Za-z0-9]*)(-)([0-9]+)((-.*)?)$ ]]; then
NAME=$(echo $HOST | cut -d '-' -f 1)
ORD=$(echo $HOST | cut -d '-' -f 2)
else
echo "Failed to match valid hostname '[name]-[ordinal]' $HOST"
exit 1
fi
MY_ID=$((ORD))
env
echo "ENSEMBLE"
print_servers
echo "Environment validation successful"
}
function create_config() {
rm -f $ZK_STATIC_CONFIG $ZK_DYNAMIC_CONFIG
echo "Creating ZooKeeper configuration"
echo "# This file was autogenerated by zkGenConfig.sh" >> $ZK_STATIC_CONFIG
echo "# DO NOT EDIT DIRECTLY" >> $ZK_STATIC_CONFIG
echo "standaloneEnabled=false" >> $ZK_STATIC_CONFIG
echo "4lw.commands.whitelist=ruok, imok, mntr" >> $ZK_STATIC_CONFIG
echo "zookeeper.datadir.autocreate=$ZK_DATADIR_AUTOCREATE" >> $ZK_STATIC_CONFIG
echo "dataDir=$ZK_HOME$ZK_DATA_DIR" >> $ZK_STATIC_CONFIG
echo "dataLogDir=$ZK_HOME$ZK_DATALOG_DIR" >> $ZK_STATIC_CONFIG
echo "tickTime=$ZK_TICK_TIME" >> $ZK_STATIC_CONFIG
echo "initLimit=$ZK_INIT_LIMIT" >> $ZK_STATIC_CONFIG
echo "syncLimit=$ZK_SYNC_LIMIT" >> $ZK_STATIC_CONFIG
echo "maxClientCnxns=$ZK_MAX_CLIENT_CNXNS" >> $ZK_STATIC_CONFIG
echo "minSessionTimeout=$ZK_MIN_SESSION_TIMEOUT" >> $ZK_STATIC_CONFIG
echo "maxSessionTimeout=$ZK_MAX_SESSION_TIMEOUT" >> $ZK_STATIC_CONFIG
echo "autopurge.snapRetainCount=$ZK_SNAP_RETAIN_COUNT" >> $ZK_STATIC_CONFIG
echo "autopurge.purgeInteval=$ZK_PURGE_INTERVAL" >> $ZK_STATIC_CONFIG
echo "dynamicConfigFile=$ZK_DYNAMIC_CONFIG" >> $ZK_STATIC_CONFIG
echo "Wrote ZooKeeper static configuration file to $ZK_STATIC_CONFIG"
print_servers >> $ZK_DYNAMIC_CONFIG
echo "Wrote ZooKeeper dynamic configuration file to $ZK_DYNAMIC_CONFIG"
if [ ! -d $ZK_HOME$ZK_DATA_DIR/version-2 ]; then
mkdir -p $ZK_HOME$ZK_DATA_DIR/version-2
echo "Created ZooKeeper data directory $ZK_HOME$ZK_DATA_DIR/version-2"
fi
if [ ! -f $ZK_HOME$ID_FILE ]; then
echo $MY_ID >> $ID_FILE
echo "Wrote ZooKeeper id file in $ID_FILE"
fi
if [ ! -d $ZK_HOME$ZK_DATALOG_DIR/version-2 ]; then
mkdir -p $ZK_HOME$ZK_DATALOG_DIR/version-2
echo "Created ZooKeeper log directory $ZK_HOME$ZK_DATALOG_DIR/version-2"
fi
}
function create_log4j_props() {
rm -f $LOGGER_PROPS
echo "Creating Zookeeper log4j properties"
echo "zookeeper.root.logger=CONSOLE" >> $LOGGER_PROPS
echo "zookeeper.console.threshold="$ZK_LOG_LEVEL >> $LOGGER_PROPS
echo "log4j.rootLogger=\${zookeeper.root.logger}" >> $LOGGER_PROPS
echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender" >> $LOGGER_PROPS
echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}" >> $LOGGER_PROPS
echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout" >> $LOGGER_PROPS
echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" >> $LOGGER_PROPS
echo "Wrote log4j configuration to $LOGGER_PROPS"
}
function create_java_env() {
rm -f $JAVA_ENV_FILE
echo "ZOO_LOG_DIR=$ZK_HOME$ZK_LOG_DIR" >> $JAVA_ENV_FILE
echo "JVMFLAGS=\"-Xmx$ZK_HEAP_SIZE -Xms$ZK_HEAP_SIZE\"" >> $JAVA_ENV_FILE
echo "Wrote JVM configuration to $JAVA_ENV_FILE"
}
validate_env && create_config && create_log4j_props && create_java_env