From c25b284f3055ddd02d1ab0e1a29c513dcc24e1a0 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Mon, 27 Feb 2017 22:19:14 -0800 Subject: [PATCH] feat(cli) store compiled config in hidden file We thus avoid users being aware of this file altogether. --- kong/cmd/cluster.lua | 2 +- kong/cmd/health.lua | 4 ++-- kong/cmd/quit.lua | 2 +- kong/cmd/reload.lua | 2 +- kong/cmd/stop.lua | 2 +- kong/cmd/utils/prefix_handler.lua | 13 ++++++++----- kong/conf_loader.lua | 2 +- kong/kong.lua | 2 +- spec/01-unit/02-conf_loader_spec.lua | 2 +- spec/01-unit/03-prefix_handler_spec.lua | 8 ++++---- spec/02-integration/01-cmd/02-start_stop_spec.lua | 2 +- spec/helpers.lua | 2 +- 12 files changed, 23 insertions(+), 20 deletions(-) diff --git a/kong/cmd/cluster.lua b/kong/cmd/cluster.lua index 7d103001d7f6..56831cc627ec 100644 --- a/kong/cmd/cluster.lua +++ b/kong/cmd/cluster.lua @@ -23,7 +23,7 @@ local function execute(args) -- load /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) diff --git a/kong/cmd/health.lua b/kong/cmd/health.lua index cd7608085637..dcb9774da675 100644 --- a/kong/cmd/health.lua +++ b/kong/cmd/health.lua @@ -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 /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, diff --git a/kong/cmd/quit.lua b/kong/cmd/quit.lua index 92e8cb5f370f..73d0585f2cb8 100644 --- a/kong/cmd/quit.lua +++ b/kong/cmd/quit.lua @@ -17,7 +17,7 @@ local function execute(args) "no such prefix: "..default_conf.prefix) -- load /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)) diff --git a/kong/cmd/reload.lua b/kong/cmd/reload.lua index 394f783b9e6e..b87f692d027b 100644 --- a/kong/cmd/reload.lua +++ b/kong/cmd/reload.lua @@ -17,7 +17,7 @@ local function execute(args) "no such prefix: "..default_conf.prefix) -- load /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)) diff --git a/kong/cmd/stop.lua b/kong/cmd/stop.lua index 96f0fb589b08..47973ea68e94 100644 --- a/kong/cmd/stop.lua +++ b/kong/cmd/stop.lua @@ -16,7 +16,7 @@ local function execute(args) "no such prefix: "..default_conf.prefix) -- load /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)) diff --git a/kong/cmd/utils/prefix_handler.lua b/kong/cmd/utils/prefix_handler.lua index fe7634bf725b..3f4e98ce16c7 100644 --- a/kong/cmd/utils/prefix_handler.lua +++ b/kong/cmd/utils/prefix_handler.lua @@ -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, ",") @@ -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 diff --git a/kong/conf_loader.lua b/kong/conf_loader.lua index 649973a44e8d..103895b08e6e 100644 --- a/kong/conf_loader.lua +++ b/kong/conf_loader.lua @@ -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"}, diff --git a/kong/kong.lua b/kong/kong.lua index 1733cd06434c..120714a33907 100644 --- a/kong/kong.lua +++ b/kong/kong.lua @@ -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 diff --git a/spec/01-unit/02-conf_loader_spec.lua b/spec/01-unit/02-conf_loader_spec.lua index dff8711c223b..bdb43f9c2ea6 100644 --- a/spec/01-unit/02-conf_loader_spec.lua +++ b/spec/01-unit/02-conf_loader_spec.lua @@ -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) diff --git a/spec/01-unit/03-prefix_handler_spec.lua b/spec/01-unit/03-prefix_handler_spec.lua index 2ff5794546b8..993a96a347ec 100644 --- a/spec/01-unit/03-prefix_handler_spec.lua +++ b/spec/01-unit/03-prefix_handler_spec.lua @@ -203,7 +203,7 @@ 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)) @@ -211,7 +211,7 @@ describe("NGINX conf compiler", function() 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() @@ -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() @@ -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) 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 57662b6dc628..b5f409c84f32 100644 --- a/spec/02-integration/01-cmd/02-start_stop_spec.lua +++ b/spec/02-integration/01-cmd/02-start_stop_spec.lua @@ -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() diff --git a/spec/helpers.lua b/spec/helpers.lua index 734d6f409d78..f383d236bce1 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -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