-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: restart + refactor: better utils structure
- Loading branch information
1 parent
d73f250
commit 9a8caf9
Showing
13 changed files
with
261 additions
and
227 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
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
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,14 @@ | ||
#!/usr/bin/env lua | ||
|
||
local constants = require "kong.constants" | ||
local start = require "kong.cli.utils.start" | ||
local stop = require "kong.cli.utils.stop" | ||
local args = require("lapp")(string.format([[ | ||
Usage: kong restart [options] | ||
Options: | ||
-c,--config (default %s) configuration file | ||
]], constants.CLI.GLOBAL_KONG_CONF)) | ||
|
||
stop(args.config) | ||
start(args.config) |
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,53 +1,12 @@ | ||
#!/usr/bin/env lua | ||
|
||
local cutils = require "kong.cli.utils" | ||
local constants = require "kong.constants" | ||
local start = require "kong.cli.utils.start" | ||
local args = require("lapp")(string.format([[ | ||
Usage: kong start [options] | ||
Options: | ||
-c,--config (default %s) configuration file | ||
]], constants.CLI.GLOBAL_KONG_CONF)) | ||
|
||
-- Make sure nginx is there and is openresty | ||
local nginx_path = cutils.find_nginx() | ||
if not nginx_path then | ||
cutils.logger:error_exit("can't find nginx") | ||
end | ||
|
||
-- Get configuration from default or given path | ||
local config_path = cutils.get_kong_config_path(args.config) | ||
local config, dao_factory = cutils.load_configuration_and_dao(config_path) | ||
|
||
-- Migrate the DB if needed and possible | ||
local keyspace, err = dao_factory:get_migrations() | ||
if err then | ||
cutils.logger:error_exit(err) | ||
elseif keyspace == nil then | ||
cutils.logger:log("Database not initialized. Running migrations...") | ||
local migrations = require("kong.tools.migrations")(dao_factory) | ||
migrations:migrate(function(migration, err) | ||
if err then | ||
cutils.logger:error_exit(err) | ||
elseif migration then | ||
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name)) | ||
end | ||
end) | ||
end | ||
|
||
-- Prepare nginx --prefix dir | ||
local nginx_working_dir = cutils.prepare_nginx_working_dir(config) | ||
|
||
-- Build nginx start command | ||
local cmd = string.format("KONG_CONF=%s %s -p %s -c %s -g 'pid %s;'", | ||
config_path, | ||
nginx_path, | ||
nginx_working_dir, | ||
constants.CLI.NGINX_CONFIG, | ||
constants.CLI.NGINX_PID) | ||
|
||
if os.execute(cmd) == 0 then | ||
cutils.logger:success("Started") | ||
else | ||
cutils.logger:error_exit("Could not start Kong") | ||
end | ||
start(args.config) |
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,26 +1,12 @@ | ||
#!/usr/bin/env lua | ||
|
||
local cutils = require "kong.cli.utils" | ||
local constants = require "kong.constants" | ||
local stop = require "kong.cli.utils.stop" | ||
local args = require("lapp")(string.format([[ | ||
Usage: kong stop [options] | ||
Options: | ||
-c,--config (default %s) configuration file | ||
]], constants.CLI.GLOBAL_KONG_CONF)) | ||
|
||
-- Get configuration from default or given path | ||
local config_path = cutils.get_kong_config_path(args.config) | ||
local config = cutils.load_configuration_and_dao(config_path) | ||
|
||
local pid = cutils.path:join(config.nginx_working_dir, constants.CLI.NGINX_PID) | ||
|
||
if not cutils.file_exists(pid) then | ||
cutils.logger:error_exit("Not running. Could not find pid at: "..pid) | ||
end | ||
|
||
local cmd = "kill -QUIT $(cat "..pid..")" | ||
|
||
if os.execute(cmd) == 0 then | ||
cutils.logger:success("Stopped") | ||
end | ||
stop(args.config) |
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,102 @@ | ||
local IO = require "kong.tools.io" | ||
local cutils = require "kong.cli.utils" | ||
local constants = require "kong.constants" | ||
|
||
local _M = {} | ||
|
||
local function is_openresty(path_to_check) | ||
local cmd = tostring(path_to_check).." -v 2>&1" | ||
local handle = io.popen(cmd) | ||
local out = handle:read() | ||
handle:close() | ||
local matched = out:match("^nginx version: ngx_openresty/") or out:match("^nginx version: openresty/") | ||
if matched then | ||
return path_to_check | ||
end | ||
end | ||
|
||
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 = tostring(prefix)..tostring(nginx_bin) | ||
if is_openresty(to_check) then | ||
return to_check | ||
end | ||
end | ||
end | ||
|
||
local function prepare_nginx_working_dir(kong_config) | ||
if kong_config.send_anonymous_reports then | ||
kong_config.nginx = "error_log syslog:server=kong-hf.mashape.com:61828 error;\n"..kong_config.nginx | ||
end | ||
|
||
-- Create nginx folder if needed | ||
local _, err = IO.path:mkdir(IO.path:join(kong_config.nginx_working_dir, "logs")) | ||
if err then | ||
cutils.logger:error_exit(err) | ||
end | ||
os.execute("touch "..IO.path:join(kong_config.nginx_working_dir, "logs", "error.log")) | ||
os.execute("touch "..IO.path:join(kong_config.nginx_working_dir, "logs", "access.log")) | ||
|
||
-- Extract nginx config to nginx folder | ||
IO.write_to_file(IO.path:join(kong_config.nginx_working_dir, constants.CLI.NGINX_CONFIG), kong_config.nginx) | ||
|
||
return kong_config.nginx_working_dir | ||
end | ||
|
||
function _M.start(args_config) | ||
-- 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") | ||
end | ||
|
||
-- Get configuration from default or given path | ||
local config_path = cutils.get_kong_config_path(args_config) | ||
local config, dao_factory = IO.load_configuration_and_dao(config_path) | ||
|
||
-- Migrate the DB if needed and possible | ||
local keyspace, err = dao_factory:get_migrations() | ||
if err then | ||
cutils.logger:error_exit(err) | ||
elseif keyspace == nil then | ||
cutils.logger:log("Database not initialized. Running migrations...") | ||
local migrations = require("kong.tools.migrations")(dao_factory) | ||
migrations:migrate(function(migration, err) | ||
if err then | ||
cutils.logger:error_exit(err) | ||
elseif migration then | ||
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name)) | ||
end | ||
end) | ||
end | ||
|
||
-- Prepare nginx --prefix dir | ||
local nginx_working_dir = prepare_nginx_working_dir(config) | ||
|
||
-- Build nginx start command | ||
local cmd = string.format("KONG_CONF=%s %s -p %s -c %s -g 'pid %s;'", | ||
config_path, | ||
nginx_path, | ||
nginx_working_dir, | ||
constants.CLI.NGINX_CONFIG, | ||
constants.CLI.NGINX_PID) | ||
|
||
if os.execute(cmd) == 0 then | ||
cutils.logger:success("Started") | ||
else | ||
cutils.logger:error_exit("Could not start Kong") | ||
end | ||
|
||
end | ||
|
||
return _M.start |
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,25 @@ | ||
local IO = require "kong.tools.io" | ||
local cutils = require "kong.cli.utils" | ||
local constants = require "kong.constants" | ||
|
||
local _M = {} | ||
|
||
function _M.stop(args_config) | ||
-- Get configuration from default or given path | ||
local config_path = cutils.get_kong_config_path(args_config) | ||
local config = IO.load_configuration_and_dao(config_path) | ||
|
||
local pid = IO.path:join(config.nginx_working_dir, constants.CLI.NGINX_PID) | ||
|
||
if not IO.file_exists(pid) then | ||
cutils.logger:error_exit("Not running. Could not find pid at: "..pid) | ||
end | ||
|
||
local cmd = "kill -QUIT $(cat "..pid..")" | ||
|
||
if os.execute(cmd) == 0 then | ||
cutils.logger:success("Stopped") | ||
end | ||
end | ||
|
||
return _M.stop |
Oops, something went wrong.