Skip to content

Commit

Permalink
feat(cli) store compiled config in hidden file
Browse files Browse the repository at this point in the history
We thus avoid users being aware of this file altogether.
  • Loading branch information
thibaultcha committed Feb 28, 2017
1 parent 41bbcf6 commit c25b284
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion kong/cmd/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function execute(args)
-- load <PREFIX>/kong.conf containing running node's config
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)
local conf = assert(conf_loader(default_conf.kong_conf))
local conf = assert(conf_loader(default_conf.kong_env))
local dao = assert(DAOFactory.new(conf))
local serf = Serf.new(conf, dao)

Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ local function execute(args)
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)
assert(pl_path.exists(default_conf.kong_conf),
assert(pl_path.exists(default_conf.kong_env),
"Kong is not running at "..default_conf.prefix)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
local conf = assert(conf_loader(default_conf.kong_env))

local pids = {
serf = conf.serf_pid,
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/quit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local function execute(args)
"no such prefix: "..default_conf.prefix)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
local conf = assert(conf_loader(default_conf.kong_env))

-- try graceful shutdown (QUIT)
assert(nginx_signals.quit(conf))
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local function execute(args)
"no such prefix: "..default_conf.prefix)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf, {
local conf = assert(conf_loader(default_conf.kong_env, {
prefix = args.prefix
}))
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local function execute(args)
"no such prefix: "..default_conf.prefix)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
local conf = assert(conf_loader(default_conf.kong_env))
local dao = assert(DAOFactory.new(conf))
assert(nginx_signals.stop(conf))
assert(serf_signals.stop(conf, dao))
Expand Down
13 changes: 8 additions & 5 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,16 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)

-- write kong.conf in prefix (for workers and CLI)
local buf = {
"# *******************************",
"# * NOTE: DO NOT EDIT THIS FILE *",
"# *******************************\n",
"# *************************",
"# * DO NOT EDIT THIS FILE *",
"# *************************",
"# This configuration file is auto-generated. If you want to modify",
"# the Kong configuration please edit/create the original `kong.conf`",
"# the Kong configuration please edit/create the original `kong.conf`",
"# file. Any modifications made here will be lost.",
"# Start Kong with `--vv` to show where it is looking for that file.",
"",
}

for k, v in pairs(kong_config) do
if type(v) == "table" then
v = table.concat(v, ",")
Expand All @@ -309,7 +311,8 @@ local function prepare_prefix(kong_config, nginx_custom_template_path)
buf[#buf+1] = k.." = "..tostring(v)
end
end
pl_file.write(kong_config.kong_conf, table.concat(buf, "\n"))

pl_file.write(kong_config.kong_env, table.concat(buf, "\n"))

return true
end
Expand Down
2 changes: 1 addition & 1 deletion kong/conf_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local PREFIX_PATHS = {
nginx_conf = {"nginx.conf"},
nginx_kong_conf = {"nginx-kong.conf"}
;
kong_conf = {"kong.conf"}
kong_env = {".kong_env"}
;
ssl_cert_default = {"ssl", "kong-default.crt"},
ssl_cert_key_default = {"ssl", "kong-default.key"},
Expand Down
2 changes: 1 addition & 1 deletion kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function Kong.init()
local conf_loader = require "kong.conf_loader"

-- retrieve kong_config
local conf_path = pl_path.join(ngx.config.prefix(), "kong.conf")
local conf_path = pl_path.join(ngx.config.prefix(), ".kong_env")
local config = assert(conf_loader(conf_path))

local events = Events() -- retrieve node plugins
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/02-conf_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("Configuration loader", function()
assert.equal("/usr/local/kong/logs/admin_access.log", conf.nginx_admin_acc_logs)
assert.equal("/usr/local/kong/nginx.conf", conf.nginx_conf)
assert.equal("/usr/local/kong/nginx-kong.conf", conf.nginx_kong_conf)
assert.equal("/usr/local/kong/kong.conf", conf.kong_conf)
assert.equal("/usr/local/kong/.kong_env", conf.kong_env)
-- ssl default paths
assert.equal("/usr/local/kong/ssl/kong-default.crt", conf.ssl_cert_default)
assert.equal("/usr/local/kong/ssl/kong-default.key", conf.ssl_cert_key_default)
Expand Down
8 changes: 4 additions & 4 deletions spec/01-unit/03-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ describe("NGINX conf compiler", function()
end)
it("creates NGINX conf and log files", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(tmp_config.kong_conf))
assert.truthy(exists(tmp_config.kong_env))
assert.truthy(exists(tmp_config.nginx_kong_conf))
assert.truthy(exists(tmp_config.nginx_err_logs))
assert.truthy(exists(tmp_config.nginx_acc_logs))
assert.truthy(exists(tmp_config.nginx_admin_acc_logs))
end)
it("dumps Kong conf", function()
assert(prefix_handler.prepare_prefix(tmp_config))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(tmp_config, in_prefix_kong_conf)
end)
it("dump Kong conf (custom conf)", function()
Expand All @@ -221,7 +221,7 @@ describe("NGINX conf compiler", function()
}))
assert.equal("foobar", conf.pg_database)
assert(prefix_handler.prepare_prefix(conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(conf, in_prefix_kong_conf)
end)
it("writes custom plugins in Kong conf", function()
Expand All @@ -232,7 +232,7 @@ describe("NGINX conf compiler", function()

assert(prefix_handler.prepare_prefix(conf))

local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.True(in_prefix_kong_conf.plugins.foo)
assert.True(in_prefix_kong_conf.plugins.bar)
end)
Expand Down
2 changes: 1 addition & 1 deletion spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("kong start/stop", function()
end)
it("start dumps Kong config in prefix", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert.truthy(helpers.path.exists(helpers.test_conf.kong_conf))
assert.truthy(helpers.path.exists(helpers.test_conf.kong_env))
end)
it("creates prefix directory if it doesn't exist", function()
finally(function()
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ return {
dao:truncate_tables()

local default_conf = conf_loader(nil, {prefix = prefix or conf.prefix})
local running_conf = conf_loader(default_conf.kong_conf)
local running_conf = conf_loader(default_conf.kong_env)
if not running_conf then return end

-- kill kong_tests.conf services
Expand Down

0 comments on commit c25b284

Please sign in to comment.