Skip to content

Commit

Permalink
Adding dnsmasq
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jun 9, 2016
2 parents 68a0c29 + ce34062 commit 0cb4036
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 13 deletions.
1 change: 1 addition & 0 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ build = {
["kong.cmd.utils.nginx_conf_compiler"] = "kong/cmd/utils/nginx_conf_compiler.lua",
["kong.cmd.utils.nginx_signals"] = "kong/cmd/utils/nginx_signals.lua",
["kong.cmd.utils.serf_signals"] = "kong/cmd/utils/serf_signals.lua",
["kong.cmd.utils.dnsmasq_signals"] = "kong/cmd/utils/dnsmasq_signals.lua",
["kong.cmd.utils.ssl"] = "kong/cmd/utils/ssl.lua",

["kong.api.init"] = "kong/api/init.lua",
Expand Down
2 changes: 2 additions & 0 deletions kong/cmd/reload.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
local log = require "kong.cmd.utils.log"
Expand All @@ -11,6 +12,7 @@ local function execute(args)
}))

assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(dnsmasq_signals.start(conf, conf.prefix))
assert(serf_signals.start(conf, conf.prefix, DAOFactory(conf)))
assert(nginx_signals.reload(conf.prefix))
log("Reloaded")
Expand Down
4 changes: 3 additions & 1 deletion kong/cmd/start.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
local log = require "kong.cmd.utils.log"
Expand All @@ -19,8 +20,9 @@ local function execute(args)

local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(dnsmasq_signals.start(conf, conf.prefix))
assert(serf_signals.start(conf, conf.prefix, dao))
assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(nginx_signals.start(conf.prefix))
log("Started")
end
Expand Down
3 changes: 3 additions & 0 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local conf_loader = require "kong.conf_loader"
local log = require "kong.cmd.utils.log"

Expand All @@ -12,6 +13,8 @@ local function execute(args)

assert(nginx_signals.stop(conf.prefix))
assert(serf_signals.stop(conf.prefix))
assert(serf_signals.stop(conf.prefix))
assert(dnsmasq_signals.stop(conf.prefix))
log("Stopped")
end

Expand Down
81 changes: 81 additions & 0 deletions kong/cmd/utils/dnsmasq_signals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local log = require "kong.cmd.utils.log"
local kill = require "kong.cmd.utils.kill"
local fmt = string.format

local _M = {}

local dnsmasq_bin_name = "dnsmasq"
local dnsmasq_pid_name = "dnsmasq.pid"
local dnsmasq_search_paths = {
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/bin",
""
}

function _M.find_bin()
log.verbose("searching for 'dnsmasq' executable...")

local found
for _, path in ipairs(dnsmasq_search_paths) do
local path_to_check = pl_path.join(path, dnsmasq_bin_name)
local cmd = fmt("%s -v", path_to_check)
local ok = pl_utils.executeex(cmd)
if ok then
found = path_to_check
break
end
end

if not found then
return nil, "could not find 'dnsmasq' executable"
end

log.verbose("found 'dnsmasq' executable at %s", found)

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, nginx_prefix)
-- is dnsmasq already running in this prefix?
local pid_path = pl_path.join(nginx_prefix, dnsmasq_pid_name)
if is_running(pid_path) then
log.verbose("dnsmasq already running at %s", pid_path)
return true
else
log.verbose("dnsmasq not running, deleting %s", pid_path)
pl_file.delete(pid_path)
end

-- make sure Serf is in PATH
local dnsmasq_bin, err = _M.find_bin()
if not dnsmasq_bin then return nil, err end

local cmd = fmt("%s -p %d --pid-file=%s -N -o --listen-address=127.0.0.1", dnsmasq_bin, kong_config.dnsmasq_port, pid_path)

log.debug("starting dnsmasq: %s", cmd)

local ok, _, _, stderr = pl_utils.executeex(cmd)
if not ok then return nil, stderr end

return true
end

function _M.stop(nginx_prefix)
local pid_path = pl_path.join(nginx_prefix, dnsmasq_pid_name)
log.verbose("stopping dnsmasq at %s", pid_path)
return kill(pid_path, "-9")
end

return _M
2 changes: 1 addition & 1 deletion spec/01-unit/01-conf/02-conf_compilation_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("NGINX conf compiler", function()

it("compiles with dnsmasq", function()
local kong_nginx_conf = nginx_conf_compiler.compile_kong_conf(custom_conf_dnsmasq)
assert.matches("resolver 127.0.0.1:8053 ipv6=off;", kong_nginx_conf)
assert.matches("resolver 127.0.0.1:9053 ipv6=off;", kong_nginx_conf)
end)
it("compiles with dnsmasq and a custom port", function()
local conf = pl_tablex.deepcopy(custom_conf_dnsmasq)
Expand Down
43 changes: 37 additions & 6 deletions spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
local helpers = require "spec.helpers"

local KILL_ALL = "pkill nginx; pkill serf; pkill dnsmasq"

local function exec(args)
args = args or ""
return helpers.execute(helpers.bin_path.." "..args)
end

describe("kong start/stop", function()
setup(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.prepare_prefix()
end)
teardown(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.clean_prefix()
end)

Expand Down Expand Up @@ -60,7 +62,7 @@ describe("kong start/stop", function()
assert.equal("", stderr)

finally(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
end)
end)
it("accepts debug", function()
Expand All @@ -72,7 +74,7 @@ describe("kong start/stop", function()
assert.equal("", stderr)

finally(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
end)
end)
end)
Expand Down Expand Up @@ -106,6 +108,35 @@ describe("kong start/stop", function()
end)
end)

describe("#only dnsmasq", function()
it("starts dnsmasq daemon", function()
local ok = exec("start --conf "..helpers.test_conf_path)
assert.True(ok)

local dnsmasq_pid_path = helpers.path.join(helpers.test_conf.prefix, "dnsmasq.pid")
local cmd = string.format("kill -0 `cat %s` >/dev/null 2>&1", dnsmasq_pid_path)
local ok, code = helpers.execute(cmd)
assert.True(ok)
assert.equal(0, code)

assert.True(exec("stop --prefix "..helpers.test_conf.prefix))
end)
it("recovers from expired dnsmasq.pid file", function()
local dnsmasq_pid_path = helpers.path.join(helpers.test_conf.prefix, "dnsmasq.pid")
local ok = helpers.execute("touch "..dnsmasq_pid_path) -- dumb pid
assert.True(ok)

assert.True(exec("start --conf "..helpers.test_conf_path))

local cmd = string.format("kill -0 `cat %s` >/dev/null 2>&1", dnsmasq_pid_path)
local ok, code = helpers.execute(cmd)
assert.True(ok)
assert.equal(0, code)

assert.True(exec("stop --prefix "..helpers.test_conf.prefix))
end)
end)

describe("errors", function()
it("start inexistent Kong conf file", function()
local ok, _, stdout, stderr = exec "start --conf foobar.conf"
Expand Down Expand Up @@ -134,7 +165,7 @@ describe("kong start/stop", function()
assert.matches("Error: could not get Nginx pid", stderr, nil, true)

finally(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.dir.rmtree(helpers.test_conf.prefix)
end)
end)
Expand All @@ -151,7 +182,7 @@ describe("kong start/stop", function()
assert.equal("", stdout)
assert.matches("Nginx is already running in", stderr)
finally(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.dir.rmtree(helpers.test_conf.prefix)
end)
end)
Expand Down
8 changes: 5 additions & 3 deletions spec/02-integration/01-cmd/04-reload_spec.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
local helpers = require "spec.helpers"

local KILL_ALL = "pkill nginx; pkill serf; pkill dnsmasq"

describe("kong reload", function()
setup(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.prepare_prefix()
end)
teardown(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
helpers.clean_prefix()
end)

it("send a HUP signal to a running nginx master process", function()
finally(function()
helpers.execute "pkill nginx; pkill serf"
helpers.execute(KILL_ALL)
end)

assert(helpers.start_kong())
Expand Down
4 changes: 2 additions & 2 deletions spec/kong_tests.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cluster_listen = 0.0.0.0:9946
cluster_listen_rpc = 127.0.0.1:9373
ssl_cert = spec/fixtures/kong_spec.crt
ssl_cert_key = spec/fixtures/kong_spec.key
dnsmasq = off
dns_resolver = 8.8.8.8
dnsmasq = on
dnsmasq_port = 9053
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
Expand Down

0 comments on commit 0cb4036

Please sign in to comment.