Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #206 from ripienaar/204
Browse files Browse the repository at this point in the history
(#204) add a choria_util agent
  • Loading branch information
ripienaar committed Mar 7, 2017
2 parents 819f20e + 078c35c commit a72a98d
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
|Date |Issue |Description |
|----------|------|---------------------------------------------------------------------------------------------------------|
|2017/03/07|204 |Add a `choria_util` agent that extract running info out of the mcollective daemon |
|2017/03/06|173 |Support playbook elapsed time in templates |
|2017/03/06|201 |Allow server randomization to be configured |
|2017/02/16|199 |Use the configured *ssl_dir* for storing the public cert cache |
Expand Down
67 changes: 67 additions & 0 deletions lib/mcollective/agent/choria_util.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
metadata :name => "choria_util",
:description => "Choria Utilities",
:author => "R.I.Pienaar <rip@devco.net>",
:license => "Apache-2.0",
:version => "1.0.0",
:url => "http:/choria.io",
:timeout => 5

requires :mcollective => "2.9.0"

action "info", :description => "Choria related information from the running Daemon and Middleware" do
output :security,
:description => "Security Provider plugin",
:display_as => "Security Provider"

output :connector,
:description => "Connector plugin",
:display_as => "Connector"

output :client_version,
:description => "Client gem version",
:display_as => "Client Version"

output :client_flavour,
:description => "Client gem flavour",
:display_as => "Client Flavour"

output :client_options,
:description => "Active client gem options",
:display_as => "Client Options"

output :connected_server,
:description => "Connected middleware server",
:display_as => "Connected Broker"

output :client_stats,
:description => "Client gem statistics",
:display_as => "Client Stats"

output :facter_domain,
:description => "Facter domain",
:display_as => "Facter Domain"

output :facter_command,
:description => "Command used for Facter",
:display_as => "Facter"

output :srv_domain,
:description => "Configured SRV domain",
:display_as => "SRV Domain"

output :using_srv,
:description => "Indicates if SRV records are considered",
:display_as => "SRV Used"

output :middleware_servers,
:description => "Middleware Servers configured or discovered",
:display_as => "Middleware"

summarize do
aggregate summary(:client_version)
aggregate summary(:client_flavour)
aggregate summary(:connected_server)
aggregate summary(:srv_domain)
aggregate summary(:using_srv)
end
end
37 changes: 37 additions & 0 deletions lib/mcollective/agent/choria_util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module MCollective
module Agent
class Choria_util < RPC::Agent
action "info" do
connector = PluginManager["connector_plugin"]

reply.fail!("Only support collectives using the Choria NATS connector") unless connector.is_a?(Connector::Nats)

reply[:security] = config.securityprovider
reply[:connector] = config.connector
reply[:client_version] = connector.client_version
reply[:client_flavour] = connector.client_flavour
reply[:client_options] = stringify_keys(connector.active_options).reject {|k, _| k == "tls"}
reply[:client_stats] = stringify_keys(connector.stats)
reply[:facter_domain] = choria.facter_domain
reply[:facter_command] = choria.facter_cmd
reply[:srv_domain] = choria.srv_domain
reply[:using_srv] = choria.should_use_srv?
reply[:middleware_servers] = choria.middleware_servers.map {|s| s.join(":")}

if connector.connected?
reply[:connected_server] = "%s:%s" % [connector.connected_server.host, connector.connected_server.port]
else
reply[:connected_server] = "disconnected"
end
end

def stringify_keys(hash)
Hash[hash.map {|key, val| [key.to_s, val]}]
end

def choria
@_choria ||= Util::Choria.new("production", nil, false)
end
end
end
end
24 changes: 23 additions & 1 deletion lib/mcollective/connector/nats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def connected?
connection.connected?
end

# Returns the current connected server
# Current connected server
#
# @return [String,nil]
def connected_server
Expand All @@ -36,6 +36,28 @@ def stats
connection.stats
end

# Client library version
#
# @return [String]
def client_version
connection.client_version
end

# Client library flavour
#
# @note this flavour/version split is because I anticipate we'll support streaming too eventually
# @return [String]
def client_flavour
connection.client_flavour
end

# Connection options from the NATS gem
#
# @return [Hash]
def active_options
connection.active_options
end

# Attempts to connect to the middleware, noop when already connected
#
# @return [void]
Expand Down
2 changes: 1 addition & 1 deletion lib/mcollective/util/choria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def check_ssl_setup(log=true)
# @param default_host [String] default hostname
# @param default_port [String] default port
# @return [Array<Array<String, String>>] groups of host and port
def middleware_servers(default_host, default_port)
def middleware_servers(default_host="puppet", default_port="4222")
if servers = get_option("choria.middleware_hosts", nil)
hosts = servers.split(",").map do |server|
server.split(":")
Expand Down
23 changes: 23 additions & 0 deletions lib/mcollective/util/natswrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ def stats
@client.stats
end

# Client library flavour
#
# @return [String]
def client_flavour
"nats-pure"
end

# Client library version
#
# @return [String]
def client_version
NATS::IO::VERSION
end

# Connection options from the NATS gem
#
# @return [Hash]
def active_options
return {} unless has_client?

@client.options
end

# Is NATS connected
#
# @return [Boolean]
Expand Down
2 changes: 2 additions & 0 deletions module/data/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mcollective_choria::client_directories:
- util/playbook/nodes
- util/playbook/tasks
mcollective_choria::server_files:
- agent/choria_util.rb
- audit/choria.rb
- registration/choria.rb
mcollective_choria::client_files:
Expand Down Expand Up @@ -48,6 +49,7 @@ mcollective_choria::client_files:
mcollective_choria::common_directories:
- util/choria
mcollective_choria::common_files:
- agent/choria_util.ddl
- connector/nats.ddl
- connector/nats.rb
- security/choria.rb
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/mcollective/connector/nats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ module MCollective
msg.collective = "mcollective"
end

describe "#client_options" do
it "should get the options from the wrapper" do
connection.expects(:active_options).returns(:rspec => 1)
expect(connector.active_options).to eq(:rspec => 1)
end
end

describe "#client_flavour" do
it "should get the flavour from the wrapper" do
connection.expects(:client_flavour).returns("rspec nats")
expect(connector.client_flavour).to eq("rspec nats")
end
end

describe "#client_version" do
it "should get the version from the wrapper" do
connection.expects(:client_version).returns("1.2.3")
expect(connector.client_version).to eq("1.2.3")
end
end

describe "#stats" do
it "should get the stats from the wrapper" do
connection.expects(:stats).returns(:stats => 1)
Expand Down
26 changes: 25 additions & 1 deletion spec/unit/mcollective/util/natswrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,36 @@
module MCollective
describe Util::NatsWrapper do
let(:wrapper) { Util::NatsWrapper.new }
let(:client) { stub(:connected? => true, :connected_server => "rspec.example.net:1234") }
let(:client) do
stub(
:options => {:nats => "options"},
:connected? => true,
:connected_server => "rspec.example.net:1234"
)
end

before(:each) do
wrapper.stub_client(client)
end

describe "#active_options" do
it "should get the options from the client" do
expect(wrapper.active_options).to eq(:nats => "options")
end
end

describe "#client_version" do
it "should return the gem version" do
expect(wrapper.client_version).to eq(NATS::IO::VERSION)
end
end

describe "#client_flavour" do
it "should return nats-pure" do
expect(wrapper.client_flavour).to eq("nats-pure")
end
end

describe "#connected_server" do
it "should be nil when not connected" do
client.expects(:connected?).returns(false)
Expand Down

0 comments on commit a72a98d

Please sign in to comment.