Skip to content

Commit

Permalink
feat(conf) now dump compiled conf in prefix
Browse files Browse the repository at this point in the history
to avoid giving `stop` and other commands a configuration file argument,
we now store the configuration in Kong's working directory (or Nginx
prefix as it is now refered to).
  • Loading branch information
thibaultcha committed Jun 22, 2016
1 parent 1f4f758 commit 9702ede
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 169 deletions.
6 changes: 4 additions & 2 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
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 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 @@ -21,7 +21,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))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf, conf.prefix))
end
assert(serf_signals.start(conf, conf.prefix, dao))
assert(nginx_signals.start(conf.prefix))
log("Started")
Expand Down
7 changes: 4 additions & 3 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 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,10 +11,11 @@ local function execute(args)
}))

local dao = DAOFactory(conf)

assert(nginx_signals.stop(conf.prefix))
assert(serf_signals.stop(conf, conf.prefix, dao))
assert(dnsmasq_signals.stop(conf.prefix))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf.prefix))
end
log("Stopped")
end

Expand Down
35 changes: 17 additions & 18 deletions kong/cmd/utils/dnsmasq_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,26 @@ local function is_running(pid_path)
end

function _M.start(kong_config, nginx_prefix)
if kong_config.dnsmasq then
-- is dnsmasq already running in this prefix?
local pid_path = pl_path.join(nginx_prefix, "pids", 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
-- is dnsmasq already running in this prefix?
local pid_path = pl_path.join(nginx_prefix, "pids", 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

local dnsmasq_bin, err = _M.find_bin()
if not dnsmasq_bin then return nil, err end
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)
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)
log.debug("starting dnsmasq: %s", cmd)

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

return true
end
Expand All @@ -82,4 +81,4 @@ function _M.stop(nginx_prefix)
return true
end

return _M
return _M
65 changes: 20 additions & 45 deletions kong/cmd/utils/nginx_conf_compiler.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
local NGINX_VARS = {
prefix = true,
plugins = true,
cluster_listen = true,
cluster_listen_rpc = true,
database = true,
pg_host = true,
pg_port = true,
pg_user = true,
pg_password = true,
pg_database = true,
cassandra_contact_points = true,
cassandra_keyspace = true,
cassandra_timeout = true,
cassandra_consistency = true,
cassandra_ssl = true,
cassandra_ssl_verify = true,
cassandra_username = true,
cassandra_password = true,
anonymous_reports = true
}

local kong_nginx_template = require "kong.templates.nginx_kong"
local nginx_template = require "kong.templates.nginx"
local pl_template = require "pl.template"
local pl_stringx = require "pl.stringx"
local pl_pretty = require "pl.pretty"
local pl_tablex = require "pl.tablex"
local pl_utils = require "pl.utils"
local pl_file = require "pl.file"
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
local ssl = require "kong.cmd.utils.ssl"
local log = require "kong.cmd.utils.log"
local fmt = string.format

local function gather_system_infos(compile_env)
local infos = {}
Expand All @@ -53,19 +29,6 @@ local function compile_conf(kong_config, conf_template)
nginx_vars = {}
}

-- variables needed in Nginx
for k in pairs(NGINX_VARS) do
local v = kong_config[k]
local typ = type(v)
if typ == "table" then
v = pl_pretty.write(v, string.rep(" ", 6), true)
elseif typ == "string" then
v = string.format("%q", v)
end

compile_env.nginx_vars[k] = v
end

local ssl_data, err = ssl.get_ssl_cert_and_key(kong_config, kong_config.prefix)
if not ssl_data then return nil, err end

Expand Down Expand Up @@ -110,10 +73,10 @@ local function touch(file_path)
end

local function prepare_prefix(kong_config, nginx_prefix)
log.verbose("preparing prefix directory at %s", nginx_prefix)
log.verbose("preparing nginx prefix directory at %s", nginx_prefix)

if not pl_path.exists(nginx_prefix) then
log.verbose(fmt("prefix directory %s not found, trying to create it", nginx_prefix))
log("prefix directory %s not found, trying to create it", nginx_prefix)
local ok, err = pl_dir.makepath(nginx_prefix)
if not ok then return nil, err end
elseif not pl_path.isdir(nginx_prefix) then
Expand All @@ -133,7 +96,7 @@ local function prepare_prefix(kong_config, nginx_prefix)
if not ok then return nil, stderr end
local ok, _, _, stderr = touch(acc_logs_path)
if not ok then return nil, stderr end

-- pids folder
local pids_path = pl_path.join(nginx_prefix, "pids")
local ok, err = pl_dir.makepath(pids_path)
Expand All @@ -143,20 +106,32 @@ local function prepare_prefix(kong_config, nginx_prefix)
local ok, err = ssl.prepare_ssl_cert_and_key(nginx_prefix)
if not ok then return nil, err end

local nginx_config_path = pl_path.join(nginx_prefix, "nginx.conf")
local kong_conf_path = pl_path.join(nginx_prefix, "kong.conf")
local nginx_conf_path = pl_path.join(nginx_prefix, "nginx.conf")
local kong_nginx_conf_path = pl_path.join(nginx_prefix, "nginx-kong.conf")

-- write NGINX conf
local nginx_conf, err = compile_nginx_conf(kong_config)
if not nginx_conf then return nil, err end
local ok, err = pl_file.write(nginx_config_path, nginx_conf)
if not ok then return nil, err end

pl_file.write(nginx_conf_path, nginx_conf)

-- write Kong's NGINX conf
local kong_nginx_conf, err = compile_kong_conf(kong_config)
if not kong_nginx_conf then return nil, err end
local ok, err = pl_file.write(kong_nginx_conf_path, kong_nginx_conf)
if not ok then return nil, err end
pl_file.write(kong_nginx_conf_path, kong_nginx_conf)

-- write kong.conf in prefix (for workers and CLI)
local buf = {}
for k, v in pairs(kong_config) do
if type(v) == "table" then
v = table.concat(v, ",")
end
if v ~= "" then
buf[#buf+1] = k.." = "..tostring(v)
end
end
pl_file.write(kong_conf_path, table.concat(buf, "\n"))

return true
end
Expand Down
24 changes: 6 additions & 18 deletions kong/cmd/utils/serf_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,19 @@ client:request { \
resty -e "$CMD"
]]

local function prepare_identifier(kong_config, nginx_prefix)
local serf_path = pl_path.join(nginx_prefix, "serf")
local ok, err = pl_dir.makepath(serf_path)
if not ok then return nil, err end

local function prepare_prefix(kong_config, nginx_prefix, script_path)
log.verbose("saving Serf identifier in %s", id_path)
local id_path = pl_path.join(nginx_prefix, "serf", serf_node_id)
if not pl_path.exists(id_path) then
local id = utils.get_hostname().."_"..kong_config.cluster_listen.."_"..utils.random_string()

log.verbose("saving Serf identifier in %s", id_path)
local ok, err = pl_file.write(id_path, id)
if not ok then return nil, err end
pl_file.write(id_path, id)
end
return true
end

local function prepare_prefix(kong_config, nginx_prefix, script_path)
local ok, err = prepare_identifier(kong_config, nginx_prefix)
if not ok then return nil, err end

log.verbose("dumping Serf shell script handler in %s", script_path)
log.verbose("saving Serf shell script handler in %s", script_path)
local script = fmt(script_template, "127.0.0.1", kong_config.admin_port)

local ok, err = pl_file.write(script_path, script)
if not ok then return nil, err end
pl_file.write(script_path, script)

local ok, _, _, stderr = pl_utils.executeex("chmod +x "..script_path)
if not ok then return nil, stderr end

Expand Down
14 changes: 9 additions & 5 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ end

local Kong = {}

function Kong.init(config)
-- retrieve node plugins
local events = Events()
function Kong.init()
local pl_path = require "pl.path"
local conf_loader = require "kong.conf_loader"

-- instanciate long-lived DAO
local dao = DAOFactory(config, events)
-- retrieve kong_config
local conf_path = pl_path.join(ngx.config.prefix(), "kong.conf")
local config = assert(conf_loader(conf_path))

local events = Events() -- retrieve node plugins
local dao = DAOFactory(config, events) -- instanciate long-lived DAO
assert(dao:run_migrations()) -- migrating in case embedded in custom nginx

-- populate singletons
Expand Down
2 changes: 1 addition & 1 deletion kong/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Serf.args_mt = {

function Serf.new(kong_config, nginx_prefix, dao)
return setmetatable({
node_name = assert(pl_file.read(pl_path.join(nginx_prefix, "serf", serf_node_id))),
node_name = pl_file.read(pl_path.join(nginx_prefix, "serf", serf_node_id)),
config = kong_config,
dao = dao
}, Serf)
Expand Down
7 changes: 1 addition & 6 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ lua_ssl_trusted_certificate '${{lua_ssl_trusted_certificate}}';
> end
init_by_lua_block {
local config = {}
> for k, v in pairs(nginx_vars) do
config["$(k)"] = $(tostring(v))
> end
kong = require 'kong'
kong.init(config)
kong.init()
}
init_worker_by_lua_block {
Expand Down
32 changes: 16 additions & 16 deletions spec/01-unit/01-conf/01-conf_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,7 @@ describe("Configuration loader", function()
assert.is_nil(conf)
assert.equal("proxy_listen_ssl must be of form 'address:port'", err)
end)
end)

describe("errors", function()
it("returns inexistent file", function()
local conf, err = conf_loader "inexistent"
assert.equal("no file at: inexistent", err)
assert.is_nil(conf)
end)
it("returns a DNS error when both a resolver and dnsmasq are enabled", function()
it("errors when both a resolver and dnsmasq are enabled", function()
local conf, err = conf_loader(nil, {
dnsmasq = true,
dns_resolver = "8.8.8.8:53"
Expand All @@ -213,6 +205,14 @@ describe("Configuration loader", function()
assert.equal("cluster_ttl_on_failure must be at least 60 seconds", err)
assert.is_nil(conf)
end)
it("does not check SSL cert and key if SSL is off", function()
local conf, err = conf_loader(nil, {
ssl = false,
ssl_cert = "/path/cert.pem"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("requires both SSL cert and key", function()
local conf, err = conf_loader(nil, {
ssl_cert = "/path/cert.pem"
Expand All @@ -233,13 +233,13 @@ describe("Configuration loader", function()
assert.is_nil(err)
assert.is_table(conf)
end)
it("does not check SSL cert and key if SSL is off", function()
local conf, err = conf_loader(nil, {
ssl = false,
ssl_cert = "/path/cert.pem"
})
assert.is_nil(err)
assert.is_table(conf)
end)

describe("errors", function()
it("returns inexistent file", function()
local conf, err = conf_loader "inexistent"
assert.equal("no file at: inexistent", err)
assert.is_nil(conf)
end)
it("returns all errors in ret value #3", function()
local conf, _, errors = conf_loader(nil, {
Expand Down
Loading

0 comments on commit 9702ede

Please sign in to comment.