Skip to content

Commit

Permalink
fix #124: nicer error msg if can't find nginx
Browse files Browse the repository at this point in the history
+ annotated bin/kong
  • Loading branch information
thibaultcha committed Apr 9, 2015
1 parent d47d07d commit 9831700
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
20 changes: 15 additions & 5 deletions bin/kong
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/usr/bin/env lua

-- Kong CLI entry-point (bin/kong).
--
-- Kong's CLI is a set of small commands invoked by a global executable, this file.
--
-- All commands are invoked by this script, then parsed (arguments and options)
-- by lapp (see http://lua-users.org/wiki/LappFramework).
--
-- This script is not parsed by lapp due to limitations of the said framework as it
-- is currently implemented.

local cutils = require "kong.cli.utils"
local infos = cutils.get_kong_infos()
local commands = {
Expand All @@ -18,16 +28,16 @@ local commands = {
local help_message = string.format([[
Usage: kong <command>
where <command> is one of:
start, restart, reload, stop, quit, version
where <command> is one of:
start, restart, reload, stop, quit, version
kong --help print this message
kong <command> --help print the help message of a command
kong --help print this message
kong <command> --help print the help message of a command
%s@%s]], infos.name, infos.version)

-- Determine validity of the given command
local cmd = arg[1]

if not cmd then
print("Missing <command>\n\n"..help_message)
os.exit(1)
Expand Down
28 changes: 14 additions & 14 deletions src/cli/utils/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ local function is_openresty(path_to_check)
return out:match("^nginx version: ngx_openresty/") or out:match("^nginx version: openresty/")
end

-- Find an `nginx` executable in defined paths
-- Paths where to search for an `nginx` executable in addition to the usual $PATH
local NGINX_BIN = "nginx"
local NGINX_SEARCH_PATHS = {
"/usr/local/openresty/nginx/sbin/",
"/usr/local/opt/openresty/bin/",
"/usr/local/bin/",
"/usr/sbin/"
}

-- Try to find an `nginx` executable in defined paths, or in $PATH
-- @return Path to found executable or nil if none was found
local function find_nginx()
local nginx_bin = "nginx"
local nginx_search_paths = {
"/usr/local/openresty/nginx/sbin/",
"/usr/local/opt/openresty/bin/",
"/usr/local/bin/",
"/usr/sbin/",
""
}

for i = 1, #nginx_search_paths do
local prefix = nginx_search_paths[i]
local to_check = prefix..nginx_bin
for i = 1, #NGINX_SEARCH_PATHS + 1 do
local prefix = NGINX_SEARCH_PATHS[i] and NGINX_SEARCH_PATHS[i] or ""
local to_check = prefix..NGINX_BIN
if is_openresty(to_check) then
return to_check
end
Expand Down Expand Up @@ -133,7 +133,7 @@ function _M.send_signal(args_config, signal)
-- Make sure nginx is there and is openresty
local nginx_path = find_nginx()
if not nginx_path then
cutils.logger:error_exit("can't find nginx")
cutils.logger:error_exit(string.format("Kong cannot find an 'nginx' executable.\nMake sure it is in your $PATH or in one of the following directories:\n%s", table.concat(NGINX_SEARCH_PATHS, "\n")))
end

local kong_config_path, kong_config = get_kong_config_path(args_config)
Expand Down
14 changes: 9 additions & 5 deletions src/cli/utils/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ function Logger:log(str)
end

function Logger:success(str)
self:log(colors.green("[SUCCESS] ")..str)
self:log(colors.green("[OK] ")..str)
end

function Logger:warn(str)
self:log(colors.yellow("[WARNING] ")..str)
self:log(colors.yellow("[WARN] ")..str)
end

function Logger:error(str)
self:log(colors.red("[ERROR] ")..str)
self:log(colors.red("[ERR] ")..str)
end

function Logger:error_exit(str)
print("")
self:error(str)
-- Optional stacktrace
--print("")
--error("", 2)
os.exit(1)
end

Expand Down Expand Up @@ -103,15 +107,15 @@ local function get_kong_config_path(args_config)
-- Make sure the configuration file really exists
if not IO.file_exists(args_config) then
logger:warn("No config at: "..args_config)
logger:error_exit("Could not find a path to configuration file.")
logger:error_exit("Could not find a configuration file.")
end

logger:log("Using config: "..args_config)

-- TODO: validate configuration
--[[local status, res = pcall(require, "kong.dao."..config.database..".factory")
if not status then
logger:error("Wrong config")
logger:error("Erroneous config")
os.exit(1)
end]]

Expand Down
10 changes: 5 additions & 5 deletions src/vendor/lapp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function quit(msg,no_usage)
end

local function error(msg,no_usage)
quit(arg[0]:gsub('.+[\\/]','')..':'..msg,no_usage)
quit(msg,no_usage)
end

local function ltrim(line)
Expand All @@ -68,7 +68,7 @@ local function trim(s)
return ltrim(rtrim(s))
end

local function open (file,opt)
local function open(file,opt)
local val,err = io.open(file,opt)
if not val then error(err,true) end
append(open_files,val)
Expand Down Expand Up @@ -122,7 +122,7 @@ local function force_short(short)
xassert(#short==1,short..": short parameters should be one character")
end

function process_options_string(str)
function process_options_string(str, main_command)
local res = {}
local varargs

Expand Down Expand Up @@ -297,8 +297,8 @@ function process_options_string(str)
-- check unused parms, set defaults and check if any required parameters were missed
for parm,ps in pairs(parms) do
if not ps.used then
if ps.required then error("missing required parameter: "..parm) end
set_result(ps,parm,ps.defval)
if ps.required then error("Missing required parameter: "..parm) end
set_result(parm,ps.defval)
end
end
return res
Expand Down

0 comments on commit 9831700

Please sign in to comment.