Skip to content

Commit

Permalink
Check for port settings in consul config files in the SysV init script
Browse files Browse the repository at this point in the history
  • Loading branch information
John Edge committed Nov 28, 2018
1 parent e204913 commit 722b633
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 31 additions & 1 deletion SOURCES/consul.init
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion SOURCES/consul.sysconfig
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 722b633

Please sign in to comment.