Skip to content

Commit

Permalink
feature: restart + refactor: better utils structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Mar 25, 2015
1 parent d73f250 commit 9a8caf9
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 227 deletions.
5 changes: 3 additions & 2 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ local commands = {
stop = "kong.cli.stop",
start = "kong.cli.start",
config = "kong.cli.config",
version = "kong.cli.version"
version = "kong.cli.version",
restart = "kong.cli.restart"
}

local help_message = string.format([[
Usage: kong <command>
where <command> is one of:
start, stop, config, db, version
start, stop, restart, config, db, version
kong --help print this message
kong <command> --help print the help message of a command
Expand Down
6 changes: 5 additions & 1 deletion kong-0.1.0beta-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ build = {

["kong.constants"] = "src/constants.lua",

["kong.cli.utils"] = "src/cli/utils.lua",
["kong.cli.utils"] = "src/cli/utils/utils.lua",
["kong.cli.utils.start"] = "src/cli/utils/start.lua",
["kong.cli.utils.stop"] = "src/cli/utils/stop.lua",
["kong.cli.db"] = "src/cli/db.lua",
["kong.cli.config"] = "src/cli/config.lua",
["kong.cli.stop"] = "src/cli/stop.lua",
["kong.cli.start"] = "src/cli/start.lua",
["kong.cli.restart"] = "src/cli/restart.lua",
["kong.cli.version"] = "src/cli/version.lua",

["kong.tools.utils"] = "src/tools/utils.lua",
["kong.tools.io"] = "src/tools/io.lua",
["kong.tools.migrations"] = "src/tools/migrations.lua",
["kong.tools.faker"] = "src/tools/faker.lua",
["kong.tools.cache"] = "src/tools/cache.lua",
Expand Down
7 changes: 4 additions & 3 deletions src/cli/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env lua

local cutils = require "kong.cli.utils"
local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local args = require("lapp")(string.format([[
Duplicate an existing configuration for given environment.
Expand All @@ -16,7 +17,7 @@ Options:
local CONFIG_FILENAME = string.format("kong%s.yml", args.env ~= "" and "_"..args.env or "")

local config_path = cutils.get_kong_config_path(args.config)
local config_content = cutils.read_file(config_path)
local config_content = IO.read_file(config_path)

local DEFAULT_ENV_VALUES = {
TEST = {
Expand Down Expand Up @@ -47,4 +48,4 @@ if DEFAULT_ENV_VALUES[args.env:upper()] then
end
end

cutils.write_to_file(cutils.path:join(args.output, CONFIG_FILENAME), config_content)
IO.write_to_file(IO.path:join(args.output, CONFIG_FILENAME), config_content)
3 changes: 2 additions & 1 deletion src/cli/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local Migrations = require "kong.tools.migrations"

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local args = lapp(string.format([[
Migrations, seeding of the DB.
Expand All @@ -27,7 +28,7 @@ if args.command == "db" then
end

local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = cutils.load_configuration_and_dao(config_path)
local _, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory, cutils.get_luarocks_install_dir())

if args.command == "migrations" then
Expand Down
14 changes: 14 additions & 0 deletions src/cli/restart.lua
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)
45 changes: 2 additions & 43 deletions src/cli/start.lua
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)
18 changes: 2 additions & 16 deletions src/cli/stop.lua
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)
102 changes: 102 additions & 0 deletions src/cli/utils/start.lua
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
25 changes: 25 additions & 0 deletions src/cli/utils/stop.lua
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
Loading

0 comments on commit 9a8caf9

Please sign in to comment.