Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli) kong prepare cli command to prepare prefix #2706

Merged
merged 3 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kong-0.10.3-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ build = {
["kong.cmd.reload"] = "kong/cmd/reload.lua",
["kong.cmd.restart"] = "kong/cmd/restart.lua",
["kong.cmd.compile"] = "kong/cmd/compile.lua",
["kong.cmd.prepare"] = "kong/cmd/prepare.lua",
["kong.cmd.migrations"] = "kong/cmd/migrations.lua",
["kong.cmd.health"] = "kong/cmd/health.lua",
["kong.cmd.version"] = "kong/cmd/version.lua",
Expand Down
5 changes: 5 additions & 0 deletions kong/cmd/compile.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local conf_loader = require "kong.conf_loader"
local log = require "kong.cmd.utils.log"

local function execute(args)
log.warn("'kong compile' is deprecated, use 'kong prepare' instead")

local conf = assert(conf_loader(args.conf))
local kong_nginx_conf = assert(prefix_handler.compile_kong_conf(conf))
print(kong_nginx_conf)
Expand All @@ -10,6 +13,8 @@ end
local lapp = [[
Usage: kong compile [OPTIONS]

[DEPRECATED] This command is deprecated. Use 'kong prepare' instead.

Compile the Nginx configuration file containing Kong's servers
contexts from a given Kong configuration file.

Expand Down
1 change: 1 addition & 0 deletions kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local cmds = {
health = true,
check = true,
compile = true,
prepare = true,
migrations = true,
version = true,
roar = true
Expand Down
39 changes: 39 additions & 0 deletions kong/cmd/prepare.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local conf_loader = require "kong.conf_loader"


local function execute(args)
local conf = assert(conf_loader(args.conf, {
prefix = args.prefix
}))

local ok, err = prefix_handler.prepare_prefix(conf, args.nginx_conf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are missing this nginx-conf argument from the command's options?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in last commit.

if not ok then
error("could not prepare Kong prefix at " .. conf.prefix .. ": " .. err)
end
end


local lapp = [[
Usage: kong prepare [OPTIONS]

Prepare the Kong prefix in the configured prefix directory. This command can
be used to start Kong from the nginx binary without using the 'kong start'
command.

Example usage:
kong migrations up
kong prepare -p /usr/local/kong -c kong.conf
nginx -p /usr/local/kong -c /usr/local/kong/nginx.conf

Options:
-c,--conf (optional string) configuration file
-p,--prefix (optional string) override prefix directory
--nginx-conf (optional string) custom Nginx configuration template
]]


return {
lapp = lapp,
execute = execute,
}
2 changes: 1 addition & 1 deletion kong/cmd/utils/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ local function log(lvl, ...)
msg = string.format("%s [%s] %s", os.date("%Y/%m/%d %H:%M:%S"), r_levels[lvl], msg)
end

if lvl < _LEVELS.error then
if lvl < _LEVELS.warn then
print(msg)
else
io.stderr:write(msg .. "\n")
Expand Down
49 changes: 49 additions & 0 deletions spec/02-integration/02-cmd/10-prepare_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local helpers = require "spec.helpers"


local TEST_PREFIX = "servroot_prepared_test"


describe("kong prepare", function()
setup(function()
pcall(helpers.dir.rmtree, TEST_PREFIX)
end)

after_each(function()
pcall(helpers.dir.rmtree, TEST_PREFIX)
end)

it("prepares a prefix", function()
assert(helpers.kong_exec("prepare -c " .. helpers.test_conf_path, {
prefix = TEST_PREFIX
}))
assert.truthy(helpers.path.exists(TEST_PREFIX))

local admin_access_log_path = helpers.path.join(TEST_PREFIX, helpers.test_conf.admin_access_log)
local admin_error_log_path = helpers.path.join(TEST_PREFIX, helpers.test_conf.admin_error_log)

assert.truthy(helpers.path.exists(admin_access_log_path))
assert.truthy(helpers.path.exists(admin_error_log_path))
end)

it("prepares a prefix from CLI arg option", function()
assert(helpers.kong_exec("prepare -c " .. helpers.test_conf_path ..
" -p " .. TEST_PREFIX))
assert.truthy(helpers.path.exists(TEST_PREFIX))

local admin_access_log_path = helpers.path.join(TEST_PREFIX, helpers.test_conf.admin_access_log)
local admin_error_log_path = helpers.path.join(TEST_PREFIX, helpers.test_conf.admin_error_log)

assert.truthy(helpers.path.exists(admin_access_log_path))
assert.truthy(helpers.path.exists(admin_error_log_path))
end)

describe("errors", function()
it("on inexistent Kong conf file", function()
local ok, stderr = helpers.kong_exec "prepare --conf foobar.conf"
assert.False(ok)
assert.is_string(stderr)
assert.matches("Error: no file at: foobar.conf", stderr, nil, true)
end)
end)
end)