Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli) start/stop with Serf service refactor
- Loading branch information
1 parent
ed83280
commit 2500c8e
Showing
23 changed files
with
653 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
.project | ||
.idea | ||
|
||
servroot* | ||
|
||
# kong | ||
nginx_tmp/ | ||
kong*.yml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,3 @@ | ||
#!/usr/bin/env luajit | ||
#!/usr/bin/env resty | ||
|
||
-- 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 meta = require "kong.meta" | ||
local commands = { | ||
db = "kong.cli.cmds.db", | ||
stop = "kong.cli.cmds.stop", | ||
quit = "kong.cli.cmds.quit", | ||
start = "kong.cli.cmds.start", | ||
reload = "kong.cli.cmds.reload", | ||
config = "kong.cli.cmds.config", | ||
restart = "kong.cli.cmds.restart", | ||
version = "kong.cli.cmds.version", | ||
status = "kong.cli.cmds.status", | ||
migrations = "kong.cli.cmds.migrations", | ||
cluster = "kong.cli.cmds.cluster", | ||
["--version"] = "kong.cli.cmds.version" | ||
} | ||
|
||
local help_message = string.format([[ | ||
Usage: kong <command> | ||
|
||
where <command> is one of: | ||
start, restart, reload, stop, quit, cluster, status, migrations, version | ||
|
||
kong --help print this message | ||
kong <command> --help print the help message of a command | ||
|
||
%s@%s]], meta._NAME, meta._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) | ||
elseif cmd == "-h" or cmd == "--help" then | ||
print(help_message) | ||
os.exit(0) | ||
elseif not commands[cmd] then | ||
print("Invalid <command>: "..cmd.."\n\n"..help_message) | ||
os.exit(1) | ||
end | ||
|
||
-- Load and execute desired command | ||
require(commands[cmd]) | ||
require("kong.cmd.init")(arg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
local pl_app = require "pl.lapp" | ||
local help = [[ | ||
Kong, open-source API gateway. | ||
Usage: kong COMMAND [OPTIONS] | ||
The available commands are: | ||
start | ||
stop | ||
Options: | ||
--trace (optional boolean) with traceback | ||
]] | ||
|
||
local DEFAULT_NGINX_PREFIX = "servroot" | ||
|
||
local cmds = { | ||
start = "start", | ||
stop = "stop", | ||
--reload = "reload", | ||
--migrate = "migrate", | ||
--reset = "reset" | ||
} | ||
|
||
return function(args) | ||
local cmd_name = args[1] | ||
if cmd_name == nil then | ||
pl_app(help) | ||
pl_app.quit() | ||
elseif not cmds[cmd_name] then | ||
pl_app(help) | ||
pl_app.quit("No such command: "..cmd_name) | ||
end | ||
|
||
local cmd = require("kong.cmd."..cmd_name) | ||
local cmd_lapp = cmd.lapp | ||
local cmd_exec = cmd.execute | ||
|
||
cmd_lapp = cmd_lapp.."\n --trace (optional boolean) with traceback\n" | ||
args = pl_app(cmd_lapp) | ||
args.prefix = args.prefix or DEFAULT_NGINX_PREFIX | ||
|
||
xpcall(function() cmd_exec(args) end, function(err) | ||
if not args.trace then | ||
err = err:match "^.-:.-:.(.*)$" | ||
io.stderr:write("Error: "..err.."\n") | ||
io.stderr:write("\n Run with --trace to see traceback\n") | ||
else | ||
local trace = debug.traceback(err, 2) | ||
io.stderr:write("Error: \n") | ||
io.stderr:write(trace.."\n") | ||
end | ||
|
||
pl_app.quit(nil, true) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler" | ||
local nginx_signals = require "kong.cmd.utils.nginx_signals" | ||
local serf_signals = require "kong.cmd.utils.serf_signals" | ||
local conf_loader = require "kong.conf_loader" | ||
local DAOFactory = require "kong.dao.factory" | ||
|
||
local function execute(args) | ||
local conf = assert(conf_loader(args.conf)) | ||
assert(nginx_conf_compiler.prepare_prefix(conf, args.prefix)) | ||
assert(serf_signals.start(conf, args.prefix, DAOFactory(conf))) | ||
assert(nginx_signals.start(args.prefix)) | ||
print("Started") | ||
end | ||
|
||
local lapp = [[ | ||
Usage: kong start [OPTIONS] | ||
Options: | ||
-c,--conf (optional string) configuration file | ||
--prefix (optional string) Nginx prefix path | ||
]] | ||
|
||
return { | ||
lapp = lapp, | ||
execute = execute | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local nginx_signals = require "kong.cmd.utils.nginx_signals" | ||
local serf_signals = require "kong.cmd.utils.serf_signals" | ||
|
||
local function execute(args) | ||
assert(nginx_signals.stop(args.prefix)) | ||
assert(serf_signals.stop(args.prefix)) | ||
print("Stopped") | ||
end | ||
|
||
local lapp = [[ | ||
Usage: kong stop [OPTIONS] | ||
Options: | ||
--prefix (optional string) Nginx prefix path | ||
]] | ||
|
||
return { | ||
lapp = lapp, | ||
execute = execute | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
local pl_utils = require "pl.utils" | ||
local pl_path = require "pl.path" | ||
local pl_file = require "pl.file" | ||
local fmt = string.format | ||
|
||
local nginx_bin_name = "nginx" | ||
local nginx_search_paths = { | ||
"/usr/local/openresty/nginx/sbin", | ||
"" | ||
} | ||
|
||
local function is_openresty(bin_path) | ||
local cmd = fmt("%s -v", bin_path) | ||
local ok, _, _, v_str = pl_utils.executeex(cmd) | ||
if ok and v_str then | ||
return v_str:match "^nginx version: ngx_openresty/" or | ||
v_str:match "^nginx version: openresty/" | ||
end | ||
end | ||
|
||
local function get_pid(nginx_prefix) | ||
local pid_path = pl_path.join(nginx_prefix, "logs", "nginx.pid") | ||
if pl_path.exists(pid_path) then | ||
return pl_file.read(pid_path) | ||
end | ||
end | ||
|
||
local function send_signal(nginx_prefix, signal) | ||
local pid = get_pid(nginx_prefix) | ||
if not pid then return nil, "could not get Nginx pid (is Kong running?)" end | ||
|
||
local cmd = fmt("kill -s %s %s", signal, pid) | ||
|
||
local ok = pl_utils.execute(cmd) | ||
if not ok then return nil, "could not send signal" end | ||
|
||
return true | ||
end | ||
|
||
local _M = {} | ||
|
||
function _M.find_bin() | ||
local found | ||
for _, path in ipairs(nginx_search_paths) do | ||
local path_to_check = pl_path.join(path, nginx_bin_name) | ||
local ok = is_openresty(path_to_check) | ||
if ok then | ||
found = path_to_check | ||
break | ||
end | ||
end | ||
|
||
if not found then | ||
return nil, "could not find OpenResty 'nginx' executable" | ||
end | ||
|
||
return found | ||
end | ||
|
||
function _M.start(nginx_prefix) | ||
local nginx_bin, err = _M.find_bin() | ||
if not nginx_bin then return nil, err end | ||
|
||
local cmd = fmt("%s -p %s -c %s", nginx_bin, nginx_prefix, "nginx.conf") | ||
|
||
local ok, _, _, stderr = pl_utils.executeex(cmd) | ||
if not ok then return nil, stderr end | ||
|
||
return true | ||
end | ||
|
||
function _M.stop(nginx_prefix) | ||
return send_signal(nginx_prefix, "QUIT") | ||
end | ||
|
||
function _M.reload(nginx_prefix) | ||
return send_signal(nginx_prefix, "HUP") | ||
end | ||
|
||
return _M |
Oops, something went wrong.