Skip to content

Commit

Permalink
Merge branch 'refactor/ssl-autogen' into refactor/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Jun 27, 2016
2 parents 4ff9353 + 4eb34b0 commit c68a486
Show file tree
Hide file tree
Showing 70 changed files with 6,710 additions and 3,983 deletions.
21 changes: 12 additions & 9 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,29 @@ build = {
["kong.singletons"] = "kong/singletons.lua",
["kong.conf_loader"] = "kong/conf_loader.lua",

["kong.templates.kong_defaults"] = "kong/templates/kong_defaults.lua",
["kong.templates.nginx"] = "kong/templates/nginx.lua",
["kong.templates.nginx_kong"] = "kong/templates/nginx_kong.lua",
["kong.templates.kong_defaults"] = "kong/templates/kong_defaults.lua",

["kong.vendor.classic"] = "kong/vendor/classic.lua",

["kong.cmd.roar"] = "kong/cmd/roar.lua",
["kong.cmd.init"] = "kong/cmd/init.lua",
["kong.cmd.stop"] = "kong/cmd/stop.lua",
["kong.cmd.start"] = "kong/cmd/start.lua",
["kong.cmd.check"] = "kong/cmd/check.lua",
["kong.cmd.reload"] = "kong/cmd/reload.lua",
["kong.cmd.cluster"] = "kong/cmd/cluster.lua",
["kong.cmd.compile"] = "kong/cmd/compile.lua",
["kong.cmd.init"] = "kong/cmd/init.lua",
["kong.cmd.migrations"] = "kong/cmd/migrations.lua",
["kong.cmd.reload"] = "kong/cmd/reload.lua",
["kong.cmd.roar"] = "kong/cmd/roar.lua",
["kong.cmd.start"] = "kong/cmd/start.lua",
["kong.cmd.stop"] = "kong/cmd/stop.lua",
["kong.cmd.version"] = "kong/cmd/version.lua",
["kong.cmd.utils.kill"] = "kong/cmd/utils/kill.lua",
["kong.cmd.utils.log"] = "kong/cmd/utils/log.lua",
["kong.cmd.utils.nginx_conf_compiler"] = "kong/cmd/utils/nginx_conf_compiler.lua",
["kong.cmd.utils.nginx_signals"] = "kong/cmd/utils/nginx_signals.lua",
["kong.cmd.utils.ssl"] = "kong/cmd/utils/ssl.lua",
["kong.cmd.utils.kill"] = "kong/cmd/utils/kill.lua",
["kong.cmd.utils.serf_signals"] = "kong/cmd/utils/serf_signals.lua",
["kong.cmd.utils.nginx_signals"] = "kong/cmd/utils/nginx_signals.lua",
["kong.cmd.utils.prefix_handler"] = "kong/cmd/utils/prefix_handler.lua",
["kong.cmd.utils.dnsmasq_signals"] = "kong/cmd/utils/dnsmasq_signals.lua",

["kong.api.init"] = "kong/api/init.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
Expand Down
51 changes: 51 additions & 0 deletions kong/cmd/cluster.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
local Serf = require "kong.serf"
local log = require "kong.cmd.utils.log"

local function execute(args)
local conf = assert(conf_loader(args.conf))
local dao = DAOFactory(conf)
local serf = Serf.new(conf, conf.prefix, dao)

if args.command == "members" then
local members = assert(serf:members(true))
for _, v in ipairs(members) do
print(string.format("%s\t%s\t%s", v.name, v.addr, v.status))
end
elseif args.command == "keygen" then
print(assert(serf:keygen()))
elseif args.command == "reachability" then
print(assert(serf:reachability()))
elseif args.command == "force-leave" then
local node_name = args[1]
assert(node_name ~= nil, "must specify the name of the node to leave")
log("force-leaving %s", node_name)
assert(serf:force_leave(node_name))
log("left node %s", node_name)
end
end

local lapp = [[
Usage: kong cluster COMMAND [OPTIONS]
The available commands are:
members
force-leave <node_name>
keygen
reachability
Options:
-c,--conf (optional string) configuration file
]]

return {
lapp = lapp,
execute = execute,
sub_commands = {
keygen = true,
members = true,
reachability = true,
["force-leave"] = true
}
}
4 changes: 2 additions & 2 deletions kong/cmd/compile.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
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))
local kong_nginx_conf = assert(nginx_conf_compiler.compile_kong_conf(conf))
local kong_nginx_conf = assert(prefix_handler.compile_kong_conf(conf))
print(kong_nginx_conf)
end

Expand Down
4 changes: 3 additions & 1 deletion kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ The available commands are:
stop
reload
check
migrations
compile
migrations
cluster
version
Options:
Expand All @@ -29,6 +30,7 @@ local cmds = {
check = "check",
compile = "compile",
migrations = "migrations",
cluster = "cluster",
version = "version",
roar = "roar"
}
Expand Down
19 changes: 13 additions & 6 deletions kong/cmd/reload.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
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 pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

local function execute(args)
local conf = assert(conf_loader(args.conf, {
prefix = args.prefix
local default_conf = assert(conf_loader()) -- just retrieve default prefix
local prefix = args.prefix or default_conf.prefix
assert(pl_path.exists(prefix), "no such prefix: "..prefix)

local conf_path = pl_path.join(prefix, "kong.conf")
local conf = assert(conf_loader(conf_path, {
prefix = prefix
}))

assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(prefix_handler.prepare_prefix(conf, conf.prefix))
assert(dnsmasq_signals.start(conf, conf.prefix))
assert(serf_signals.start(conf, conf.prefix, DAOFactory(conf)))
assert(nginx_signals.reload(conf.prefix))
log("Reloaded")
Expand All @@ -20,8 +28,7 @@ local lapp = [[
Usage: kong reload [OPTIONS]
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) Nginx prefix path
--prefix (optional string) prefix Kong is running at
]]

return {
Expand Down
17 changes: 7 additions & 10 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
local nginx_conf_compiler = require "kong.cmd.utils.nginx_conf_compiler"
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
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 log = require "kong.cmd.utils.log"

--[[
Start Kong.
Kong being a bundle of several applications and services, start acts
as follows:
--]]

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

local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(prefix_handler.prepare_prefix(conf, conf.prefix))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf, conf.prefix))
end
assert(serf_signals.start(conf, conf.prefix, dao))
assert(nginx_signals.start(conf.prefix))
log("Started")
Expand All @@ -30,7 +27,7 @@ Usage: kong start [OPTIONS]
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) Nginx prefix path
--prefix (optional string) override prefix directory
]]

return {
Expand Down
22 changes: 16 additions & 6 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
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 pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

local function execute(args)
-- no conf file loaded, we just want the prefix,
-- potentially overriden by the argument
local conf = assert(conf_loader(nil, {
prefix = args.prefix
local default_conf = assert(conf_loader()) -- just retrieve default prefix
local prefix = args.prefix or default_conf.prefix
assert(pl_path.exists(prefix), "no such prefix: "..prefix)

local conf_path = pl_path.join(prefix, "kong.conf")
local conf = assert(conf_loader(conf_path, {
prefix = prefix
}))

local dao = DAOFactory(conf)
assert(nginx_signals.stop(conf.prefix))
assert(serf_signals.stop(conf.prefix))
assert(serf_signals.stop(conf, conf.prefix, dao))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf.prefix))
end
log("Stopped")
end

local lapp = [[
Usage: kong stop [OPTIONS]
Options:
--prefix (optional string) Nginx prefix path
--prefix (optional string) prefix Kong is running at
]]

return {
Expand Down
83 changes: 83 additions & 0 deletions kong/cmd/utils/dnsmasq_signals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local kill = require "kong.cmd.utils.kill"
local log = require "kong.cmd.utils.log"
local fmt = string.format

local _M = {}

local dnsmasq_bin_name = "dnsmasq"
local dnsmasq_pid_name = "dnsmasq.pid"
local dnsmasq_search_paths = {
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/bin",
""
}

function _M.find_bin()
log.verbose("searching for 'dnsmasq' executable...")

local found
for _, path in ipairs(dnsmasq_search_paths) do
local path_to_check = pl_path.join(path, dnsmasq_bin_name)
local cmd = fmt("%s -v", path_to_check)
if pl_utils.executeex(cmd) then
found = path_to_check
break
end
end

if not found then
return nil, "could not find 'dnsmasq' executable"
end

log.verbose("found 'dnsmasq' executable at %s", found)

return found
end

local function is_running(pid_path)
if not pl_path.exists(pid_path) then return nil end
local code = kill(pid_path, "-0")
return code == 0
end

function _M.start(kong_config, nginx_prefix)
-- is dnsmasq already running in this prefix?
local pid_path = pl_path.join(nginx_prefix, "pids", dnsmasq_pid_name)
if is_running(pid_path) then
log.verbose("dnsmasq already running at %s", pid_path)
return true
else
log.verbose("dnsmasq not running, deleting %s", pid_path)
pl_file.delete(pid_path)
end

local dnsmasq_bin, err = _M.find_bin()
if not dnsmasq_bin then return nil, err end

local cmd = fmt("%s -p %d --pid-file=%s -N -o --listen-address=127.0.0.1",
dnsmasq_bin, kong_config.dnsmasq_port, pid_path)

log.debug("starting dnsmasq: %s", cmd)

local ok, _, _, stderr = pl_utils.executeex(cmd)
if not ok then return nil, stderr end

return true
end

function _M.stop(nginx_prefix)
local pid_path = pl_path.join(nginx_prefix, "pids", dnsmasq_pid_name)
if pl_path.exists(pid_path) then
log.verbose("stopping dnsmasq at %s", pid_path)
return kill(pid_path, "-9")
end
return true
end

return _M
Loading

0 comments on commit c68a486

Please sign in to comment.