From f0f61b0e7e389dca862cabbc750cad45584b91f2 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 6 Jul 2016 17:27:37 -0700 Subject: [PATCH 01/15] restart --- kong-0.8.2-0.rockspec | 1 + kong/cmd/init.lua | 2 ++ kong/cmd/restart.lua | 25 ++++++++++++++++ kong/cmd/stop.lua | 5 ++-- kong/cmd/utils/nginx_signals.lua | 4 +-- .../01-cmd/02-start_stop_spec.lua | 6 ++-- .../02-integration/01-cmd/08-restart_spec.lua | 30 +++++++++++++++++++ 7 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 kong/cmd/restart.lua create mode 100644 spec/02-integration/01-cmd/08-restart_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index 6493856e3d2..a660ba37e4e 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -61,6 +61,7 @@ build = { ["kong.cmd.start"] = "kong/cmd/start.lua", ["kong.cmd.check"] = "kong/cmd/check.lua", ["kong.cmd.reload"] = "kong/cmd/reload.lua", + ["kong.cmd.restart"] = "kong/cmd/restart.lua", ["kong.cmd.cluster"] = "kong/cmd/cluster.lua", ["kong.cmd.compile"] = "kong/cmd/compile.lua", ["kong.cmd.migrations"] = "kong/cmd/migrations.lua", diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index bf8204d5ad4..57577afd84c 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -13,6 +13,7 @@ Usage: kong COMMAND [OPTIONS] The available commands are: start stop + restart reload check compile @@ -26,6 +27,7 @@ Options: local cmds = { start = "start", stop = "stop", + restart = "restart", reload = "reload", check = "check", compile = "compile", diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua new file mode 100644 index 00000000000..1cdac7351d7 --- /dev/null +++ b/kong/cmd/restart.lua @@ -0,0 +1,25 @@ +local conf_loader = require "kong.conf_loader" +local stop = require "kong.cmd.stop" +local start = require "kong.cmd.start" + +local function execute(args) + local conf = assert(conf_loader(args.conf)) + args.prefix = conf.prefix -- Required for stop + args.graceful = nil -- Restart is always not graceful (reload should be used instead) + + pcall(stop.execute, args) + start.execute(args) +end + +local lapp = [[ +Usage: kong restart [OPTIONS] + +Options: + -c,--conf (optional string) configuration file + --prefix (optional string) override prefix directory +]] + +return { + lapp = lapp, + execute = execute +} \ No newline at end of file diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index a5b12164e90..4237ea76ed7 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,12 +16,12 @@ local function execute(args) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf)) + assert(nginx_signals.stop(conf, args.graceful)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped") + log("Stopped%s", args.graceful and " gracefully" or "") end local lapp = [[ @@ -29,6 +29,7 @@ Usage: kong stop [OPTIONS] Options: --prefix (optional string) prefix Kong is running at + --graceful (optional boolean) graceful shutdown ]] return { diff --git a/kong/cmd/utils/nginx_signals.lua b/kong/cmd/utils/nginx_signals.lua index e605f013c55..44785a143b3 100644 --- a/kong/cmd/utils/nginx_signals.lua +++ b/kong/cmd/utils/nginx_signals.lua @@ -86,8 +86,8 @@ function _M.start(kong_conf) return true end -function _M.stop(kong_conf) - return send_signal(kong_conf.nginx_pid, "QUIT") +function _M.stop(kong_conf, graceful) + return send_signal(kong_conf.nginx_pid, graceful and "QUIT" or "TERM") end function _M.reload(kong_conf) diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index 1afdfddb94d..fe19de7c17a 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -1,5 +1,3 @@ -local pl_dir = require "pl.dir" -local pl_path = require "pl.path" local helpers = require "spec.helpers" describe("kong start/stop", function() @@ -33,6 +31,10 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) + it("start/stop gracefully", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) + end) it("start with inexistent prefix", function() finally(function() pcall(helpers.dir.rmtree, "foobar") diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua new file mode 100644 index 00000000000..0fccc2b9f4a --- /dev/null +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -0,0 +1,30 @@ +local helpers = require "spec.helpers" + +describe("kong restart", function() + before_each(function() + helpers.kill_all() + end) + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + + 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 = ""})) + + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) + + assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""})) + + assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) + 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) From 2c3710d25c5d5cbcf772928532fce9ef74353613 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 6 Jul 2016 23:21:36 -0700 Subject: [PATCH 02/15] health --- kong-0.8.2-0.rockspec | 1 + kong/cmd/health.lua | 54 +++++++++++++++++++ kong/cmd/init.lua | 1 + .../02-integration/01-cmd/08-restart_spec.lua | 2 - spec/02-integration/01-cmd/09-health_spec.lua | 33 ++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 kong/cmd/health.lua create mode 100644 spec/02-integration/01-cmd/09-health_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index a660ba37e4e..b6ef33f53d3 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -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", diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua new file mode 100644 index 00000000000..1102c85eb06 --- /dev/null +++ b/kong/cmd/health.lua @@ -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 +} \ No newline at end of file diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index 57577afd84c..f632f93e938 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -34,6 +34,7 @@ local cmds = { migrations = "migrations", cluster = "cluster", version = "version", + health = "health", roar = "roar" } diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index 0fccc2b9f4a..b4d874090f5 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -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 = ""})) @@ -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) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua new file mode 100644 index 00000000000..0c62f691b28 --- /dev/null +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -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) From 6c2820ce928f6f0199bd3ea95879a27bd645f65e Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 7 Jul 2016 14:02:57 -0700 Subject: [PATCH 03/15] refactor --- kong/cmd/health.lua | 21 ++++++++++----------- kong/cmd/restart.lua | 1 - 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index 1102c85eb06..c526a0a59b6 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -1,27 +1,26 @@ 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 pl_path = require "pl.path" +local kill = require "kong.cmd.utils.kill" +local log = require "kong.cmd.utils.log" 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 + return kill(pid_path, "-0") == 0 end local function execute(args) - local default_conf = assert(conf_loader(args.conf, { + local conf = assert(conf_loader(args.conf, { prefix = args.prefix })) - assert(pl_path.exists(default_conf.prefix), - "no such prefix: "..default_conf.prefix) + assert(pl_path.exists(conf.prefix), + "no such prefix: "..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 + nginx = conf.nginx_pid, + serf = conf.serf_pid, + dnsmasq = conf.dnsmasq and conf.dnsmasq_pid or nil } local count = 0 diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua index 1cdac7351d7..7b583034831 100644 --- a/kong/cmd/restart.lua +++ b/kong/cmd/restart.lua @@ -5,7 +5,6 @@ local start = require "kong.cmd.start" local function execute(args) local conf = assert(conf_loader(args.conf)) args.prefix = conf.prefix -- Required for stop - args.graceful = nil -- Restart is always not graceful (reload should be used instead) pcall(stop.execute, args) start.execute(args) From cef1baa85d6142782a8cac2fb59d102b3fdc9f2d Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 6 Jul 2016 17:27:37 -0700 Subject: [PATCH 04/15] feat(cli) kong health + restart commands --- kong-0.8.2-0.rockspec | 2 + kong/cmd/health.lua | 53 +++++++++++++++++++ kong/cmd/init.lua | 3 ++ kong/cmd/restart.lua | 24 +++++++++ kong/cmd/stop.lua | 5 +- kong/cmd/utils/nginx_signals.lua | 4 +- .../01-cmd/02-start_stop_spec.lua | 6 ++- .../02-integration/01-cmd/08-restart_spec.lua | 28 ++++++++++ spec/02-integration/01-cmd/09-health_spec.lua | 33 ++++++++++++ 9 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 kong/cmd/health.lua create mode 100644 kong/cmd/restart.lua create mode 100644 spec/02-integration/01-cmd/08-restart_spec.lua create mode 100644 spec/02-integration/01-cmd/09-health_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index 6493856e3d2..b6ef33f53d3 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -61,9 +61,11 @@ build = { ["kong.cmd.start"] = "kong/cmd/start.lua", ["kong.cmd.check"] = "kong/cmd/check.lua", ["kong.cmd.reload"] = "kong/cmd/reload.lua", + ["kong.cmd.restart"] = "kong/cmd/restart.lua", ["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", diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua new file mode 100644 index 00000000000..c526a0a59b6 --- /dev/null +++ b/kong/cmd/health.lua @@ -0,0 +1,53 @@ +local conf_loader = require "kong.conf_loader" +local pl_stringx = require "pl.stringx" +local pl_tablex = require "pl.tablex" +local pl_path = require "pl.path" +local kill = require "kong.cmd.utils.kill" +local log = require "kong.cmd.utils.log" + +local function is_running(pid_path) + if not pl_path.exists(pid_path) then return nil end + return kill(pid_path, "-0") == 0 +end + +local function execute(args) + local conf = assert(conf_loader(args.conf, { + prefix = args.prefix + })) + assert(pl_path.exists(conf.prefix), + "no such prefix: "..conf.prefix) + + local pids = { + nginx = conf.nginx_pid, + serf = conf.serf_pid, + dnsmasq = conf.dnsmasq and 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 +} \ No newline at end of file diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index bf8204d5ad4..f632f93e938 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -13,6 +13,7 @@ Usage: kong COMMAND [OPTIONS] The available commands are: start stop + restart reload check compile @@ -26,12 +27,14 @@ Options: local cmds = { start = "start", stop = "stop", + restart = "restart", reload = "reload", check = "check", compile = "compile", migrations = "migrations", cluster = "cluster", version = "version", + health = "health", roar = "roar" } diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua new file mode 100644 index 00000000000..7b583034831 --- /dev/null +++ b/kong/cmd/restart.lua @@ -0,0 +1,24 @@ +local conf_loader = require "kong.conf_loader" +local stop = require "kong.cmd.stop" +local start = require "kong.cmd.start" + +local function execute(args) + local conf = assert(conf_loader(args.conf)) + args.prefix = conf.prefix -- Required for stop + + pcall(stop.execute, args) + start.execute(args) +end + +local lapp = [[ +Usage: kong restart [OPTIONS] + +Options: + -c,--conf (optional string) configuration file + --prefix (optional string) override prefix directory +]] + +return { + lapp = lapp, + execute = execute +} \ No newline at end of file diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index a5b12164e90..4237ea76ed7 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,12 +16,12 @@ local function execute(args) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf)) + assert(nginx_signals.stop(conf, args.graceful)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped") + log("Stopped%s", args.graceful and " gracefully" or "") end local lapp = [[ @@ -29,6 +29,7 @@ Usage: kong stop [OPTIONS] Options: --prefix (optional string) prefix Kong is running at + --graceful (optional boolean) graceful shutdown ]] return { diff --git a/kong/cmd/utils/nginx_signals.lua b/kong/cmd/utils/nginx_signals.lua index e605f013c55..44785a143b3 100644 --- a/kong/cmd/utils/nginx_signals.lua +++ b/kong/cmd/utils/nginx_signals.lua @@ -86,8 +86,8 @@ function _M.start(kong_conf) return true end -function _M.stop(kong_conf) - return send_signal(kong_conf.nginx_pid, "QUIT") +function _M.stop(kong_conf, graceful) + return send_signal(kong_conf.nginx_pid, graceful and "QUIT" or "TERM") end function _M.reload(kong_conf) diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index a2956650b2d..b383669c03e 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -1,5 +1,3 @@ -local pl_dir = require "pl.dir" -local pl_path = require "pl.path" local helpers = require "spec.helpers" describe("kong start/stop", function() @@ -33,6 +31,10 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) + it("start/stop gracefully", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) + end) it("start with inexistent prefix", function() finally(function() pcall(helpers.dir.rmtree, "foobar") diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua new file mode 100644 index 00000000000..b4d874090f5 --- /dev/null +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -0,0 +1,28 @@ +local helpers = require "spec.helpers" + +describe("kong restart", function() + before_each(function() + helpers.kill_all() + end) + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + + 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 = ""})) + + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) + + assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""})) + + assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) + 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) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua new file mode 100644 index 00000000000..0c62f691b28 --- /dev/null +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -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) From c55c9812318ff179678cf8b7fc3ef59bd0a422e6 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 7 Jul 2016 16:47:25 -0700 Subject: [PATCH 05/15] refactor(cli) better health/restart impl + tests --- kong/cmd/health.lua | 52 +++++++++------- kong/cmd/restart.lua | 13 ++-- .../01-cmd/02-start_stop_spec.lua | 4 +- .../02-integration/01-cmd/08-restart_spec.lua | 42 +++++++++++-- spec/02-integration/01-cmd/09-health_spec.lua | 59 ++++++++++++++----- 5 files changed, 124 insertions(+), 46 deletions(-) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index c526a0a59b6..7aec15177e2 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -1,25 +1,35 @@ -local conf_loader = require "kong.conf_loader" -local pl_stringx = require "pl.stringx" -local pl_tablex = require "pl.tablex" -local pl_path = require "pl.path" -local kill = require "kong.cmd.utils.kill" local log = require "kong.cmd.utils.log" +local kill = require "kong.cmd.utils.kill" +local pl_path = require "pl.path" +local pl_tablex = require "pl.tablex" +local pl_stringx = require "pl.stringx" +local conf_loader = require "kong.conf_loader" local function is_running(pid_path) - if not pl_path.exists(pid_path) then return nil end + if not pl_path.exists(pid_path) then return end return kill(pid_path, "-0") == 0 end local function execute(args) - local conf = assert(conf_loader(args.conf, { - prefix = args.prefix - })) - assert(pl_path.exists(conf.prefix), - "no such prefix: "..conf.prefix) + local conf + + if args.prefix then + -- retrieve prefix or use given one + local default_conf = assert(conf_loader(nil, { + prefix = args.prefix + })) + assert(pl_path.exists(default_conf.prefix), + "no such prefix: "..default_conf.prefix) + + -- load /kong.conf containing running node's config + conf = assert(conf_loader(default_conf.kong_conf)) + else + conf = assert(conf_loader(args.conf)) + end local pids = { - nginx = conf.nginx_pid, serf = conf.serf_pid, + nginx = conf.nginx_pid, dnsmasq = conf.dnsmasq and conf.dnsmasq_pid or nil } @@ -29,14 +39,16 @@ local function execute(args) 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 + log(msg) end - - assert(count > 0, "Kong is not running") - assert(count == pl_tablex.size(pids), "Some services are not running") + + log("") -- line jump + + assert(count > 0, "Kong is not running at "..conf.prefix) + assert(count == pl_tablex.size(pids), "Some services are not running at "..conf.prefix) + + log("Kong is healthy at %s", conf.prefix) end local lapp = [[ @@ -44,10 +56,10 @@ Usage: kong health [OPTIONS] Options: -c,--conf (optional string) configuration file - --prefix (optional string) override prefix directory + --prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua index 7b583034831..863acb690cc 100644 --- a/kong/cmd/restart.lua +++ b/kong/cmd/restart.lua @@ -1,10 +1,13 @@ -local conf_loader = require "kong.conf_loader" local stop = require "kong.cmd.stop" local start = require "kong.cmd.start" +local conf_loader = require "kong.conf_loader" local function execute(args) - local conf = assert(conf_loader(args.conf)) - args.prefix = conf.prefix -- Required for stop + if args.conf then + -- retrieve the prefix for stop + local conf = assert(conf_loader(args.conf)) + args.prefix = conf.prefix + end pcall(stop.execute, args) start.execute(args) @@ -15,10 +18,10 @@ Usage: kong restart [OPTIONS] Options: -c,--conf (optional string) configuration file - --prefix (optional string) override prefix directory + --prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index b383669c03e..c68328ebc58 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -20,11 +20,11 @@ describe("kong start/stop", function() it("start/stop default conf/prefix", function() -- don't want to force migrations to be run on default -- keyspace/database - assert(helpers.kong_exec "start", { + assert(helpers.kong_exec("start", { database = helpers.test_conf.database, pg_database = helpers.test_conf.pg_database, cassandra_keyspace = helpers.test_conf.cassandra_keyspace - }) + })) assert(helpers.kong_exec "stop") end) it("start/stop custom Kong conf/prefix", function() diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index b4d874090f5..1a29b5b1f98 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -3,6 +3,7 @@ local helpers = require "spec.helpers" describe("kong restart", function() before_each(function() helpers.kill_all() + helpers.prepare_prefix() end) teardown(function() helpers.kill_all() @@ -12,17 +13,50 @@ 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 = ""})) + it("restarts if already running from --conf", function() + local env = { + dnsmasq = true, + dns_resolver = "" + } - local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, env)) local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) - assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""})) + assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path, env)) + assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) + 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) + it("restarts if already running from --prefix", function() + local env = { + dnsmasq = true, + dns_resolver = "" + } + + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, env)) + local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) + assert(helpers.kong_exec("restart --prefix "..helpers.test_conf.prefix, env)) assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) 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) + it("restarts with default configuration and prefix", function() + -- don't want to force migrations to be run on default + -- keyspace/database + local env = { + database = helpers.test_conf.database, + pg_database = helpers.test_conf.pg_database, + cassandra_keyspace = helpers.test_conf.cassandra_keyspace, + dnsmasq = true, + dns_resolver = "" + } + + assert(helpers.kong_exec("start", env)) + assert(helpers.kong_exec("restart", env)) + end) end) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua index 0c62f691b28..90a047f240e 100644 --- a/spec/02-integration/01-cmd/09-health_spec.lua +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -1,33 +1,62 @@ local helpers = require "spec.helpers" -local prefix_handler = require "kong.cmd.utils.prefix_handler" describe("kong restart", function() before_each(function() helpers.kill_all() + helpers.prepare_prefix() 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)) + it("succeeds when Kong is running with --conf", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local _, _, stdout = assert(helpers.kong_exec("health --conf "..helpers.test_conf_path)) + assert.matches("serf%.-running", stdout) + assert.matches("nginx%.-running", stdout) + assert.not_matches("dnsmasq.*running", stdout) + assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) + end) + it("succeeds when Kong is running with --prefix", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local _, _, stdout = assert(helpers.kong_exec("health --prefix "..helpers.test_conf.prefix)) + assert.matches("serf%.-running", stdout) + assert.matches("nginx%.-running", stdout) + assert.not_matches("dnsmasq.*running", stdout) + assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) + end) + it("fails when Kong is not running", function() + local ok, stderr, stdout = helpers.kong_exec("health --conf "..helpers.test_conf_path) + assert.False(ok) + assert.matches("Kong is not running at "..helpers.test_conf.prefix, stderr, nil, true) + end) + it("fails when a service is not running", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + 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, nil, true) + end) + it("checks dnsmasq if enabled", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path, { + dnsmasq = true, + dns_resolver = "" + }) + assert.False(ok) + assert.matches("Some services are not running", stderr, nil, true) 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) + it("errors on inexisting prefix", function() + local ok, stderr = helpers.kong_exec("health --prefix inexistant") assert.False(ok) - assert.matches("Some services are not running", stderr) + assert.matches("no such prefix: ", stderr, nil, true) end) end) end) From de032934f5cae9c4422eb882d1aa2bf9be39d450 Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 7 Jul 2016 20:35:55 -0700 Subject: [PATCH 06/15] kong quit and tests --- kong-0.8.2-0.rockspec | 1 + kong/cmd/init.lua | 1 + kong/cmd/quit.lua | 37 ++++++++++++++ kong/cmd/stop.lua | 5 +- .../01-cmd/02-start_stop_spec.lua | 4 -- .../02-integration/01-cmd/07-cluster_spec.lua | 50 ++++++++++++++++++- .../02-integration/01-cmd/08-restart_spec.lua | 4 ++ spec/02-integration/01-cmd/09-health_spec.lua | 4 ++ spec/02-integration/01-cmd/10-quit_spec.lua | 21 ++++++++ 9 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 kong/cmd/quit.lua create mode 100644 spec/02-integration/01-cmd/10-quit_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index b6ef33f53d3..33628dcdbcb 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -58,6 +58,7 @@ build = { ["kong.cmd.roar"] = "kong/cmd/roar.lua", ["kong.cmd.init"] = "kong/cmd/init.lua", ["kong.cmd.stop"] = "kong/cmd/stop.lua", + ["kong.cmd.quit"] = "kong/cmd/quit.lua", ["kong.cmd.start"] = "kong/cmd/start.lua", ["kong.cmd.check"] = "kong/cmd/check.lua", ["kong.cmd.reload"] = "kong/cmd/reload.lua", diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index f632f93e938..1c9d01e8c59 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -27,6 +27,7 @@ Options: local cmds = { start = "start", stop = "stop", + quit = "quit", restart = "restart", reload = "reload", check = "check", diff --git a/kong/cmd/quit.lua b/kong/cmd/quit.lua new file mode 100644 index 00000000000..3e1df719665 --- /dev/null +++ b/kong/cmd/quit.lua @@ -0,0 +1,37 @@ +local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals" +local nginx_signals = require "kong.cmd.utils.nginx_signals" +local serf_signals = require "kong.cmd.utils.serf_signals" +local conf_loader = require "kong.conf_loader" +local DAOFactory = require "kong.dao.factory" +local pl_path = require "pl.path" +local log = require "kong.cmd.utils.log" + +local function execute(args) + -- retrieve prefix or use given one + local default_conf = assert(conf_loader(nil, { + prefix = args.prefix + })) + assert(pl_path.exists(default_conf.prefix), + "no such prefix: "..default_conf.prefix) + + -- load /kong.conf containing running node's config + local conf = assert(conf_loader(default_conf.kong_conf)) + assert(nginx_signals.stop(conf, true)) + assert(serf_signals.stop(conf, DAOFactory(conf))) + if conf.dnsmasq then + assert(dnsmasq_signals.stop(conf)) + end + log("Stopped gracefully") +end + +local lapp = [[ +Usage: kong quit [OPTIONS] + +Options: + --prefix (optional string) prefix Kong is running at +]] + +return { + lapp = lapp, + execute = execute +} diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index 4237ea76ed7..a5b12164e90 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,12 +16,12 @@ local function execute(args) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf, args.graceful)) + assert(nginx_signals.stop(conf)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped%s", args.graceful and " gracefully" or "") + log("Stopped") end local lapp = [[ @@ -29,7 +29,6 @@ Usage: kong stop [OPTIONS] Options: --prefix (optional string) prefix Kong is running at - --graceful (optional boolean) graceful shutdown ]] return { diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index fe19de7c17a..c8420374c16 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -31,10 +31,6 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) - it("start/stop gracefully", function() - assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) - end) it("start with inexistent prefix", function() finally(function() pcall(helpers.dir.rmtree, "foobar") diff --git a/spec/02-integration/01-cmd/07-cluster_spec.lua b/spec/02-integration/01-cmd/07-cluster_spec.lua index 93df414cec6..79c4c5b050c 100644 --- a/spec/02-integration/01-cmd/07-cluster_spec.lua +++ b/spec/02-integration/01-cmd/07-cluster_spec.lua @@ -1,9 +1,55 @@ local helpers = require "spec.helpers" describe("kong cluster", function() - it("keygen", function() - local _, stderr, stdout = helpers.kong_exec "cluster keygen" + before_each(function() + helpers.kill_all() + end) + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + + + it("cluster help", function() + local _, stderr = helpers.kong_exec "cluster --help" + assert.not_equal("", stderr) + end) + it("generates a key", function() + local _, stderr, stdout = assert(helpers.kong_exec("cluster keygen")) assert.equal("", stderr) assert.equal(26, stdout:len()) -- 24 + \r\n end) + it("shows members", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster members --conf "..helpers.test_conf_path)) + assert.matches("alive", stdout) + end) + it("shows rechability", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster reachability --conf "..helpers.test_conf_path)) + assert.matches("Successfully contacted all live nodes", stdout) + end) + it("force-leaves a node", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster force-leave 127.0.0.1 --conf "..helpers.test_conf_path)) + assert.matches("left node 127.0.0.1", stdout) + end) + + describe("errors", function() + it("fails to show members when Kong is not running", function() + local ok, stderr = helpers.kong_exec("cluster members --conf "..helpers.test_conf_path) + assert.False(ok) + assert.matches("Error connecting to Serf agent", stderr) + end) + it("fails to show reachability when Kong is not running", function() + local ok, stderr = helpers.kong_exec("cluster reachability --conf "..helpers.test_conf_path) + assert.False(ok) + assert.matches("Error connecting to Serf agent", stderr) + end) + it("fails to force-leave when a node is not specified", function() + local ok, stderr = helpers.kong_exec("cluster force-leave --conf "..helpers.test_conf_path) + assert.False(ok) + assert.matches("must specify the name of the node to leave", stderr) + end) + end) end) diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index b4d874090f5..698e597d92a 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -9,6 +9,10 @@ describe("kong restart", function() helpers.clean_prefix() end) + it("restart help", function() + local _, stderr = helpers.kong_exec "health --help" + assert.not_equal("", stderr) + end) it("restarts if not running", function() assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path)) end) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua index 0c62f691b28..86f1881c461 100644 --- a/spec/02-integration/01-cmd/09-health_spec.lua +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -10,6 +10,10 @@ describe("kong restart", function() helpers.clean_prefix() end) + it("health help", function() + local _, stderr = helpers.kong_exec "health --help" + assert.not_equal("", stderr) + 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)) diff --git a/spec/02-integration/01-cmd/10-quit_spec.lua b/spec/02-integration/01-cmd/10-quit_spec.lua new file mode 100644 index 00000000000..f34220d5fdf --- /dev/null +++ b/spec/02-integration/01-cmd/10-quit_spec.lua @@ -0,0 +1,21 @@ +local helpers = require "spec.helpers" + +describe("kong start/stop", function() + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + before_each(function() + helpers.kill_all() + end) + + it("quit help", function() + local _, stderr = helpers.kong_exec "quit --help" + assert.not_equal("", stderr) + end) + it("quits gracefully", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("quit --prefix "..helpers.test_conf.prefix)) + end) + +end) From 3cd3ece7239261671bb952cd10442ec8ae720efa Mon Sep 17 00:00:00 2001 From: thefosk Date: Fri, 8 Jul 2016 11:36:06 -0700 Subject: [PATCH 07/15] better assert.matches --- spec/02-integration/01-cmd/07-cluster_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/02-integration/01-cmd/07-cluster_spec.lua b/spec/02-integration/01-cmd/07-cluster_spec.lua index 79c4c5b050c..18fcd7fb988 100644 --- a/spec/02-integration/01-cmd/07-cluster_spec.lua +++ b/spec/02-integration/01-cmd/07-cluster_spec.lua @@ -32,7 +32,7 @@ describe("kong cluster", function() it("force-leaves a node", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) local _, _, stdout = assert(helpers.kong_exec("cluster force-leave 127.0.0.1 --conf "..helpers.test_conf_path)) - assert.matches("left node 127.0.0.1", stdout) + assert.matches("left node 127.0.0.1", stdout, nil, true) end) describe("errors", function() From 80906cedeb44ceca6ab71b803e10f80092a28f6c Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Mon, 11 Jul 2016 16:21:21 -0700 Subject: [PATCH 08/15] fix(cli) health command only accepts --prefix arg Commands expecting a running Kong should not accept a --conf argument --- kong/cmd/health.lua | 40 +++++++------------ spec/02-integration/01-cmd/09-health_spec.lua | 19 +++------ 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index 6dd0420b075..09ead74d3af 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -5,27 +5,18 @@ local pl_tablex = require "pl.tablex" local pl_stringx = require "pl.stringx" local conf_loader = require "kong.conf_loader" -local function is_running(pid_path) - if not pl_path.exists(pid_path) then return end - return kill(pid_path, "-0") == 0 -end - local function execute(args) - local conf - - if args.prefix then - -- retrieve prefix or use given one - local default_conf = assert(conf_loader(nil, { - prefix = args.prefix - })) - assert(pl_path.exists(default_conf.prefix), - "no such prefix: "..default_conf.prefix) - - -- load /kong.conf containing running node's config - conf = assert(conf_loader(default_conf.kong_conf)) - else - conf = assert(conf_loader(args.conf)) - end + -- retrieve default prefix or use given one + local default_conf = assert(conf_loader(nil, { + prefix = args.prefix + })) + assert(pl_path.exists(default_conf.prefix), + "no such prefix: "..default_conf.prefix) + assert(pl_path.exists(default_conf.kong_conf), + "Kong is not running at "..default_conf.prefix) + + -- load /kong.conf containing running node's config + local conf = assert(conf_loader(default_conf.kong_conf)) local pids = { serf = conf.serf_pid, @@ -35,8 +26,8 @@ local function execute(args) 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") + local running = kill.is_running(v) + local msg = pl_stringx.ljust(k, 12, ".")..(running and "running" or "not running") if running then count = count + 1 end @@ -55,11 +46,10 @@ local lapp = [[ Usage: kong health [OPTIONS] Options: - -c,--conf (optional string) configuration file - --prefix (optional string) prefix at which Kong should be running + -p,--prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua index 44159dd84b8..55a3b9ae1d5 100644 --- a/spec/02-integration/01-cmd/09-health_spec.lua +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -14,16 +14,7 @@ describe("kong restart", function() local _, stderr = helpers.kong_exec "health --help" assert.not_equal("", stderr) end) - it("succeeds when Kong is running with --conf", function() - assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - - local _, _, stdout = assert(helpers.kong_exec("health --conf "..helpers.test_conf_path)) - assert.matches("serf%.-running", stdout) - assert.matches("nginx%.-running", stdout) - assert.not_matches("dnsmasq.*running", stdout) - assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) - end) - it("succeeds when Kong is running with --prefix", function() + it("succeeds when Kong is running with custom --prefix", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) local _, _, stdout = assert(helpers.kong_exec("health --prefix "..helpers.test_conf.prefix)) @@ -33,7 +24,7 @@ describe("kong restart", function() assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) end) it("fails when Kong is not running", function() - local ok, stderr, stdout = helpers.kong_exec("health --conf "..helpers.test_conf_path) + local ok, stderr = helpers.kong_exec("health --prefix "..helpers.test_conf.prefix) assert.False(ok) assert.matches("Kong is not running at "..helpers.test_conf.prefix, stderr, nil, true) end) @@ -41,14 +32,14 @@ describe("kong restart", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) helpers.execute("pkill serf") - local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path) + local ok, stderr = helpers.kong_exec("health --prefix "..helpers.test_conf.prefix) assert.False(ok) assert.matches("Some services are not running", stderr, nil, true) end) it("checks dnsmasq if enabled", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path, { + local ok, stderr = helpers.kong_exec("health --prefix "..helpers.test_conf.prefix, { dnsmasq = true, dns_resolver = "" }) @@ -63,4 +54,4 @@ describe("kong restart", function() assert.matches("no such prefix: ", stderr, nil, true) end) end) -end) \ No newline at end of file +end) From ccea6d30c106c8da9aec3c34d26217d3832f521a Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Mon, 11 Jul 2016 16:22:36 -0700 Subject: [PATCH 09/15] feat(cli) implement 'kong quit' with --timeout - abstract away `is_running()` in `kill.lua` util - add missing `kong health` to `kong --help` message - some linting inherited from the base branch of this branch --- kong/cmd/init.lua | 4 ++- kong/cmd/quit.lua | 31 ++++++++++++++++--- kong/cmd/restart.lua | 6 ++-- kong/cmd/stop.lua | 11 +++---- kong/cmd/utils/dnsmasq_signals.lua | 10 ++---- kong/cmd/utils/kill.lua | 13 +++++++- kong/cmd/utils/nginx_signals.lua | 25 ++++++++------- kong/cmd/utils/serf_signals.lua | 17 +++------- .../01-cmd/02-start_stop_spec.lua | 10 ++++-- spec/02-integration/01-cmd/04-reload_spec.lua | 2 +- spec/02-integration/01-cmd/10-quit_spec.lua | 21 ------------- .../06-cluster/cluster_spec.lua | 1 - spec/helpers.lua | 2 +- 13 files changed, 78 insertions(+), 75 deletions(-) delete mode 100644 spec/02-integration/01-cmd/10-quit_spec.lua diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index 1c9d01e8c59..5fb6882b587 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -13,8 +13,10 @@ Usage: kong COMMAND [OPTIONS] The available commands are: start stop + quit restart reload + health check compile migrations @@ -30,12 +32,12 @@ local cmds = { quit = "quit", restart = "restart", reload = "reload", + health = "health", check = "check", compile = "compile", migrations = "migrations", cluster = "cluster", version = "version", - health = "health", roar = "roar" } diff --git a/kong/cmd/quit.lua b/kong/cmd/quit.lua index 3e1df719665..70b193bf785 100644 --- a/kong/cmd/quit.lua +++ b/kong/cmd/quit.lua @@ -4,31 +4,52 @@ local serf_signals = require "kong.cmd.utils.serf_signals" local conf_loader = require "kong.conf_loader" local DAOFactory = require "kong.dao.factory" local pl_path = require "pl.path" +local kill = require "kong.cmd.utils.kill" local log = require "kong.cmd.utils.log" local function execute(args) - -- retrieve prefix or use given one + -- retrieve default prefix or use given one local default_conf = assert(conf_loader(nil, { prefix = args.prefix })) assert(pl_path.exists(default_conf.prefix), - "no such prefix: "..default_conf.prefix) + "no such prefix: "..default_conf.prefix) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf, true)) + + -- try graceful shutdown (QUIT) + assert(nginx_signals.quit(conf)) + + log.verbose("waiting for Nginx to finish processing requests...") + + local tstart = ngx.time() + local texp, running = tstart + math.max(args.timeout, 1) -- min 1s timeout + repeat + ngx.sleep(0.2) + running = kill.is_running(conf.nginx_pid) + until not running or ngx.time() >= texp + + if running then + log.verbose("Nginx is still running at %s, forcing shutdown", conf.prefix) + assert(nginx_signals.stop(conf)) + end + assert(serf_signals.stop(conf, DAOFactory(conf))) + if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped gracefully") + + log("Stopped (gracefully)") end local lapp = [[ Usage: kong quit [OPTIONS] Options: - --prefix (optional string) prefix Kong is running at + -p,--prefix (optional string) prefix Kong is running at + -t,--timeout (default 10) timeout before forced shutdown ]] return { diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua index 072f1600aed..284444852a8 100644 --- a/kong/cmd/restart.lua +++ b/kong/cmd/restart.lua @@ -17,11 +17,11 @@ local lapp = [[ Usage: kong restart [OPTIONS] Options: - -c,--conf (optional string) configuration file - --prefix (optional string) prefix at which Kong should be running + -c,--conf (optional string) configuration file + -p,--prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index 4237ea76ed7..6f7dab495d1 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -7,29 +7,28 @@ local pl_path = require "pl.path" local log = require "kong.cmd.utils.log" local function execute(args) - -- retrieve prefix or use given one + -- retrieve default prefix or use given one local default_conf = assert(conf_loader(nil, { prefix = args.prefix })) assert(pl_path.exists(default_conf.prefix), - "no such prefix: "..default_conf.prefix) + "no such prefix: "..default_conf.prefix) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf, args.graceful)) + assert(nginx_signals.stop(conf)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped%s", args.graceful and " gracefully" or "") + log("Stopped") end local lapp = [[ Usage: kong stop [OPTIONS] Options: - --prefix (optional string) prefix Kong is running at - --graceful (optional boolean) graceful shutdown + -p,--prefix (optional string) prefix Kong is running at ]] return { diff --git a/kong/cmd/utils/dnsmasq_signals.lua b/kong/cmd/utils/dnsmasq_signals.lua index 0510b5c5c3f..d891b274149 100644 --- a/kong/cmd/utils/dnsmasq_signals.lua +++ b/kong/cmd/utils/dnsmasq_signals.lua @@ -39,15 +39,9 @@ function _M.find_bin() return found end -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 - function _M.start(kong_config) -- is dnsmasq already running in this prefix? - if is_running(kong_config.dnsmasq_pid) then + if kill.is_running(kong_config.dnsmasq_pid) then log.verbose("dnsmasq already running at %s", kong_config.dnsmasq_pid) return true else @@ -71,7 +65,7 @@ end function _M.stop(kong_config) log.verbose("stopping dnsmasq at %s", kong_config.dnsmasq_pid) - return kill(kong_config.dnsmasq_pid, "-15") -- SIGTERM + return kill.kill(kong_config.dnsmasq_pid, "-15") -- SIGTERM end return _M diff --git a/kong/cmd/utils/kill.lua b/kong/cmd/utils/kill.lua index 443feb334df..e380ca7a2a4 100644 --- a/kong/cmd/utils/kill.lua +++ b/kong/cmd/utils/kill.lua @@ -1,3 +1,5 @@ +local pl_path = require "pl.path" + local cmd_tmpl = [[ kill %s `cat %s` >/dev/null 2>&1 ]] @@ -7,4 +9,13 @@ local function kill(pid_file, args) return os.execute(cmd) end -return kill +local function is_running(pid_file) + if pl_path.exists(pid_file) then + return kill(pid_file) == 0 + end +end + +return { + kill = kill, + is_running = is_running +} diff --git a/kong/cmd/utils/nginx_signals.lua b/kong/cmd/utils/nginx_signals.lua index 44785a143b3..6b5741c4d84 100644 --- a/kong/cmd/utils/nginx_signals.lua +++ b/kong/cmd/utils/nginx_signals.lua @@ -1,8 +1,8 @@ -local pl_utils = require "pl.utils" -local pl_path = require "pl.path" -local kill = require "kong.cmd.utils.kill" local log = require "kong.cmd.utils.log" +local kill = require "kong.cmd.utils.kill" +local pl_path = require "pl.path" local version = require "version" +local pl_utils = require "pl.utils" local fmt = string.format local nginx_bin_name = "nginx" @@ -13,7 +13,7 @@ local nginx_search_paths = { local nginx_version_command = "-v" -- commandline param to get version local nginx_version_pattern = "^nginx.-openresty.-([%d%.]+)" -- pattern to grab version from output local nginx_compatible = version.set("1.9.3.2","1.9.7.5") -- compatible from-to versions - + local function is_openresty(bin_path) local cmd = fmt("%s %s", bin_path, nginx_version_command) local ok, _, _, stderr = pl_utils.executeex(cmd) @@ -36,7 +36,7 @@ local function send_signal(pid_path, signal) log.verbose("sending %s signal to Nginx running at %s", signal, pid_path) - local code = kill(pid_path, "-s "..signal) + local code = kill.kill(pid_path, "-s "..signal) if code ~= 0 then return nil, "could not send signal" end return true @@ -69,11 +69,8 @@ function _M.start(kong_conf) local nginx_bin, err = _M.find_bin() if not nginx_bin then return nil, err end - if pl_path.exists(kong_conf.nginx_pid) then - local code = kill(kong_conf.nginx_pid, "-0") - if code == 0 then - return nil, "Nginx is already running in "..kong_conf.prefix - end + if kill.is_running(kong_conf.nginx_pid) then + return nil, "Nginx is already running in "..kong_conf.prefix end local cmd = fmt("%s -p %s -c %s", nginx_bin, kong_conf.prefix, "nginx.conf") @@ -86,8 +83,12 @@ function _M.start(kong_conf) return true end -function _M.stop(kong_conf, graceful) - return send_signal(kong_conf.nginx_pid, graceful and "QUIT" or "TERM") +function _M.stop(kong_conf) + return send_signal(kong_conf.nginx_pid, "TERM") +end + +function _M.quit(kong_conf, graceful) + return send_signal(kong_conf.nginx_pid, "QUIT") end function _M.reload(kong_conf) diff --git a/kong/cmd/utils/serf_signals.lua b/kong/cmd/utils/serf_signals.lua index 528fa48a2ba..aace0cf5cf1 100644 --- a/kong/cmd/utils/serf_signals.lua +++ b/kong/cmd/utils/serf_signals.lua @@ -4,7 +4,6 @@ local pl_stringx = require "pl.stringx" local pl_utils = require "pl.utils" -local pl_path = require "pl.path" local pl_file = require "pl.file" local Serf = require "kong.serf" local kill = require "kong.cmd.utils.kill" @@ -34,17 +33,11 @@ local function check_serf_bin() return nil, "could not find Serf executable (is it in your $PATH?)" end -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 _M = {} function _M.start(kong_config, dao) -- is Serf already running in this prefix? - if is_running(kong_config.serf_pid) then + if kill.is_running(kong_config.serf_pid) then log.verbose("Serf agent already running at %s", kong_config.serf_pid) return true else @@ -80,14 +73,14 @@ function _M.start(kong_config, dao) local ok = pl_utils.execute(cmd) if not ok then return nil end + log.verbose("waiting for Serf agent to be running...") + -- ensure started (just an improved version of previous Serf service) local tstart = ngx.time() local texp, started = tstart + start_timeout - repeat - log.debug("waiting for Serf agent to be running...") ngx.sleep(0.2) - started = is_running(kong_config.serf_pid) + started = kill.is_running(kong_config.serf_pid) until started or ngx.time() >= texp if not started then @@ -119,7 +112,7 @@ function _M.stop(kong_config, dao) if not ok then return nil, err end log.verbose("stopping Serf agent at %s", kong_config.serf_pid) - local code, res = kill(kong_config.serf_pid, "-15") --SIGTERM + local code, res = kill.kill(kong_config.serf_pid, "-15") --SIGTERM if code == 256 then -- If no error is returned pl_file.delete(kong_config.serf_pid) end diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index c68328ebc58..b4c2435c15b 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -17,7 +17,7 @@ describe("kong start/stop", function() local _, stderr = helpers.kong_exec "stop --help" assert.not_equal("", stderr) end) - it("start/stop default conf/prefix", function() + it("start/stop gracefully with default conf/prefix", function() -- don't want to force migrations to be run on default -- keyspace/database assert(helpers.kong_exec("start", { @@ -31,9 +31,13 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) - it("start/stop gracefully", function() + it("start/stop forcefully", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) + assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) + end) + it("start/quit gracefully with --timeout option", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("quit --timeout 2 --prefix "..helpers.test_conf.prefix)) end) it("start with inexistent prefix", function() finally(function() diff --git a/spec/02-integration/01-cmd/04-reload_spec.lua b/spec/02-integration/01-cmd/04-reload_spec.lua index cc9b6833c5e..f2d2fa21e28 100644 --- a/spec/02-integration/01-cmd/04-reload_spec.lua +++ b/spec/02-integration/01-cmd/04-reload_spec.lua @@ -18,7 +18,7 @@ describe("kong reload", function() local nginx_pid = helpers.file.read(helpers.test_conf.nginx_pid) -- kong_exec uses test conf too, so same prefix - local ok, err = assert(helpers.kong_exec("reload --prefix "..helpers.test_conf.prefix)) + assert(helpers.kong_exec("reload --prefix "..helpers.test_conf.prefix)) -- same master PID assert.equal(nginx_pid, helpers.file.read(helpers.test_conf.nginx_pid)) diff --git a/spec/02-integration/01-cmd/10-quit_spec.lua b/spec/02-integration/01-cmd/10-quit_spec.lua deleted file mode 100644 index f34220d5fdf..00000000000 --- a/spec/02-integration/01-cmd/10-quit_spec.lua +++ /dev/null @@ -1,21 +0,0 @@ -local helpers = require "spec.helpers" - -describe("kong start/stop", function() - teardown(function() - helpers.kill_all() - helpers.clean_prefix() - end) - before_each(function() - helpers.kill_all() - end) - - it("quit help", function() - local _, stderr = helpers.kong_exec "quit --help" - assert.not_equal("", stderr) - end) - it("quits gracefully", function() - assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - assert(helpers.kong_exec("quit --prefix "..helpers.test_conf.prefix)) - end) - -end) diff --git a/spec/02-integration/06-cluster/cluster_spec.lua b/spec/02-integration/06-cluster/cluster_spec.lua index f1dee36f2bb..0bb6d0f806f 100644 --- a/spec/02-integration/06-cluster/cluster_spec.lua +++ b/spec/02-integration/06-cluster/cluster_spec.lua @@ -1,6 +1,5 @@ local cjson = require "cjson" local cache = require "kong.tools.database_cache" -local pl_path = require "pl.path" local helpers = require "spec.helpers" local pl_tablex = require "pl.tablex" local pl_stringx = require "pl.stringx" diff --git a/spec/helpers.lua b/spec/helpers.lua index dddd762e739..55516469af6 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -626,7 +626,7 @@ return { return kong_exec("start --conf "..TEST_CONF_PATH, env) end, stop_kong = function() - return kong_exec("stop --prefix "..conf.prefix) + return kong_exec("stop --force --prefix "..conf.prefix) end, kill_all = function() dao:truncate_tables() -- truncate nodes table too From 72177987c9f77c811a343f7a7765f30671b22ca5 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 6 Jul 2016 17:27:37 -0700 Subject: [PATCH 10/15] restart --- kong-0.8.2-0.rockspec | 1 + kong/cmd/init.lua | 2 ++ kong/cmd/restart.lua | 25 ++++++++++++++++ kong/cmd/stop.lua | 4 +-- kong/cmd/utils/nginx_signals.lua | 4 +-- .../01-cmd/02-start_stop_spec.lua | 4 +++ .../02-integration/01-cmd/08-restart_spec.lua | 30 +++++++++++++++++++ 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 kong/cmd/restart.lua create mode 100644 spec/02-integration/01-cmd/08-restart_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index 6493856e3d2..a660ba37e4e 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -61,6 +61,7 @@ build = { ["kong.cmd.start"] = "kong/cmd/start.lua", ["kong.cmd.check"] = "kong/cmd/check.lua", ["kong.cmd.reload"] = "kong/cmd/reload.lua", + ["kong.cmd.restart"] = "kong/cmd/restart.lua", ["kong.cmd.cluster"] = "kong/cmd/cluster.lua", ["kong.cmd.compile"] = "kong/cmd/compile.lua", ["kong.cmd.migrations"] = "kong/cmd/migrations.lua", diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index bf2d72e97f6..fbae12f4268 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -13,6 +13,7 @@ Usage: kong COMMAND [OPTIONS] The available commands are: start stop + restart reload check compile @@ -26,6 +27,7 @@ Options: local cmds = { start = "start", stop = "stop", + restart = "restart", reload = "reload", check = "check", compile = "compile", diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua new file mode 100644 index 00000000000..1cdac7351d7 --- /dev/null +++ b/kong/cmd/restart.lua @@ -0,0 +1,25 @@ +local conf_loader = require "kong.conf_loader" +local stop = require "kong.cmd.stop" +local start = require "kong.cmd.start" + +local function execute(args) + local conf = assert(conf_loader(args.conf)) + args.prefix = conf.prefix -- Required for stop + args.graceful = nil -- Restart is always not graceful (reload should be used instead) + + pcall(stop.execute, args) + start.execute(args) +end + +local lapp = [[ +Usage: kong restart [OPTIONS] + +Options: + -c,--conf (optional string) configuration file + --prefix (optional string) override prefix directory +]] + +return { + lapp = lapp, + execute = execute +} \ No newline at end of file diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index 45c45f6aba7..52fdffcb727 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,12 +16,12 @@ local function execute(args) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf)) + assert(nginx_signals.stop(conf, args.graceful)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped") + log("Stopped%s", args.graceful and " gracefully" or "") end local lapp = [[ diff --git a/kong/cmd/utils/nginx_signals.lua b/kong/cmd/utils/nginx_signals.lua index e605f013c55..44785a143b3 100644 --- a/kong/cmd/utils/nginx_signals.lua +++ b/kong/cmd/utils/nginx_signals.lua @@ -86,8 +86,8 @@ function _M.start(kong_conf) return true end -function _M.stop(kong_conf) - return send_signal(kong_conf.nginx_pid, "QUIT") +function _M.stop(kong_conf, graceful) + return send_signal(kong_conf.nginx_pid, graceful and "QUIT" or "TERM") end function _M.reload(kong_conf) diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index a532f7c9ec2..a0114bc5f6b 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -31,6 +31,10 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) + it("start/stop gracefully", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) + end) it("start with inexistent prefix", function() finally(function() pcall(helpers.dir.rmtree, "foobar") diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua new file mode 100644 index 00000000000..0fccc2b9f4a --- /dev/null +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -0,0 +1,30 @@ +local helpers = require "spec.helpers" + +describe("kong restart", function() + before_each(function() + helpers.kill_all() + end) + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + + 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 = ""})) + + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) + + assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""})) + + assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) + 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) From 30bb41c882910aa6bb10547b47e49e91afb5c878 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 6 Jul 2016 23:21:36 -0700 Subject: [PATCH 11/15] health --- kong-0.8.2-0.rockspec | 1 + kong/cmd/health.lua | 54 +++++++++++++++++++ kong/cmd/init.lua | 1 + .../02-integration/01-cmd/08-restart_spec.lua | 2 - spec/02-integration/01-cmd/09-health_spec.lua | 33 ++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 kong/cmd/health.lua create mode 100644 spec/02-integration/01-cmd/09-health_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index a660ba37e4e..b6ef33f53d3 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -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", diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua new file mode 100644 index 00000000000..1102c85eb06 --- /dev/null +++ b/kong/cmd/health.lua @@ -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 +} \ No newline at end of file diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index fbae12f4268..518c3d2c531 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -34,6 +34,7 @@ local cmds = { migrations = "migrations", cluster = "cluster", version = "version", + health = "health", roar = "roar" } diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index 0fccc2b9f4a..b4d874090f5 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -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 = ""})) @@ -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) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua new file mode 100644 index 00000000000..0c62f691b28 --- /dev/null +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -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) From e8cb830773a8c075b06373977bdac3062d0f77e6 Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 7 Jul 2016 14:02:57 -0700 Subject: [PATCH 12/15] refactor --- kong/cmd/health.lua | 21 ++++++++++----------- kong/cmd/restart.lua | 1 - 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index 1102c85eb06..c526a0a59b6 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -1,27 +1,26 @@ 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 pl_path = require "pl.path" +local kill = require "kong.cmd.utils.kill" +local log = require "kong.cmd.utils.log" 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 + return kill(pid_path, "-0") == 0 end local function execute(args) - local default_conf = assert(conf_loader(args.conf, { + local conf = assert(conf_loader(args.conf, { prefix = args.prefix })) - assert(pl_path.exists(default_conf.prefix), - "no such prefix: "..default_conf.prefix) + assert(pl_path.exists(conf.prefix), + "no such prefix: "..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 + nginx = conf.nginx_pid, + serf = conf.serf_pid, + dnsmasq = conf.dnsmasq and conf.dnsmasq_pid or nil } local count = 0 diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua index 1cdac7351d7..7b583034831 100644 --- a/kong/cmd/restart.lua +++ b/kong/cmd/restart.lua @@ -5,7 +5,6 @@ local start = require "kong.cmd.start" local function execute(args) local conf = assert(conf_loader(args.conf)) args.prefix = conf.prefix -- Required for stop - args.graceful = nil -- Restart is always not graceful (reload should be used instead) pcall(stop.execute, args) start.execute(args) From c566b76edff841fdd82c59916075a17708ebd5e2 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 7 Jul 2016 16:47:25 -0700 Subject: [PATCH 13/15] refactor(cli) better health/restart impl + tests --- kong/cmd/health.lua | 52 +++++++++------- kong/cmd/restart.lua | 13 ++-- .../01-cmd/02-start_stop_spec.lua | 4 +- .../02-integration/01-cmd/08-restart_spec.lua | 42 +++++++++++-- spec/02-integration/01-cmd/09-health_spec.lua | 59 ++++++++++++++----- 5 files changed, 124 insertions(+), 46 deletions(-) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index c526a0a59b6..7aec15177e2 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -1,25 +1,35 @@ -local conf_loader = require "kong.conf_loader" -local pl_stringx = require "pl.stringx" -local pl_tablex = require "pl.tablex" -local pl_path = require "pl.path" -local kill = require "kong.cmd.utils.kill" local log = require "kong.cmd.utils.log" +local kill = require "kong.cmd.utils.kill" +local pl_path = require "pl.path" +local pl_tablex = require "pl.tablex" +local pl_stringx = require "pl.stringx" +local conf_loader = require "kong.conf_loader" local function is_running(pid_path) - if not pl_path.exists(pid_path) then return nil end + if not pl_path.exists(pid_path) then return end return kill(pid_path, "-0") == 0 end local function execute(args) - local conf = assert(conf_loader(args.conf, { - prefix = args.prefix - })) - assert(pl_path.exists(conf.prefix), - "no such prefix: "..conf.prefix) + local conf + + if args.prefix then + -- retrieve prefix or use given one + local default_conf = assert(conf_loader(nil, { + prefix = args.prefix + })) + assert(pl_path.exists(default_conf.prefix), + "no such prefix: "..default_conf.prefix) + + -- load /kong.conf containing running node's config + conf = assert(conf_loader(default_conf.kong_conf)) + else + conf = assert(conf_loader(args.conf)) + end local pids = { - nginx = conf.nginx_pid, serf = conf.serf_pid, + nginx = conf.nginx_pid, dnsmasq = conf.dnsmasq and conf.dnsmasq_pid or nil } @@ -29,14 +39,16 @@ local function execute(args) 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 + log(msg) end - - assert(count > 0, "Kong is not running") - assert(count == pl_tablex.size(pids), "Some services are not running") + + log("") -- line jump + + assert(count > 0, "Kong is not running at "..conf.prefix) + assert(count == pl_tablex.size(pids), "Some services are not running at "..conf.prefix) + + log("Kong is healthy at %s", conf.prefix) end local lapp = [[ @@ -44,10 +56,10 @@ Usage: kong health [OPTIONS] Options: -c,--conf (optional string) configuration file - --prefix (optional string) override prefix directory + --prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/kong/cmd/restart.lua b/kong/cmd/restart.lua index 7b583034831..863acb690cc 100644 --- a/kong/cmd/restart.lua +++ b/kong/cmd/restart.lua @@ -1,10 +1,13 @@ -local conf_loader = require "kong.conf_loader" local stop = require "kong.cmd.stop" local start = require "kong.cmd.start" +local conf_loader = require "kong.conf_loader" local function execute(args) - local conf = assert(conf_loader(args.conf)) - args.prefix = conf.prefix -- Required for stop + if args.conf then + -- retrieve the prefix for stop + local conf = assert(conf_loader(args.conf)) + args.prefix = conf.prefix + end pcall(stop.execute, args) start.execute(args) @@ -15,10 +18,10 @@ Usage: kong restart [OPTIONS] Options: -c,--conf (optional string) configuration file - --prefix (optional string) override prefix directory + --prefix (optional string) prefix at which Kong should be running ]] return { lapp = lapp, execute = execute -} \ No newline at end of file +} diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index a0114bc5f6b..476aa412b5b 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -20,11 +20,11 @@ describe("kong start/stop", function() it("start/stop default conf/prefix", function() -- don't want to force migrations to be run on default -- keyspace/database - assert(helpers.kong_exec "start", { + assert(helpers.kong_exec("start", { database = helpers.test_conf.database, pg_database = helpers.test_conf.pg_database, cassandra_keyspace = helpers.test_conf.cassandra_keyspace - }) + })) assert(helpers.kong_exec "stop") end) it("start/stop custom Kong conf/prefix", function() diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index b4d874090f5..1a29b5b1f98 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -3,6 +3,7 @@ local helpers = require "spec.helpers" describe("kong restart", function() before_each(function() helpers.kill_all() + helpers.prepare_prefix() end) teardown(function() helpers.kill_all() @@ -12,17 +13,50 @@ 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 = ""})) + it("restarts if already running from --conf", function() + local env = { + dnsmasq = true, + dns_resolver = "" + } - local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, env)) local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) - assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""})) + assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path, env)) + assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) + 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) + it("restarts if already running from --prefix", function() + local env = { + dnsmasq = true, + dns_resolver = "" + } + + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, env)) + local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid)) + local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid)) + local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)) + assert(helpers.kong_exec("restart --prefix "..helpers.test_conf.prefix, env)) assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid) 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) + it("restarts with default configuration and prefix", function() + -- don't want to force migrations to be run on default + -- keyspace/database + local env = { + database = helpers.test_conf.database, + pg_database = helpers.test_conf.pg_database, + cassandra_keyspace = helpers.test_conf.cassandra_keyspace, + dnsmasq = true, + dns_resolver = "" + } + + assert(helpers.kong_exec("start", env)) + assert(helpers.kong_exec("restart", env)) + end) end) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua index 0c62f691b28..90a047f240e 100644 --- a/spec/02-integration/01-cmd/09-health_spec.lua +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -1,33 +1,62 @@ local helpers = require "spec.helpers" -local prefix_handler = require "kong.cmd.utils.prefix_handler" describe("kong restart", function() before_each(function() helpers.kill_all() + helpers.prepare_prefix() 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)) + it("succeeds when Kong is running with --conf", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local _, _, stdout = assert(helpers.kong_exec("health --conf "..helpers.test_conf_path)) + assert.matches("serf%.-running", stdout) + assert.matches("nginx%.-running", stdout) + assert.not_matches("dnsmasq.*running", stdout) + assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) + end) + it("succeeds when Kong is running with --prefix", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local _, _, stdout = assert(helpers.kong_exec("health --prefix "..helpers.test_conf.prefix)) + assert.matches("serf%.-running", stdout) + assert.matches("nginx%.-running", stdout) + assert.not_matches("dnsmasq.*running", stdout) + assert.matches("Kong is healthy at "..helpers.test_conf.prefix, stdout, nil, true) + end) + it("fails when Kong is not running", function() + local ok, stderr, stdout = helpers.kong_exec("health --conf "..helpers.test_conf_path) + assert.False(ok) + assert.matches("Kong is not running at "..helpers.test_conf.prefix, stderr, nil, true) + end) + it("fails when a service is not running", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + 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, nil, true) + end) + it("checks dnsmasq if enabled", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + + local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path, { + dnsmasq = true, + dns_resolver = "" + }) + assert.False(ok) + assert.matches("Some services are not running", stderr, nil, true) 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) + it("errors on inexisting prefix", function() + local ok, stderr = helpers.kong_exec("health --prefix inexistant") assert.False(ok) - assert.matches("Some services are not running", stderr) + assert.matches("no such prefix: ", stderr, nil, true) end) end) end) From 6e00b811a02bd1298839a5efc6b76a6cfb9db8c5 Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 7 Jul 2016 20:35:55 -0700 Subject: [PATCH 14/15] kong quit and tests --- kong-0.8.2-0.rockspec | 1 + kong/cmd/init.lua | 1 + kong/cmd/quit.lua | 37 ++++++++++++++ kong/cmd/stop.lua | 4 +- .../01-cmd/02-start_stop_spec.lua | 4 -- .../02-integration/01-cmd/07-cluster_spec.lua | 50 ++++++++++++++++++- .../02-integration/01-cmd/08-restart_spec.lua | 4 ++ spec/02-integration/01-cmd/09-health_spec.lua | 8 +++ spec/02-integration/01-cmd/10-quit_spec.lua | 21 ++++++++ 9 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 kong/cmd/quit.lua create mode 100644 spec/02-integration/01-cmd/10-quit_spec.lua diff --git a/kong-0.8.2-0.rockspec b/kong-0.8.2-0.rockspec index b6ef33f53d3..33628dcdbcb 100644 --- a/kong-0.8.2-0.rockspec +++ b/kong-0.8.2-0.rockspec @@ -58,6 +58,7 @@ build = { ["kong.cmd.roar"] = "kong/cmd/roar.lua", ["kong.cmd.init"] = "kong/cmd/init.lua", ["kong.cmd.stop"] = "kong/cmd/stop.lua", + ["kong.cmd.quit"] = "kong/cmd/quit.lua", ["kong.cmd.start"] = "kong/cmd/start.lua", ["kong.cmd.check"] = "kong/cmd/check.lua", ["kong.cmd.reload"] = "kong/cmd/reload.lua", diff --git a/kong/cmd/init.lua b/kong/cmd/init.lua index 518c3d2c531..4001b394820 100644 --- a/kong/cmd/init.lua +++ b/kong/cmd/init.lua @@ -27,6 +27,7 @@ Options: local cmds = { start = "start", stop = "stop", + quit = "quit", restart = "restart", reload = "reload", check = "check", diff --git a/kong/cmd/quit.lua b/kong/cmd/quit.lua new file mode 100644 index 00000000000..3e1df719665 --- /dev/null +++ b/kong/cmd/quit.lua @@ -0,0 +1,37 @@ +local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals" +local nginx_signals = require "kong.cmd.utils.nginx_signals" +local serf_signals = require "kong.cmd.utils.serf_signals" +local conf_loader = require "kong.conf_loader" +local DAOFactory = require "kong.dao.factory" +local pl_path = require "pl.path" +local log = require "kong.cmd.utils.log" + +local function execute(args) + -- retrieve prefix or use given one + local default_conf = assert(conf_loader(nil, { + prefix = args.prefix + })) + assert(pl_path.exists(default_conf.prefix), + "no such prefix: "..default_conf.prefix) + + -- load /kong.conf containing running node's config + local conf = assert(conf_loader(default_conf.kong_conf)) + assert(nginx_signals.stop(conf, true)) + assert(serf_signals.stop(conf, DAOFactory(conf))) + if conf.dnsmasq then + assert(dnsmasq_signals.stop(conf)) + end + log("Stopped gracefully") +end + +local lapp = [[ +Usage: kong quit [OPTIONS] + +Options: + --prefix (optional string) prefix Kong is running at +]] + +return { + lapp = lapp, + execute = execute +} diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index 52fdffcb727..45c45f6aba7 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,12 +16,12 @@ local function execute(args) -- load /kong.conf containing running node's config local conf = assert(conf_loader(default_conf.kong_conf)) - assert(nginx_signals.stop(conf, args.graceful)) + assert(nginx_signals.stop(conf)) assert(serf_signals.stop(conf, DAOFactory(conf))) if conf.dnsmasq then assert(dnsmasq_signals.stop(conf)) end - log("Stopped%s", args.graceful and " gracefully" or "") + log("Stopped") end local lapp = [[ diff --git a/spec/02-integration/01-cmd/02-start_stop_spec.lua b/spec/02-integration/01-cmd/02-start_stop_spec.lua index 476aa412b5b..1be1bbe59a0 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -31,10 +31,6 @@ describe("kong start/stop", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix)) end) - it("start/stop gracefully", function() - assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) - assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix)) - end) it("start with inexistent prefix", function() finally(function() pcall(helpers.dir.rmtree, "foobar") diff --git a/spec/02-integration/01-cmd/07-cluster_spec.lua b/spec/02-integration/01-cmd/07-cluster_spec.lua index dff1af66046..cd318185d56 100644 --- a/spec/02-integration/01-cmd/07-cluster_spec.lua +++ b/spec/02-integration/01-cmd/07-cluster_spec.lua @@ -1,8 +1,54 @@ local helpers = require "spec.helpers" describe("kong cluster", function() - it("keygen", function() - local _, _, stdout = assert(helpers.kong_exec "cluster keygen") + before_each(function() + helpers.kill_all() + end) + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + + it("cluster help", function() + local _, stderr = helpers.kong_exec "cluster --help" + assert.not_equal("", stderr) + end) + it("generates a key", function() + local _, stderr, stdout = assert(helpers.kong_exec "cluster keygen") + assert.equal("", stderr) assert.equal(26, stdout:len()) -- 24 + \r\n end) + it("shows members", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster members --prefix "..helpers.test_conf.prefix)) + assert.matches("alive", stdout) + end) + it("shows rechability", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster reachability --prefix "..helpers.test_conf.prefix)) + assert.matches("Successfully contacted all live nodes", stdout) + end) + it("force-leaves a node", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + local _, _, stdout = assert(helpers.kong_exec("cluster force-leave 127.0.0.1 --prefix "..helpers.test_conf.prefix)) + assert.matches("left node 127.0.0.1", stdout) + end) + + describe("errors", function() + it("fails to show members when Kong is not running", function() + local ok, stderr = helpers.kong_exec("cluster members --prefix "..helpers.test_conf.prefix) + assert.False(ok) + assert.matches("Error connecting to Serf agent", stderr) + end) + it("fails to show reachability when Kong is not running", function() + local ok, stderr = helpers.kong_exec("cluster reachability --prefix "..helpers.test_conf.prefix) + assert.False(ok) + assert.matches("Error connecting to Serf agent", stderr) + end) + it("fails to force-leave when a node is not specified", function() + local ok, stderr = helpers.kong_exec("cluster force-leave --prefix "..helpers.test_conf.prefix) + assert.False(ok) + assert.matches("must specify the name of the node to leave", stderr) + end) + end) end) diff --git a/spec/02-integration/01-cmd/08-restart_spec.lua b/spec/02-integration/01-cmd/08-restart_spec.lua index 1a29b5b1f98..cd09d8e976f 100644 --- a/spec/02-integration/01-cmd/08-restart_spec.lua +++ b/spec/02-integration/01-cmd/08-restart_spec.lua @@ -10,6 +10,10 @@ describe("kong restart", function() helpers.clean_prefix() end) + it("restart help", function() + local _, stderr = helpers.kong_exec "health --help" + assert.not_equal("", stderr) + end) it("restarts if not running", function() assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path)) end) diff --git a/spec/02-integration/01-cmd/09-health_spec.lua b/spec/02-integration/01-cmd/09-health_spec.lua index 90a047f240e..f5823f3d2ad 100644 --- a/spec/02-integration/01-cmd/09-health_spec.lua +++ b/spec/02-integration/01-cmd/09-health_spec.lua @@ -51,6 +51,14 @@ describe("kong restart", function() assert.False(ok) assert.matches("Some services are not running", stderr, nil, true) end) + it("health help", function() + local _, stderr = helpers.kong_exec "health --help" + assert.not_equal("", stderr) + 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("errors on inexisting prefix", function() diff --git a/spec/02-integration/01-cmd/10-quit_spec.lua b/spec/02-integration/01-cmd/10-quit_spec.lua new file mode 100644 index 00000000000..f34220d5fdf --- /dev/null +++ b/spec/02-integration/01-cmd/10-quit_spec.lua @@ -0,0 +1,21 @@ +local helpers = require "spec.helpers" + +describe("kong start/stop", function() + teardown(function() + helpers.kill_all() + helpers.clean_prefix() + end) + before_each(function() + helpers.kill_all() + end) + + it("quit help", function() + local _, stderr = helpers.kong_exec "quit --help" + assert.not_equal("", stderr) + end) + it("quits gracefully", function() + assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) + assert(helpers.kong_exec("quit --prefix "..helpers.test_conf.prefix)) + end) + +end) From 5d35ee23901f3ac83065e9e3242a53bd1efe0d82 Mon Sep 17 00:00:00 2001 From: thefosk Date: Fri, 8 Jul 2016 11:36:06 -0700 Subject: [PATCH 15/15] better assert.matches --- spec/02-integration/01-cmd/07-cluster_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/02-integration/01-cmd/07-cluster_spec.lua b/spec/02-integration/01-cmd/07-cluster_spec.lua index cd318185d56..b0e4b9bbe62 100644 --- a/spec/02-integration/01-cmd/07-cluster_spec.lua +++ b/spec/02-integration/01-cmd/07-cluster_spec.lua @@ -31,7 +31,7 @@ describe("kong cluster", function() it("force-leaves a node", function() assert(helpers.kong_exec("start --conf "..helpers.test_conf_path)) local _, _, stdout = assert(helpers.kong_exec("cluster force-leave 127.0.0.1 --prefix "..helpers.test_conf.prefix)) - assert.matches("left node 127.0.0.1", stdout) + assert.matches("left node 127.0.0.1", stdout, nil, true) end) describe("errors", function()