Skip to content

Commit

Permalink
health
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jul 7, 2016
1 parent f0f61b0 commit 2c3710d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ build = {
["kong.cmd.cluster"] = "kong/cmd/cluster.lua",
["kong.cmd.compile"] = "kong/cmd/compile.lua",
["kong.cmd.migrations"] = "kong/cmd/migrations.lua",
["kong.cmd.health"] = "kong/cmd/health.lua",
["kong.cmd.version"] = "kong/cmd/version.lua",
["kong.cmd.utils.log"] = "kong/cmd/utils/log.lua",
["kong.cmd.utils.kill"] = "kong/cmd/utils/kill.lua",
Expand Down
54 changes: 54 additions & 0 deletions kong/cmd/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local conf_loader = require "kong.conf_loader"
local log = require "kong.cmd.utils.log"
local kill = require "kong.cmd.utils.kill"
local pl_stringx = require "pl.stringx"
local pl_path = require "pl.path"
local pl_tablex = require "pl.tablex"

local function is_running(pid_path)
if not pl_path.exists(pid_path) then return nil end
local code = kill(pid_path, "-0")
return code == 0
end

local function execute(args)
local default_conf = assert(conf_loader(args.conf, {
prefix = args.prefix
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)

local pids = {
nginx = default_conf.nginx_pid,
serf = default_conf.serf_pid,
dnsmasq = default_conf.dnsmasq and default_conf.dnsmasq_pid or nil
}

local count = 0
for k, v in pairs(pids) do
local running = is_running(v)
local msg = pl_stringx.ljust(k, 10, ".")..(running and "running" or "not running")
if running then
count = count + 1
log(msg)
else
log.warn(msg)
end
end

assert(count > 0, "Kong is not running")
assert(count == pl_tablex.size(pids), "Some services are not running")
end

local lapp = [[
Usage: kong health [OPTIONS]
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) override prefix directory
]]

return {
lapp = lapp,
execute = execute
}
1 change: 1 addition & 0 deletions kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local cmds = {
migrations = "migrations",
cluster = "cluster",
version = "version",
health = "health",
roar = "roar"
}

Expand Down
2 changes: 0 additions & 2 deletions spec/02-integration/01-cmd/08-restart_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("kong restart", function()
it("restarts if not running", function()
assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path))
end)

it("restarts if already running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))

Expand All @@ -26,5 +25,4 @@ describe("kong restart", function()
assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.serf_pid)), serf_pid)
assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)), dnsmasq_pid)
end)

end)
33 changes: 33 additions & 0 deletions spec/02-integration/01-cmd/09-health_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local helpers = require "spec.helpers"
local prefix_handler = require "kong.cmd.utils.prefix_handler"

describe("kong restart", function()
before_each(function()
helpers.kill_all()
end)
teardown(function()
helpers.kill_all()
helpers.clean_prefix()
end)

it("succeeds when Kong is running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))
assert(helpers.kong_exec("health --conf "..helpers.test_conf_path))
end)

describe("errors", function()
it("fails when Kong is not running", function()
assert(prefix_handler.prepare_prefix(helpers.test_conf))
local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Kong is not running", stderr)
end)
it("fails when a service is not running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))
helpers.execute("pkill serf")
local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Some services are not running", stderr)
end)
end)
end)

0 comments on commit 2c3710d

Please sign in to comment.