Skip to content

Commit

Permalink
feat(conf) store prefix paths in config table
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Jun 28, 2016
1 parent c68a486 commit 8264ae3
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 261 deletions.
2 changes: 1 addition & 1 deletion kong/cmd/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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)
local serf = Serf.new(conf, dao)

if args.command == "members" then
local members = assert(serf:members(true))
Expand Down
22 changes: 11 additions & 11 deletions kong/cmd/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ local pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

local function execute(args)
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
-- retrieve prefix or use given one
local default_conf = assert(conf_loader(nil, {
prefix = args.prefix
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_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))
-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
assert(prefix_handler.prepare_prefix(conf))
assert(dnsmasq_signals.start(conf))
assert(serf_signals.start(conf, DAOFactory(conf)))
assert(nginx_signals.reload(conf))
log("Reloaded")
end

Expand Down
8 changes: 4 additions & 4 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ local function execute(args)

local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(prefix_handler.prepare_prefix(conf, conf.prefix))
assert(prefix_handler.prepare_prefix(conf))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf, conf.prefix))
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, conf.prefix, dao))
assert(nginx_signals.start(conf.prefix))
assert(serf_signals.start(conf, dao))
assert(nginx_signals.start(conf))
log("Started")
end

Expand Down
21 changes: 10 additions & 11 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ local pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

local function execute(args)
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
-- retrieve prefix or use given one
local default_conf = assert(conf_loader(nil, {
prefix = args.prefix
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)

local dao = DAOFactory(conf)
assert(nginx_signals.stop(conf.prefix))
assert(serf_signals.stop(conf, conf.prefix, dao))
-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
assert(nginx_signals.stop(conf))
assert(serf_signals.stop(conf, DAOFactory(conf)))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf.prefix))
assert(dnsmasq_signals.stop(conf))
end
log("Stopped")
end
Expand Down
23 changes: 10 additions & 13 deletions kong/cmd/utils/dnsmasq_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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",
Expand Down Expand Up @@ -46,22 +45,21 @@ local function is_running(pid_path)
return code == 0
end

function _M.start(kong_config, nginx_prefix)
function _M.start(kong_config)
-- 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)
if is_running(kong_config.dnsmasq_pid) then
log.verbose("dnsmasq already running at %s", kong_config.dnsmasq_pid)
return true
else
log.verbose("dnsmasq not running, deleting %s", pid_path)
pl_file.delete(pid_path)
log.verbose("dnsmasq not running, deleting %s", kong_config.dnsmasq_pid)
pl_file.delete(kong_config.dnsmasq_pid)
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)
dnsmasq_bin, kong_config.dnsmasq_port, kong_config.dnsmasq_pid)

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

Expand All @@ -71,11 +69,10 @@ function _M.start(kong_config, nginx_prefix)
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")
function _M.stop(kong_config)
if pl_path.exists(kong_config.dnsmasq_pid) then
log.verbose("stopping dnsmasq at %s", kong_config.dnsmasq_pid)
return kill(kong_config.dnsmasq_pid, "-9")
end
return true
end
Expand Down
33 changes: 12 additions & 21 deletions kong/cmd/utils/nginx_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,12 @@ local function is_openresty(bin_path)
end
end

local function get_pid_path(nginx_prefix)
local pid_path = pl_path.join(nginx_prefix, "pids", "nginx.pid")
if pl_path.exists(pid_path) then
return pid_path
end
end

local function send_signal(nginx_prefix, signal)
local pid_path = get_pid_path(nginx_prefix)
if not pid_path then
local function send_signal(pid_path, signal)
if not pl_path.exists(pid_path) then
return nil, "could not get Nginx pid (is Nginx running in this prefix?)"
end

log.verbose("sending %s signal to nginx running at %s", signal, pid_path)
log.verbose("sending %s signal to Nginx running at %s", signal, pid_path)

local code = kill(pid_path, "-s "..signal)
if code ~= 0 then return nil, "could not send signal" end
Expand Down Expand Up @@ -64,19 +56,18 @@ function _M.find_bin()
return found
end

function _M.start(nginx_prefix)
function _M.start(kong_conf)
local nginx_bin, err = _M.find_bin()
if not nginx_bin then return nil, err end

local pid_path = get_pid_path(nginx_prefix)
if pid_path then
local code = kill(pid_path, "-0")
if pl_path.exists(kong_conf.nginx_pid) then
local code = kill(kong_conf.nginx_pid, "-0")
if code == 0 then
return nil, "Nginx is already running in "..nginx_prefix
return nil, "Nginx is already running in "..kong_conf.prefix
end
end

local cmd = fmt("%s -p %s -c %s", nginx_bin, nginx_prefix, "nginx.conf")
local cmd = fmt("%s -p %s -c %s", nginx_bin, kong_conf.prefix, "nginx.conf")

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

Expand All @@ -86,12 +77,12 @@ function _M.start(nginx_prefix)
return true
end

function _M.stop(nginx_prefix)
return send_signal(nginx_prefix, "QUIT")
function _M.stop(kong_conf)
return send_signal(kong_conf.nginx_pid, "QUIT")
end

function _M.reload(nginx_prefix)
return send_signal(nginx_prefix, "HUP")
function _M.reload(kong_conf)
return send_signal(kong_conf.nginx_pid, "HUP")
end

return _M
71 changes: 24 additions & 47 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ local utils = require "kong.tools.utils"
local ssl = require "kong.cmd.utils.ssl"
local log = require "kong.cmd.utils.log"

local serf_node_id = "serf.id"

-- script from old services.serf module
local script_template = [[
#!/bin/sh
Expand Down Expand Up @@ -100,75 +98,54 @@ local function touch(file_path)
return pl_utils.executeex("touch "..file_path)
end

local function prepare_prefix(kong_config, nginx_prefix)
log.verbose("preparing nginx prefix directory at %s", nginx_prefix)
local function prepare_prefix(kong_config)
log.verbose("preparing nginx prefix directory at %s", kong_config.prefix)

if not pl_path.exists(nginx_prefix) then
log("prefix directory %s not found, trying to create it", nginx_prefix)
local ok, err = pl_dir.makepath(nginx_prefix)
if not pl_path.exists(kong_config.prefix) then
log("prefix directory %s not found, trying to create it", kong_config.prefix)
local ok, err = pl_dir.makepath(kong_config.prefix)
if not ok then return nil, err end
elseif not pl_path.isdir(nginx_prefix) then
return nil, nginx_prefix.." is not a directory"
elseif not pl_path.isdir(kong_config.prefix) then
return nil, kong_config.prefix.." is not a directory"
end

-- create log dir in case
local logs_path = pl_path.join(nginx_prefix, "logs")
local ok, err = pl_dir.makepath(logs_path)
if not ok then return nil, err end
-- create directories in prefix
for _, dir in ipairs {"logs", "serf", "pids"} do
local ok, err = pl_dir.makepath(pl_path.join(kong_config.prefix, dir))
if not ok then return nil, err end
end

-- create log files in case
local err_logs_path = pl_path.join(logs_path, "error.log")
local acc_logs_path = pl_path.join(logs_path, "access.log")

local ok, _, _, stderr = touch(err_logs_path)
local ok, _, _, stderr = touch(kong_config.nginx_err_logs)
if not ok then return nil, stderr end
local ok, _, _, stderr = touch(acc_logs_path)
local ok, _, _, stderr = touch(kong_config.nginx_acc_logs)
if not ok then return nil, stderr end

-- pids folder
local pids_path = pl_path.join(nginx_prefix, "pids")
local ok, err = pl_dir.makepath(pids_path)
if not ok then return nil, err end

-- serf folder (node identifier + shell script)
local serf_path = pl_path.join(nginx_prefix, "serf")
local ok, err = pl_dir.makepath(serf_path)
if not ok then return nil, err end

local id_path = pl_path.join(nginx_prefix, "serf", serf_node_id)
log.verbose("saving Serf identifier in %s", id_path)
if not pl_path.exists(id_path) then
log.verbose("saving Serf identifier in %s", kong_config.serf_node_id)
if not pl_path.exists(kong_config.serf_node_id) then
local id = utils.get_hostname().."_"..kong_config.cluster_listen.."_"..utils.random_string()
pl_file.write(id_path, id)
pl_file.write(kong_config.serf_node_id, id)
end

local script_path = pl_path.join(nginx_prefix, "serf", "serf_event.sh")
log.verbose("saving Serf shell script handler in %s", script_path)
log.verbose("saving Serf shell script handler in %s", kong_config.serf_event)
local script = string.format(script_template, "127.0.0.1", kong_config.admin_port)

pl_file.write(script_path, script)

local ok, _, _, stderr = pl_utils.executeex("chmod +x "..script_path)
pl_file.write(kong_config.serf_event, script)
local ok, _, _, stderr = pl_utils.executeex("chmod +x "..kong_config.serf_event)
if not ok then return nil, stderr end

-- auto-generate default SSL certificate
local ok, err = ssl.prepare_ssl_cert_and_key(nginx_prefix)
local ok, err = ssl.prepare_ssl_cert_and_key(kong_config.prefix)
if not ok then return nil, err end

local kong_conf_path = pl_path.join(nginx_prefix, "kong.conf")
local nginx_conf_path = pl_path.join(nginx_prefix, "nginx.conf")
local kong_nginx_conf_path = pl_path.join(nginx_prefix, "nginx-kong.conf")

-- write NGINX conf
local nginx_conf, err = compile_nginx_conf(kong_config)
if not nginx_conf then return nil, err end

pl_file.write(nginx_conf_path, nginx_conf)
pl_file.write(kong_config.nginx_conf, nginx_conf)

-- write Kong's NGINX conf
local kong_nginx_conf, err = compile_kong_conf(kong_config)
if not kong_nginx_conf then return nil, err end
pl_file.write(kong_nginx_conf_path, kong_nginx_conf)
pl_file.write(kong_config.nginx_kong_conf, kong_nginx_conf)

-- write kong.conf in prefix (for workers and CLI)
local buf = {}
Expand All @@ -180,7 +157,7 @@ local function prepare_prefix(kong_config, nginx_prefix)
buf[#buf+1] = k.." = "..tostring(v)
end
end
pl_file.write(kong_conf_path, table.concat(buf, "\n"))
pl_file.write(kong_config.kong_conf, table.concat(buf, "\n"))

return true
end
Expand Down
Loading

0 comments on commit 8264ae3

Please sign in to comment.