Skip to content

Commit 722b633

Browse files
author
John Edge
committed
Check for port settings in consul config files in the SysV init script
Fixes tomhillable#69
1 parent e204913 commit 722b633

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

SOURCES/consul.init

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ logfile="/var/log/$prog"
3636
export GOMAXPROCS=${GOMAXPROCS:-2}
3737
export MAXWAIT=${MAXWAIT:-10}
3838

39+
get_port() {
40+
# get port number from the consul config file and assign to a variable
41+
# passed in as the first argument
42+
#
43+
# usage: get_port var_name
44+
local __setvar=$1
45+
local config_dir
46+
local config_file
47+
local port
48+
49+
config_dir="${CONFIG_DIR:-/etc/consul.d}"
50+
51+
if [ -d "${config_dir}" ]; then
52+
config_file=$(find "${config_dir}" -name \*.json)
53+
if [ -f "${config_file}" ]; then
54+
# grab port number from json file with some awk magic
55+
port=$(
56+
/bin/awk '/ports/ { getline; print $2 }' "${config_file}"
57+
)
58+
fi
59+
fi
60+
61+
eval $__setvar="${port:-8500}"
62+
}
63+
64+
3965
start() {
4066
[ -x $exec ] || exit 5
4167

@@ -62,9 +88,13 @@ start() {
6288
local pid=$(< ${pidfile})
6389
local curwait=0
6490
local ready=0
91+
local consul_port
92+
93+
# assign $consul_port based on what the `get_port` func returns
94+
get_port consul_port
6595

6696
while checkpid ${pid} && [ $curwait -lt ${MAXWAIT} ] && [ $ready -ne 1 ]; do
67-
if netstat -nptl | grep -q "^tcp.*:8500.*LISTEN \+${pid}\/${prog}"; then
97+
if netstat -nptl | grep -q "^tcp.*:${consul_port}.*LISTEN \+${pid}\/${prog}"; then
6898
ready=1
6999
else
70100
sleep 1

SOURCES/consul.sysconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
CMD_OPTS="agent -config-dir=/etc/consul.d -data-dir=/var/lib/consul"
1+
CONFIG_DIR="/etc/consul.d"
2+
DATA_DIR="/var/lib/consul"
3+
CMD_OPTS="agent -config-dir=${CONFIG_DIR} -data-dir=${DATA_DIR}"
24
#GOMAXPROCS=4

0 commit comments

Comments
 (0)