-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding missing serf commands and cluster CLI
- Loading branch information
1 parent
11e41e1
commit 2b8b04d
Showing
7 changed files
with
130 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
local pl_app = require "pl.lapp" | ||
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 | ||
This comment has been minimized.
Sorry, something went wrong. |
||
})) | ||
|
||
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)) | ||
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 | ||
pl_app.quit("You need to specify the node name to leave") | ||
This comment has been minimized.
Sorry, something went wrong.
thibaultcha
Member
|
||
end | ||
log(fmt("Force-leaving %s", node_name)) | ||
assert(serf:force_leave(node_name)) | ||
log("Done") | ||
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 = {members = true, keygen = true, reachability = true, ["force-leave"] = true} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
local NGINX_VARS = { | ||
prefix = true, | ||
plugins = true, | ||
cluster_listen = true, | ||
cluster_listen_rpc = true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,13 @@ | |
|
||
local pl_stringx = require "pl.stringx" | ||
local pl_utils = require "pl.utils" | ||
local pl_path = require "pl.path" | ||
local pl_file = require "pl.file" | ||
local cjson = require "cjson.safe" | ||
local log = require "kong.cmd.utils.log" | ||
local fmt = string.format | ||
|
||
local ok, _, stdout, stderr = pl_utils.executeex "/bin/hostname" | ||
if not ok then error(stderr) end | ||
local hostname = pl_stringx.strip(stdout) | ||
local serf_node_id = "serf.id" | ||
|
||
local Serf = {} | ||
Serf.__index = Serf | ||
|
@@ -22,9 +22,9 @@ Serf.args_mt = { | |
end | ||
} | ||
|
||
function Serf.new(kong_config, dao) | ||
function Serf.new(kong_config, nginx_prefix, dao) | ||
return setmetatable({ | ||
node_name = hostname.."_"..kong_config.cluster_listen, | ||
node_name = assert(pl_file.read(pl_path.join(nginx_prefix, serf_node_id))), | ||
config = kong_config, | ||
dao = dao | ||
}, Serf) | ||
|
@@ -40,7 +40,7 @@ function Serf:invoke_signal(signal, args, no_rpc) | |
local rpc = no_rpc and "" or "-rpc-addr="..self.config.cluster_listen_rpc | ||
local cmd = fmt("serf %s %s %s", signal, rpc, tostring(args)) | ||
local ok, code, stdout = pl_utils.executeex(cmd) | ||
if not ok or code ~= 0 then return nil, stdout end | ||
if not ok or code ~= 0 then return nil, pl_stringx.splitlines(stdout)[1] end -- always print the first error line of serf | ||
|
||
return stdout | ||
end | ||
|
@@ -49,6 +49,23 @@ function Serf:join_node(address) | |
return select(2, self:invoke_signal("join", address)) == nil | ||
end | ||
|
||
function Serf:leave() | ||
local res, err = self:invoke_signal("leave") | ||
if not res then return nil, err end | ||
|
||
local _, err = self.dao.nodes:delete {name = self.node_name} | ||
if err then return nil, err end | ||
|
||
return true | ||
end | ||
|
||
function Serf:force_leave(node_name) | ||
local res, err = self:invoke_signal("force-leave", node_name) | ||
if not res then return nil, err end | ||
|
||
return true | ||
end | ||
|
||
function Serf:members() | ||
local res, err = self:invoke_signal("members", {["-format"] = "json"}) | ||
if not res then return nil, err end | ||
|
@@ -59,6 +76,18 @@ function Serf:members() | |
return json.members | ||
end | ||
|
||
function Serf:keygen() | ||
local res, err = self:invoke_signal("keygen") | ||
if not res then return nil, err end | ||
return res | ||
end | ||
|
||
function Serf:reachability() | ||
local res, err = self:invoke_signal("reachability") | ||
if not res then return nil, err end | ||
return res | ||
This comment has been minimized.
Sorry, something went wrong.
thibaultcha
Member
|
||
end | ||
|
||
function Serf:autojoin() | ||
-- Delete current node just in case it was there | ||
-- (due to an inconsistency caused by a crash) | ||
|
@@ -110,7 +139,7 @@ function Serf:add_node() | |
local _, err = self.dao.nodes:insert({ | ||
name = self.node_name, | ||
cluster_listening_address = pl_stringx.strip(addr) | ||
}, {ttl = 3600}) | ||
}, {ttl = self.config.cluster_ttl_on_failure}) | ||
if err then return nil, tostring(err) end | ||
|
||
return true | ||
|
There is no
prefix
argument in your command according to thelapp
string definition of it