Skip to content

Commit

Permalink
Merge pull request #194 from vjanelle/federations
Browse files Browse the repository at this point in the history
(#195) Allow overriding federation as cli argument and playbook func args
  • Loading branch information
ripienaar authored Jun 12, 2024
2 parents 6f4fad7 + 71bc741 commit 0eb3b5c
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.8
3.2.3
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ group :development, :test do
gem "listen", "~> 3"
gem "mcollective-test"
gem "mocha", "~> 0.12.2"
gem "puppet", "~> 6"
gem "puppet", "~> 8"
gem "rake", ">= 12.3.3"
gem "rdoc"
gem "rspec", "~> 3.9.0"
gem "rubocop", "1.6.1"
gem "semantic_puppet"
gem "webmock"
gem "yard"
gem "google-protobuf", "3.17.3"
gem "google-protobuf", "3.25"

end
1 change: 1 addition & 0 deletions lib/mcollective/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(options)
@config.loadconfig(options[:config]) unless @config.configured
@options = options
@connection_timeout = options[:connection_timeout]
@config.federations = options[:federations] if options[:federations]
else
raise "Invalid parameter passed to Client constructor. Valid types are Hash or String"
end
Expand Down
5 changes: 3 additions & 2 deletions lib/mcollective/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module MCollective
class Config
include Singleton

attr_accessor :mode
attr_accessor :mode, :federations

attr_reader :daemonize, :pluginconf, :configured, :logfile, :keeplogs, :max_log_size, :loglevel, :logfacility,
:identity, :connector, :securityprovider, :factsource, :registration, :registerinterval, :classesfile,
Expand Down Expand Up @@ -112,7 +112,7 @@ def parse_config_file(configfile, libdirs) # rubocop:disable Metrics/MethodLengt
when "default_batch_sleep_time"
@default_batch_sleep_time = Float(val)
else
# server config might now be choria config which will divirge from mcollective
# server config might now be choria config which will diverge from mcollective
# in time, so we only raise this error when it looks like we aren't loading
# a server config else we try our best to load as much as we can
raise("Unknown config parameter '#{key}'") unless configfile =~ /server/
Expand Down Expand Up @@ -231,6 +231,7 @@ def set_config_defaults(configfile) # rubocop:disable Naming/AccessorMethodName
@connection_timeout = nil
@default_batch_size = 0
@default_batch_sleep_time = 1
@federations = []
end

def libdir
Expand Down
6 changes: 6 additions & 0 deletions lib/mcollective/discovery/delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def self.binary_name
end

def self.discover(filter, timeout, limit, client)
@config = Config.instance

raise("Cannot find the choria binary in your path") unless Util.command_in_path?("choria")

cmd = [binary_name, "discover", "-j", "--silent"]
Expand Down Expand Up @@ -37,6 +39,10 @@ def self.discover(filter, timeout, limit, client)
cmd << "-S" << c.first["expr"]
end

unless @config.federations.empty?
cmd << "--federations" << @config.federations.join(",")
end

client.options.fetch(:discovery_options, []).each do |opt|
cmd << "--do" << opt
end
Expand Down
4 changes: 4 additions & 0 deletions lib/mcollective/optionparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def add_common_options
@parser.on("--connection-timeout TIMEOUT", Integer, "Set the timeout for establishing a connection to the middleware") do |v|
@options[:connection_timeout] = Integer(v)
end

@parser.on("--federations FEDERATIONS", String, "Target federations") do |v|
@options[:federations] = String(v).split(",")
end
end

private
Expand Down
3 changes: 2 additions & 1 deletion lib/mcollective/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def rpcoptions

Helpers.add_simplerpc_options(parser, opts)
end

end

# Wrapper to create clients, supposed to be used as
Expand Down Expand Up @@ -171,7 +172,7 @@ def empty_filter?(options)
def self.const_missing(const_name)
super unless const_name == :DDL

Log.warn("MCollective::RPC::DDL is deprecatd, please use MCollective::DDL instead")
Log.warn("MCollective::RPC::DDL is deprecated, please use MCollective::DDL instead")
MCollective::DDL
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/mcollective/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def self.default_options
:collective => nil,
:discovery_method => nil,
:discovery_options => Config.instance.default_discovery_options,
:filter => empty_filter
:filter => empty_filter,
:federations => []
}
end

Expand Down
4 changes: 3 additions & 1 deletion lib/mcollective/util/choria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def federated?
#
# @return [Array<String>]
def federation_collectives
if override_networks = env_fetch("CHORIA_FED_COLLECTIVE", nil)
if (@config.federations.length > 0)
@config.federations
elsif (override_networks = env_fetch("CHORIA_FED_COLLECTIVE", nil))
override_networks.split(",").map(&:strip).reject(&:empty?)
else
get_option("choria.federation.collectives", "").split(",").map(&:strip).reject(&:empty?)
Expand Down
11 changes: 11 additions & 0 deletions lib/mcollective/util/playbook/nodes/mcollective_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ def create_and_configure_client
# @param data [Hash] input data matching nodes.json schema
# @return [McollectiveNodes]
def from_hash(data)
@config = Config.instance

# Allow specifying federations at playbook execution time
if data["federations"]
if data["federations"].instance_of?(String)
@config.federations = [data["federations"]]
else
@config.federations = data["federations"]
end
end

@discovery_method = data.fetch("discovery_method", "mc")
@agents = data.fetch("agents", ["rpcutil"])
@facts = data.fetch("facts", [])
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mcollective/rpc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module MCollective
describe RPC do
describe "#const_missing" do
it "should deprecate only the DDL class" do
Log.expects(:warn).with("MCollective::RPC::DDL is deprecatd, please use MCollective::DDL instead")
Log.expects(:warn).with("MCollective::RPC::DDL is deprecated, please use MCollective::DDL instead")
expect(MCollective::RPC::DDL).to eq(MCollective::DDL)

expect { MCollective::RPC::Foo }.to raise_error(NameError)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mcollective/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ module MCollective
empty_filter = Util.empty_filter
config_file = Util.config_file_for_user

expect(Util.default_options).to eq({:verbose => false, :disctimeout => nil, :timeout => 5, :config => config_file, :filter => empty_filter, :collective => nil, :discovery_method => nil, :discovery_options => []})
expect(Util.default_options).to eq({:verbose => false, :disctimeout => nil, :timeout => 5, :config => config_file, :filter => empty_filter, :collective => nil, :discovery_method => nil, :discovery_options => [], :federations => []})
end
end

Expand Down

0 comments on commit 0eb3b5c

Please sign in to comment.