diff --git a/SOURCES/consul.init b/SOURCES/consul.init index 1c35522..ebfbd11 100755 --- a/SOURCES/consul.init +++ b/SOURCES/consul.init @@ -36,6 +36,32 @@ logfile="/var/log/$prog" export GOMAXPROCS=${GOMAXPROCS:-2} export MAXWAIT=${MAXWAIT:-10} +get_port() { + # get port number from the consul config file and assign to a variable + # passed in as the first argument + # + # usage: get_port var_name + local __setvar=$1 + local config_dir + local config_file + local port + + config_dir="${CONFIG_DIR:-/etc/consul.d}" + + if [ -d "${config_dir}" ]; then + config_file=$(find "${config_dir}" -name \*.json) + if [ -f "${config_file}" ]; then + # grab port number from json file with some awk magic + port=$( + /bin/awk '/ports/ { getline; print $2 }' "${config_file}" + ) + fi + fi + + eval $__setvar="${port:-8500}" +} + + start() { [ -x $exec ] || exit 5 @@ -62,9 +88,13 @@ start() { local pid=$(< ${pidfile}) local curwait=0 local ready=0 + local consul_port + + # assign $consul_port based on what the `get_port` func returns + get_port consul_port while checkpid ${pid} && [ $curwait -lt ${MAXWAIT} ] && [ $ready -ne 1 ]; do - if netstat -nptl | grep -q "^tcp.*:8500.*LISTEN \+${pid}\/${prog}"; then + if netstat -nptl | grep -q "^tcp.*:${consul_port}.*LISTEN \+${pid}\/${prog}"; then ready=1 else sleep 1 diff --git a/SOURCES/consul.sysconfig b/SOURCES/consul.sysconfig index c99741d..00ee6e1 100644 --- a/SOURCES/consul.sysconfig +++ b/SOURCES/consul.sysconfig @@ -1,2 +1,4 @@ -CMD_OPTS="agent -config-dir=/etc/consul.d -data-dir=/var/lib/consul" +CONFIG_DIR="/etc/consul.d" +DATA_DIR="/var/lib/consul" +CMD_OPTS="agent -config-dir=${CONFIG_DIR} -data-dir=${DATA_DIR}" #GOMAXPROCS=4