Skip to content

Commit

Permalink
feat(conf) new serf_path property
Browse files Browse the repository at this point in the history
This property is especially useful when Serf is not in the sh default
$PATH (used by OpenResty at runtime). Especially useful for travis-ci.
  • Loading branch information
thibaultcha committed Aug 5, 2016
1 parent ebf50de commit e4a14c9
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .ci/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash

set -e

export BUSTED_ARGS="-o gtest -v --exclude-tags=ci"
export TEST_CMD="KONG_SERF_PATH=$SERF_INSTALL/serf bin/busted $BUSTED_ARGS"

if [ "$TEST_SUITE" == "lint" ]; then
make lint
Expand Down
10 changes: 7 additions & 3 deletions .ci/setup_env.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
set -e

OPENRESTY_INSTALL=$CACHE_DIR/openresty
LUAROCKS_INSTALL=$CACHE_DIR/luarocks
SERF_INSTALL=$CACHE_DIR/serf
export OPENRESTY_INSTALL=$CACHE_DIR/openresty
export LUAROCKS_INSTALL=$CACHE_DIR/luarocks
export SERF_INSTALL=$CACHE_DIR/serf

mkdir -p $CACHE_DIR

Expand Down Expand Up @@ -92,3 +92,7 @@ if [[ "$TEST_SUITE" != "unit" ]] && [[ "$TEST_SUITE" != "lint" ]]; then
ccm status
fi

nginx -V
resty -V
luarocks --version
serf version
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEV_ROCKS = busted luacheck lua-llthreads2
BUSTED_ARGS ?= -v
TEST_CMD = bin/busted $(BUSTED_ARGS)
TEST_CMD ?= bin/busted $(BUSTED_ARGS)

.PHONY: install dev lint test test-integration test-plugins test-all

Expand Down
9 changes: 4 additions & 5 deletions kong/cmd/utils/serf_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ local log = require "kong.cmd.utils.log"
local version = require "version"
local fmt = string.format

local serf_bin_name = "serf"
local serf_event_name = "kong"
local serf_version_command = " version" -- commandline param to get version
local serf_version_pattern = "^Serf v([%d%.]+)" -- pattern to grab version from output
local serf_compatible = version.set("0.7.0", "0.7.0") -- compatible from-to versions
local start_timeout = 5

local function check_serf_bin()
local cmd = fmt("%s %s", serf_bin_name, serf_version_command)
local function check_serf_bin(kong_config)
local cmd = fmt("%s %s", kong_config.serf_path, serf_version_command)
local ok, _, stdout = pl_utils.executeex(cmd)
if ok and stdout then
local version_match = stdout:match(serf_version_pattern)
Expand All @@ -46,7 +45,7 @@ function _M.start(kong_config, dao)
end

-- make sure Serf is in PATH
local ok, err = check_serf_bin()
local ok, err = check_serf_bin(kong_config)
if not ok then return nil, err end

local serf = Serf.new(kong_config, dao)
Expand All @@ -64,7 +63,7 @@ function _M.start(kong_config, dao)
}, Serf.args_mt)

local cmd = string.format("nohup %s agent %s > %s 2>&1 & echo $! > %s",
serf_bin_name, tostring(args),
kong_config.serf_path, tostring(args),
kong_config.serf_log, kong_config.serf_pid)

log.debug("starting Serf agent: %s", cmd)
Expand Down
8 changes: 6 additions & 2 deletions kong/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ function Serf:invoke_signal(signal, args, no_rpc)
setmetatable(args, Serf.args_mt)
end
local rpc = no_rpc and "" or "-rpc-addr="..self.config.cluster_listen_rpc
local cmd = string.format("serf %s %s %s", signal, rpc, tostring(args))
local ok, code, stdout = pl_utils.executeex(cmd)
local cmd = string.format("%s %s %s %s", self.config.serf_path, signal, rpc, tostring(args))
local ok, code, stdout, stderr = pl_utils.executeex(cmd)
if not ok or code ~= 0 then
ngx.log(ngx.ERR, pl_utils.executeex('echo $PATH'))
ngx.log(ngx.ERR, "ok: ", ok, " code: ", code, " stdout: ", stdout, " stderr: ", stderr)
end
if not ok or code ~= 0 then return nil, pl_stringx.splitlines(stdout)[1] end -- always print the first error line of serf

return stdout
Expand Down
1 change: 1 addition & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ lua_ssl_trusted_certificate = NONE
lua_ssl_verify_depth = 1
lua_package_path = ?/init.lua;./kong/?.lua
lua_package_cpath = NONE
serf_path = serf
]]
8 changes: 5 additions & 3 deletions spec/02-integration/03-admin_api/06-cluster_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ describe("Admin API", function()
setup(function()
local pl_utils = require "pl.utils"
local kill = require "kong.cmd.utils.kill"
local cmd = string.format("nohup serf agent -rpc-addr=127.0.0.1:20000 "
local cmd = string.format("nohup %s agent -rpc-addr=127.0.0.1:20000 "
.."-bind=127.0.0.1:20001 -node=newnode > "
.."%s 2>&1 & echo $! > %s",
log_path, pid_path)
helpers.test_conf.serf_path, log_path, pid_path)
assert(pl_utils.execute(cmd))

local tstart = ngx.time()
Expand All @@ -64,7 +64,9 @@ describe("Admin API", function()

it("force-leaves a node", function()
-- old test converted
local cmd = string.format("serf join -rpc-addr=%s 127.0.0.1:20001", helpers.test_conf.cluster_listen_rpc)
local cmd = string.format("%s join -rpc-addr=%s 127.0.0.1:20001",
helpers.test_conf.serf_path,
helpers.test_conf.cluster_listen_rpc)
assert(helpers.execute(cmd))

local res = assert(client:send {
Expand Down
3 changes: 2 additions & 1 deletion spec/02-integration/04-core/02-hooks_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ describe("Core Hooks", function()
}
setmetatable(args, require "kong.tools.printable")

local cmd = string.format("nohup serf agent %s > %s 2>&1 & echo $! > %s",
local cmd = string.format("nohup %s agent %s > %s 2>&1 & echo $! > %s",
helpers.test_conf.serf_path,
tostring(args),
LOG_FILE, PID_FILE)

Expand Down
9 changes: 7 additions & 2 deletions spec/02-integration/06-cluster/01-cluster_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ describe("Cluster", function()

-- We need to wait a few seconds for the async job to kick in and join all the nodes together
helpers.wait_until(function()
local _, _, stdout = assert(helpers.execute("serf members -format=json -rpc-addr="..NODES.servroot1.cluster_listen_rpc))
local _, _, stdout = assert(helpers.execute(
string.format("%s members -format=json -rpc-addr=%s",
helpers.test_conf.serf_path, NODES.servroot1.cluster_listen_rpc)
)
)
return #cjson.decode(stdout).members == 3
end, 5)

Expand Down Expand Up @@ -344,10 +348,11 @@ describe("Cluster", function()
assert(node_name, "node_name is nil")

-- The member has now failed, let's bring it up again
assert(helpers.execute(string.format("serf agent -profile=wan -node=%s -rpc-addr=%s"
assert(helpers.execute(string.format("%s agent -profile=wan -node=%s -rpc-addr=%s"
.." -bind=%s -event-handler=member-join,"
.."member-leave,member-failed,member-update,"
.."member-reap,user:kong=%s > /dev/null &",
helpers.test_conf.serf_path,
node_name,
NODES.servroot2.cluster_listen_rpc,
NODES.servroot2.cluster_listen,
Expand Down
16 changes: 12 additions & 4 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,26 @@ local function res_status(state, args)
table.insert(args, 1, res.status)
table.insert(args, 1, expected)
args.n = 3

if res.status == 500 then
-- on HTTP 500, we can try to read the server's error logs
-- for debugging purposes (very useful for travis)
local str = pl_file.read(conf.nginx_err_logs)
local str_t = pl_stringx.split(str, "\n")
if not str then
return false -- no err logs to read in this prefix
end

local str_t = pl_stringx.splitlines(str)
local first_line = #str_t - math.min(20, #str_t) + 1
local msg = "\nError logs ("..conf.nginx_err_logs.."): \n"
local msg_t = {"\nError logs ("..conf.nginx_err_logs.."):"}
for i = first_line, #str_t do
msg = msg .. str_t[i] .. "\n"
msg_t[#msg_t+1] = str_t[i]
end
table.insert(args, 4, msg)

table.insert(args, 4, table.concat(msg_t, "\n"))
args.n = 4
end

return false
else
local body, err = res:read_body()
Expand Down Expand Up @@ -729,6 +736,7 @@ end
local function kong_exec(cmd, env)
cmd = cmd or ""
env = env or {}
env.serf_path = conf.serf_path

local env_vars = ""
for k, v in pairs(env) do
Expand Down

0 comments on commit e4a14c9

Please sign in to comment.