2024-12-24 13:40:15 +08:00

85 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
##################################################
# Mount dir #
# - /opt/zk/data #
# - /opt/zk/dataLog #
# - /opt/zk/logs #
# ENV #
# - MYID #
# - JVMFLAGS #
# - _CONF_* #
##################################################
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
PIDS=
GOT_SIGTERM=
function Print {
local file=/dev/null
[ '-f' = "$1" ] && file=$2 && shift && shift
date +"[%F %T] $*" | tee -a $file
}
function Quit {
while :; do
pkill -f java && Print killing java ... || break
sleep 1
done
Print Container stopped.
test -n "$GOT_SIGTERM"
}
function Usage {
Print 'This container should run with
**root user**
**/opt/zk/{data,dataLog,logs} mounted from host**
'
}
function ModifyConf {
Print Modifying server.properties ...
local kv=
local conf='conf/zoo.cfg'
while read kv; do
[ -z "$kv" ] && break
Print Modifying property: ${kv%%=*} ...
sed -i "/^${kv%%=*} *=/d" $conf
echo "$kv" >> $conf
done <<< "$(env | grep '^_CONF_' | sed 's/_CONF_//')"
sed -i -e '/^dataDir/d' -e '/^dataLogDir/d' $conf
echo -e 'dataDir=/opt/zk/data\ndataLogDir=/opt/zk/dataLog' >> $conf
if [ ! -e data/myid ]; then
Print Generating myid ...
echo $MYID > data/myid
fi
}
function StartProc {
Print Starting zookeeper ...
./bin/zkServer.sh start-foreground &>> logs/zk.out &
PIDS="$PIDS $!"
Print Zookeeper started.
}
function Main {
local pid=
cd /opt/zk
Usage
ModifyConf
StartProc
trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
while [ -z "$GOT_SIGTERM" ] && sleep 2; do
for pid in $PIDS; do
[ ! -e /proc/$pid ] && Print Unexpected error! && exit
done
done
}
# Start here
Main