Skip to content

Commit

Permalink
fix(cli) correct support for prefix/conf in CLI and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Jun 22, 2016
1 parent 9702ede commit 12e91ad
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 342 deletions.
22 changes: 11 additions & 11 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +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.cluster"] = "kong/cmd/cluster.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.cmd.utils.ssl"] = "kong/cmd/utils/ssl.lua",

["kong.api.init"] = "kong/api/init.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
Expand Down
62 changes: 31 additions & 31 deletions kong.conf.default
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Kong configuration file.
#
# All commented values are default values. Uncomment and update a property to
# All commented values are default values. Uncomment and update a property to
# configure it.

# The Kong working directory. The directory will contain Kong process files and
Expand All @@ -12,26 +12,26 @@
# This section determines the network settings for Kong. By default Kong listens
# for connections from all the network interfaces available on the server.

# Address and port on which the server will accept HTTP requests, consumers will
# Address and port on which the server will accept HTTP requests, consumers will
# make requests on this port.
# proxy_listen = 0.0.0.0:8000

# Same as proxy_listen, but for HTTPS requests.
# proxy_listen_ssl = 0.0.0.0:8443

# Address and port on which the admin API will listen to. The admin API is a
# private API which lets you manage your Kong infrastructure. It needs to be
# Address and port on which the admin API will listen to. The admin API is a
# private API which lets you manage your Kong infrastructure. It needs to be
# secured appropriately.
# admin_listen = 0.0.0.0:8001

# Address and port used by the node to communicate with other Kong nodes in the
# cluster with both UDP and TCP messages. All the nodes in the cluster must be
# able to communicate with this node on this address. Only IPv4 addresses are
# Address and port used by the node to communicate with other Kong nodes in the
# cluster with both UDP and TCP messages. All the nodes in the cluster must be
# able to communicate with this node on this address. Only IPv4 addresses are
# allowed (no hostnames).
# cluster_listen = 0.0.0.0:7946

# Address and port used by the node to communicate with the local clustering
# agent (TCP only, and local only). Used internally by this Kong node. Only
# Address and port used by the node to communicate with the local clustering
# agent (TCP only, and local only). Used internally by this Kong node. Only
# IPv4 addresses are allowed (no hostnames).
# cluster_listen_rpc = 127.0.0.1:7373

Expand Down Expand Up @@ -78,43 +78,43 @@
# cassandra_ssl_verify = off
# cassandra_ssl_trusted_cert = NONE

# Cluster authentication options. Provide a user and a password here if your
# Cluster authentication options. Provide a user and a password here if your
# cluster uses the "PasswordAuthenticator" scheme.
# cassandra_username = kong
# cassandra_password = kong

################################## CLUSTER #####################################

# Cluster settings for Kong nodes. Every Kong node that points to the same
# Cluster settings for Kong nodes. Every Kong node that points to the same
# database MUST join together to form a Kong Cluster, in both single or multi-DC
# setups. Kong works on the IP layer (hostnames are not supported, only IPs are
# allowed) and it expects a flat network topology without any NAT between the
# datacenters. A common setup is having a VPN between the two datacenters such
# setups. Kong works on the IP layer (hostnames are not supported, only IPs are
# allowed) and it expects a flat network topology without any NAT between the
# datacenters. A common setup is having a VPN between the two datacenters such
# that the "flat" network assumption of Kong is not violated.

# By default, the cluster_listen address is advertised. If the cluster_listen
# host is "0.0.0.0", then the first local, non-loopback, IPv4 address will be
# advertised to the other nodes. However, in some cases (specifically NAT
# By default, the cluster_listen address is advertised. If the cluster_listen
# host is "0.0.0.0", then the first local, non-loopback, IPv4 address will be
# advertised to the other nodes. However, in some cases (specifically NAT
# traversal), there may be a routable address that cannot be bound to. This flag
# enables gossiping a different address to support this.
# cluster_advertise = NONE

# Key for encrypting network traffic within Kong. Must be a base64-encoded
# Key for encrypting network traffic within Kong. Must be a base64-encoded
# 16-byte key.
# cluster_encrypt = NONE

# The TTL (time to live), in seconds, of a node in the cluster when it stops
# sending healthcheck pings, possibly caused by a node or network failure. If
# the node is not able to send a new healthcheck ping before the expiration,
# then new nodes in the cluster will stop attempting to connect to it on
# The TTL (time to live), in seconds, of a node in the cluster when it stops
# sending healthcheck pings, possibly caused by a node or network failure. If
# the node is not able to send a new healthcheck ping before the expiration,
# then new nodes in the cluster will stop attempting to connect to it on
# startup. Should be at least 60.
# cluster_ttl_on_failure = 3600

#################################### DNS #######################################

# By default Kong leverages on dnsmasq to resolve DNS addresses to the upstream
# services by using the system settings in /etc/hosts and /etc/resolv.conf.
# dnsmasq = on
dnsmasq = off

# The port used by dnsmasq, only used locally by Kong.
# dnsmasq_port = 8053
Expand All @@ -130,21 +130,21 @@
# proxy_listen and proxy_listen_ssl properties. You can optionally enable or
# disable SSL support (note that this may break plugins that are leveraging it).

ssl = on
ssl_cert = NONE
ssl_cert_key = NONE
#ssl = on
#ssl_cert = NONE
#ssl_cert_key = NONE

################################## GENERAL #####################################

# The log level for the events returned by Kong and its services.
# log_level = error

# Comma separated list of additional plugins names to load on this node, used to
# Comma separated list of additional plugins names to load on this node, used to
# load custom plugins that are not already bundled with Kong.
# Plugins will be loaded from the kong.plugins.{name}.* namespace.
# custom_plugins = NONE

# The path to the SSL certificate and key that Kong will use when listening on
# The path to the SSL certificate and key that Kong will use when listening on
# the proxy_listen_ssl port.
# ssl_cert = NONE
# ssl_cert_key = NONE
Expand All @@ -160,7 +160,7 @@ ssl_cert_key = NONE
# Allows Kong to set specific connection and proxying settings in Nginx.
# nginx_optimizations = on

# The size in MB of the internal preallocated in-memory cache for database
# entities. The default value is `128`, and the potential maximum value is the
# The size in MB of the internal preallocated in-memory cache for database
# entities. The default value is `128`, and the potential maximum value is the
# total size of the datastore.
# mem_cache_size = 128m
# mem_cache_size = 128m
14 changes: 4 additions & 10 deletions kong/cmd/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,29 @@ 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 fmt = string.format

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

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(fmt("%s\t%s\t%s", v.name, v.addr, v.status))
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
log("Please wait..")
print(assert(serf:reachability()))
elseif args.command == "force-leave" then
local node_name = args[1]
if not node_name then
error("you need to specify the node name to leave")
end
log(fmt("Force-leaving %s", node_name))
log("force-leaving %s", node_name)
assert(serf:force_leave(node_name))
log("Done")
log("left node %s", node_name)
end
end

Expand All @@ -44,7 +39,6 @@ The available commands are:
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) Nginx prefix path
]]

return {
Expand Down
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
3 changes: 2 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 Down
17 changes: 11 additions & 6 deletions kong/cmd/reload.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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 dnsmasq_signals = require "kong.cmd.utils.dnsmasq_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))
Expand All @@ -22,7 +28,6 @@ local lapp = [[
Usage: kong reload [OPTIONS]
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) Nginx prefix path
]]

Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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"
Expand All @@ -20,7 +20,7 @@ local function execute(args)

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
Expand Down
11 changes: 8 additions & 3 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ 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
}))

local dao = DAOFactory(conf)
Expand All @@ -23,7 +29,6 @@ local lapp = [[
Usage: kong stop [OPTIONS]
Options:
-c,--conf (optional string) configuration file
--prefix (optional string) Nginx prefix path
]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,37 @@ local pl_utils = require "pl.utils"
local pl_file = require "pl.file"
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
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
PAYLOAD=`cat` # Read from stdin
if [ "$SERF_EVENT" != "user" ]; then
PAYLOAD="{\"type\":\"${SERF_EVENT}\",\"entity\": \"${PAYLOAD}\"}"
fi
CMD="\
local http = require 'resty.http' \
local client = http.new() \
client:connect('%s', %d) \
client:request { \
method = 'POST', \
path = '/cluster/events/', \
body = [=[${PAYLOAD}]=], \
headers = { \
['content-type'] = 'application/json' \
} \
}"
resty -e "$CMD"
]]

local function gather_system_infos(compile_env)
local infos = {}

Expand Down Expand Up @@ -102,6 +130,27 @@ local function prepare_prefix(kong_config, nginx_prefix)
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
local id = utils.get_hostname().."_"..kong_config.cluster_listen.."_"..utils.random_string()
pl_file.write(id_path, 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)
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)
if not ok then return nil, stderr end

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

0 comments on commit 12e91ad

Please sign in to comment.