Skip to content

Commit

Permalink
fix(api) allow to delete APIs and Consumers by name
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha authored and subnetmarco committed Jan 21, 2016
1 parent cf4ea3a commit e7ce5d6
Show file tree
Hide file tree
Showing 23 changed files with 747 additions and 108 deletions.
22 changes: 22 additions & 0 deletions .ci/setup_dnsmasq.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

if [ "$TEST_SUITE" == "unit" ]; then
echo "Exiting, no integration tests"
exit
fi

mkdir -p $DNSMASQ_DIR

if [ ! "$(ls -A $DNSMASQ_DIR)" ]; then
pushd $DNSMASQ_DIR
wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-${DNSMASQ_VERSION}.tar.gz
tar xzf dnsmasq-${DNSMASQ_VERSION}.tar.gz

pushd dnsmasq-${DNSMASQ_VERSION}
make install DESTDIR=$DNSMASQ_DIR
popd

popd
fi
28 changes: 14 additions & 14 deletions .ci/setup_lua.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ source .ci/platform.sh
# Lua/LuaJIT
############

if [ "$LUA" == "luajit" ]; then
if [ "$LUA_VERSION" == "luajit" ]; then
LUAJIT="yes"
LUA="luajit-2.0"
elif [ "$LUA" == "luajit-2.0" ]; then
LUA_VERSION="luajit-2.0"
elif [ "$LUA_VERSION" == "luajit-2.0" ]; then
LUAJIT="yes"
elif [ "$LUA" == "luajit-2.1" ]; then
elif [ "$LUA_VERSION" == "luajit-2.1" ]; then
LUAJIT="yes"
fi

Expand All @@ -33,32 +33,32 @@ if [ "$LUAJIT" == "yes" ]; then
git clone https://github.com/luajit/luajit $LUAJIT_DIR
pushd $LUAJIT_DIR

if [ "$LUA" == "luajit-2.0" ]; then
if [ "$LUA_VERSION" == "luajit-2.0" ]; then
git checkout v2.0.4
elif [ "$LUA" == "luajit-2.1" ]; then
elif [ "$LUA_VERSION" == "luajit-2.1" ]; then
git checkout v2.1
fi

make
make install PREFIX=$LUAJIT_DIR
popd

if [ "$LUA" == "luajit-2.1" ]; then
if [ "$LUA_VERSION" == "luajit-2.1" ]; then
ln -sf $LUAJIT_DIR/bin/luajit-2.1.0-beta1 $LUAJIT_DIR/bin/luajit
fi

ln -sf $LUAJIT_DIR/bin/luajit $LUAJIT_DIR/bin/lua
fi

LUA_INCLUDE="$LUAJIT_DIR/include/$LUA"
LUA_INCLUDE="$LUAJIT_DIR/include/$LUA_VERSION"
else
if [ "$LUA" == "lua5.1" ]; then
if [ "$LUA_VERSION" == "lua5.1" ]; then
curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz
pushd lua-5.1.5
elif [ "$LUA" == "lua5.2" ]; then
elif [ "$LUA_VERSION" == "lua5.2" ]; then
curl http://www.lua.org/ftp/lua-5.2.3.tar.gz | tar xz
pushd lua-5.2.3
elif [ "$LUA" == "lua5.3" ]; then
elif [ "$LUA_VERSION" == "lua5.3" ]; then
curl http://www.lua.org/ftp/lua-5.3.0.tar.gz | tar xz
pushd lua-5.3.0
fi
Expand All @@ -84,11 +84,11 @@ git checkout v$LUAROCKS_VERSION

if [ "$LUAJIT" == "yes" ]; then
LUA_DIR=$LUAJIT_DIR
elif [ "$LUA" == "lua5.1" ]; then
elif [ "$LUA_VERSION" == "lua5.1" ]; then
CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --lua-version=5.1"
elif [ "$LUA" == "lua5.2" ]; then
elif [ "$LUA_VERSION" == "lua5.2" ]; then
CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --lua-version=5.2"
elif [ "$LUA" == "lua5.3" ]; then
elif [ "$LUA_VERSION" == "lua5.3" ]; then
CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --lua-version=5.3"
fi

Expand Down
17 changes: 17 additions & 0 deletions .ci/setup_serf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e

if [ "$TEST_SUITE" == "unit" ]; then
echo "Exiting, no integration tests"
exit
fi

mkdir -p $SERF_DIR

if [ ! "$(ls -A $SERF_DIR)" ]; then
pushd $SERF_DIR
wget https://releases.hashicorp.com/serf/${SERF_VERSION}/serf_${SERF_VERSION}_linux_amd64.zip
unzip serf_${SERF_VERSION}_linux_amd64.zip
popd
fi
4 changes: 2 additions & 2 deletions kong-0.5.4-1.rockspec → kong-0.6.0rc3-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "kong"
version = "0.5.4-1"
version = "0.6.0rc3-1"
supported_platforms = {"linux", "macosx"}
source = {
url = "git://github.com/Mashape/kong",
tag = "0.5.4"
tag = "0.6.0rc3"
}
description = {
summary = "Kong is a scalable and customizable API Management Layer built on top of Nginx.",
Expand Down
17 changes: 10 additions & 7 deletions kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ return {
GET = function(self, dao_factory, helpers)
local res, err = Serf(configuration):invoke_signal("members", {["-format"] = "json"})
if err then
return helpers.yield_error(err)
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end

local members = cjson.decode(res).members
local result = {data = {}, total = #members}
local result = {data = {}}
for _, v in pairs(members) do
table_insert(result.data, {
name = v.name,
address = v.addr,
status = v.status
})
if not self.params.status or (self.params.status and v.status == self.params.status) then
table_insert(result.data, {
name = v.name,
address = v.addr,
status = v.status
})
end
end

result.total = #result.data
return responses.send_HTTP_OK(result)
end,

Expand Down
10 changes: 3 additions & 7 deletions kong/cli/cmds/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ Usage: kong cluster <command> <args> [options]
Commands:
<command> (string) where <command> is one of:
join, members, force-leave, reachability, keygen
members, force-leave, reachability, keygen
Options:
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

local JOIN = "join"
local KEYGEN = "keygen"
local FORCE_LEAVE = "force-leave"
local SUPPORTED_COMMANDS = {JOIN, "members", KEYGEN, "reachability", FORCE_LEAVE}
local SUPPORTED_COMMANDS = {"members", KEYGEN, "reachability", FORCE_LEAVE}

if not utils.table_contains(SUPPORTED_COMMANDS, args.command) then
lapp.quit("Invalid cluster command. Supported commands are: "..table.concat(SUPPORTED_COMMANDS, ", "))
Expand All @@ -37,10 +36,7 @@ args.config = nil

local skip_running_check

if signal == JOIN and utils.table_size(args) ~= 1 then
logger:error("You must specify one address")
os.exit(1)
elseif signal == FORCE_LEAVE and utils.table_size(args) ~= 1 then
if signal == FORCE_LEAVE and utils.table_size(args) ~= 1 then
logger:error("You must specify a node name")
os.exit(1)
elseif signal == KEYGEN then
Expand Down
4 changes: 2 additions & 2 deletions kong/cli/services/base_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ function BaseService:new(name, nginx_working_dir)
end

function BaseService:is_running()
local result = nil
local result = false

local pid = IO.read_file(self._pid_file_path)
if pid then
local _, code = IO.os_execute("ps -p "..stringy.strip(pid))
local _, code = IO.os_execute("kill -0 "..stringy.strip(pid))
if code and code == 0 then
result = pid
end
Expand Down
6 changes: 4 additions & 2 deletions kong/cli/services/dnsmasq.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ function Dnsmasq:start()
return nil, err
end

local res, code = IO.os_execute(cmd.." -p "..self._configuration.dns_resolver.port.." --pid-file="..self._pid_file_path.." -N -o")
-- dnsmasq always listens on the local 127.0.0.1 address
local res, code = IO.os_execute(cmd.." -p "..self._configuration.dns_resolver.port.." --pid-file="..self._pid_file_path.." -N -o --listen-address=127.0.0.1")
if code == 0 then
while not self:is_running() do
-- Wait for PID file to be created
end

setmetatable(self._configuration.dns_resolver, require "kong.tools.printable")
logger:info(string.format([[dnsmasq ...........%s]], tostring(self._configuration.dns_resolver)))
logger:info(string.format([[dnsmasq............%s]], tostring(self._configuration.dns_resolver)))
return true
else
return nil, res
end
end
return true
end

function Dnsmasq:stop()
Expand Down
23 changes: 14 additions & 9 deletions kong/cli/services/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ local function prepare_ssl_certificates(configuration)
trusted_ssl_cert_path = trusted_ssl_cert_path }
end

local function get_current_user()
return IO.os_execute("whoami")
end

local function prepare_nginx_configuration(configuration, ssl_config)
-- Extract nginx config from kong config, replace any needed value
local nginx_config = configuration.nginx
local nginx_inject = {
proxy_port = configuration.listen_address..":"..configuration.proxy_port,
proxy_ssl_port = configuration.listen_address..":"..configuration.proxy_ssl_port,
admin_api_port = configuration.listen_address..":"..configuration.admin_api_port,
user = get_current_user(),
proxy_listen = configuration.proxy_listen,
proxy_listen_ssl = configuration.proxy_listen_ssl,
admin_api_listen = configuration.admin_api_listen,
dns_resolver = configuration.dns_resolver.address,
memory_cache_size = configuration.memory_cache_size,
ssl_cert = ssl_config.ssl_cert_path,
Expand Down Expand Up @@ -194,13 +199,13 @@ function Nginx:start()

local ok, err = self:_invoke_signal(cmd, START)
if ok then
local ports = {
proxy_port = self._configuration.proxy_port,
proxy_ssl_port = self._configuration.proxy_ssl_port,
admin_api_port = self._configuration.admin_api_port
local listen_addresses = {
proxy_listen = self._configuration.proxy_listen,
proxy_listen_ssl = self._configuration.proxy_listen_ssl,
admin_api_listen = self._configuration.admin_api_listen
}
setmetatable(ports, require "kong.tools.printable")
logger:info(string.format([[nginx .............%s]], tostring(ports)))
setmetatable(listen_addresses, require "kong.tools.printable")
logger:info(string.format([[nginx .............%s]], tostring(listen_addresses)))
end

return ok, err
Expand Down
37 changes: 21 additions & 16 deletions kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Serf:new(configuration)
end

function Serf:_get_cmd()
local cmd, err = Serf.super._get_cmd(self, {}, function(path)
local cmd, err = Serf.super._get_cmd(self, {}, function(path)
local res, code = IO.os_execute(path.." version")
if code == 0 then
return res:match("^Serf v0.7.0")
Expand Down Expand Up @@ -57,7 +57,7 @@ fi
echo $PAYLOAD > /tmp/payload
COMMAND='require("kong.tools.http_client").post("http://127.0.0.1:]]..self._configuration.admin_api_port..[[/cluster/events/", ]].."[['${PAYLOAD}']]"..[[, {["content-type"] = "application/json"})'
COMMAND='require("kong.tools.http_client").post("http://]]..self._configuration.admin_api_listen..[[/cluster/events/", ]].."[['${PAYLOAD}']]"..[[, {["content-type"] = "application/json"})'
echo $COMMAND | ]]..luajit_path..[[
]]
Expand All @@ -75,12 +75,20 @@ echo $COMMAND | ]]..luajit_path..[[
return true
end


function Serf:_join_node(address)
local _, err = self:invoke_signal("join", {address})
if err then
return false
end
return true
end

function Serf:_autojoin(current_node_name)
if self._configuration.cluster["auto-join"] then
logger:info("Trying to auto-join Kong nodes, please wait..")

logger:info("Auto-joining cluster, please wait..")

-- Delete current node just in case it was there
-- Delete current node just in case it was there (due to an inconsistency caused by a crash)
local _, err = self._dao_factory.nodes:delete({
name = current_node_name
})
Expand All @@ -95,25 +103,22 @@ function Serf:_autojoin(current_node_name)
if #nodes == 0 then
logger:warn("Cannot auto-join the cluster because no nodes were found")
else

-- Sort by newest to oldest
-- Sort by newest to oldest (although by TTL would be a better sort)
table.sort(nodes, function(a, b)
return a.created_at > b.created_at
end)

local joined
for _, v in ipairs(nodes) do
local _, err = self:invoke_signal("join", {v.cluster_listening_address})
if err then
logger:warn("Cannot join "..v.cluster_listening_address..". If the node does not exist anymore it will be automatically purged.")
else
if self:_join_node(v.cluster_listening_address) then
logger:info("Successfully auto-joined "..v.cluster_listening_address)
joined = true
break
else
logger:warn("Cannot join "..v.cluster_listening_address..". If the node does not exist anymore it will be automatically purged.")
end
end
if not joined then
--return false, "Could not join the existing cluster"
logger:warn("Could not join the existing cluster")
end
end
Expand All @@ -136,8 +141,8 @@ function Serf:start()

-- Prepare arguments
local cmd_args = {
["-bind"] = self._configuration.listen_address..":"..self._configuration.cluster_listening_port,
["-rpc-addr"] = "127.0.0.1:"..self._configuration.cluster_rpc_listening_port,
["-bind"] = self._configuration.cluster_listen,
["-rpc-addr"] = self._configuration.cluster_listen_rpc,
["-advertise"] = self._configuration.cluster.advertise,
["-encrypt"] = self._configuration.cluster.encrypt,
["-log-level"] = "err",
Expand Down Expand Up @@ -184,7 +189,7 @@ function Serf:invoke_signal(signal, args, no_rpc, skip_running_check)

if not args then args = {} end
setmetatable(args, require "kong.tools.printable")
local res, code = IO.os_execute(cmd.." "..signal.." "..(no_rpc and "" or "-rpc-addr=127.0.0.1:"..self._configuration.cluster_rpc_listening_port).." "..tostring(args), true)
local res, code = IO.os_execute(cmd.." "..signal.." "..(no_rpc and "" or "-rpc-addr="..self._configuration.cluster_listen_rpc).." "..tostring(args), true)
if code == 0 then
return res
else
Expand All @@ -195,7 +200,7 @@ end
function Serf:event(t_payload)
local args = {
["-coalesce"] = false,
["-rpc-addr"] = "127.0.0.1:"..self._configuration.cluster_rpc_listening_port
["-rpc-addr"] = self._configuration.cluster_listen_rpc
}
setmetatable(args, require "kong.tools.printable")

Expand Down
7 changes: 3 additions & 4 deletions kong/cli/utils/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ function _M.check_status(configuration, configuration_path)
end

function _M.stop_all(configuration, configuration_path)
-- Stop in reverse order to keep dependencies running
for index = #services,1,-1 do
services[index](configuration, configuration_path):stop()
end
for _, service in ipairs(services) do
service(configuration, configuration_path):stop()
end
end

function _M.start_all(configuration, configuration_path)
Expand Down
2 changes: 1 addition & 1 deletion kong/constants.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local VERSION = "0.5.4"
local VERSION = "0.6.0rc3"

return {
NAME = "kong",
Expand Down
Loading

0 comments on commit e7ce5d6

Please sign in to comment.