Skip to content

Commit

Permalink
Merge pull request #20 from ripienaar/19
Browse files Browse the repository at this point in the history
(#19) Support ~/.choriarc and /etc/choria/client.conf
  • Loading branch information
ripienaar authored Dec 18, 2018
2 parents 1b705e6 + 76146fa commit 07b77b4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 33 deletions.
80 changes: 59 additions & 21 deletions lib/mcollective/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,43 +149,79 @@ def self.windows_prefix
File.join(Dir::COMMON_APPDATA, "PuppetLabs", "mcollective")
end

# Picks a config file defaults to ~/.mcollective
# else /etc/mcollective/client.cfg
def self.config_file_for_user
# the set of acceptable config files
def self.choria_windows_prefix
require "win32/dir"
File.join(Dir::COMMON_APPDATA, "ChoriaIO", "choria")
end

def self.mcollective_config_paths_for_user
config_paths = []

# user dotfile
begin
# File.expand_path will raise if HOME isn't set, catch it
user_path = File.expand_path("~/.mcollective")
config_paths << user_path
rescue Exception # rubocop:disable Lint/RescueException, Lint/HandleExceptions
end

# standard locations
if windows?
config_paths << File.join(windows_prefix, "etc", "client.cfg")
else
config_paths << "/etc/puppetlabs/mcollective/client.cfg"
config_paths << "/etc/mcollective/client.cfg"
end

# use the first readable config file, or if none are the first listed
config_paths
end

def self.choria_config_paths_for_user
config_paths = []

begin
# File.expand_path will raise if HOME isn't set, catch it
user_path = File.expand_path("~/.choriarc")
config_paths << user_path
rescue Exception # rubocop:disable Lint/RescueException, Lint/HandleExceptions
end

if windows?
config_paths << File.join(choria_windows_prefix, "etc", "client.conf")
else
config_paths << "/etc/choria/client.conf"
end

config_paths
end

# Picks the default user config file, pririties are first Choria ones then old MCollective ones
#
# In roughly this order, first to exist is used:
#
# - ~/.choriarc
# - APPData/ChoriaIO/choria/etc/client.conf on windows
# - /etc/choria/client.conf on unix
# - ~/.mcollective
# - APPData/PuppetLabs/mcollective/etc/client.cfg on windows
# - /etc/puppetlabs/mcollective/client.cfg
# - /etc/mcollective/client.cfg
def self.config_file_for_user
config_paths = choria_config_paths_for_user + mcollective_config_paths_for_user
found = config_paths.find_index { |file| File.readable?(file) } || 0
config_paths[found]
end

# Creates a standard options hash
def self.default_options
{:verbose => false,
:disctimeout => nil,
:timeout => 5,
:config => config_file_for_user,
:collective => nil,
:discovery_method => nil,
:discovery_options => Config.instance.default_discovery_options,
:filter => empty_filter}
{
:verbose => false,
:disctimeout => nil,
:timeout => 5,
:config => config_file_for_user,
:collective => nil,
:discovery_method => nil,
:discovery_options => Config.instance.default_discovery_options,
:filter => empty_filter
}
end

def self.make_subscriptions(agent, type, collective=nil)
Expand Down Expand Up @@ -276,12 +312,14 @@ def self.windows?
def self.color(code)
colorize = Config.instance.color

colors = {:red => "",
:green => "",
:yellow => "",
:cyan => "",
:bold => "",
:reset => ""}
colors = {
:red => "",
:green => "",
:yellow => "",
:cyan => "",
:bold => "",
:reset => ""
}

if colorize
return colors[code] || ""
Expand Down
63 changes: 51 additions & 12 deletions spec/unit/mcollective/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,64 @@ module MCollective
end
end

describe '#config_file_for_user' do
before :each do
File.stubs(:expand_path).raises
File.stubs(:readable?).returns(false)
describe "#mcollective_config_paths_for_user" do
it "should support unix" do
Util.stubs(:windows?).returns(false)

expect(Util.mcollective_config_paths_for_user).to eq([
File.expand_path("~/.mcollective"),
"/etc/puppetlabs/mcollective/client.cfg",
"/etc/mcollective/client.cfg"
])
end

it 'should default to /etc/puppetlabs/mcollective/client.cfg if ~/.mcollective can not be expanded' do
Util.config_file_for_user.should == '/etc/puppetlabs/mcollective/client.cfg'
if Util.windows?
it "should support windows" do
Util.stubs(:windows?).returns(true)
Util.stubs(:choria_windows_prefix).returns("C:/temp")

expect(Util.choria_config_paths_for_user).to eq([
File.expand_path("~/.mcollective"),
File.join("c:/temp", "etc", "client.cfg")
])
end
end
end

describe "#choria_config_paths_for_user" do
it "should support unix" do
Util.stubs(:windows?).returns(false)

it 'should default to ~/.mcollective if it can be expanded' do
File.expects(:expand_path).with('~/.mcollective').returns('/home/mco/.mcollective')
Util.config_file_for_user.should == '/home/mco/.mcollective'
expect(Util.choria_config_paths_for_user).to eq([
File.expand_path("~/.choriarc"),
"/etc/choria/client.conf"
])
end

it 'should choose /etc/mcollective/client.cfg if it is readable' do
File.expects(:readable?).with('/etc/mcollective/client.cfg').returns(true)
Util.config_file_for_user.should == '/etc/mcollective/client.cfg'
if Util.windows?
it "should support windows" do
Util.stubs(:windows?).returns(true)
Util.stubs(:choria_windows_prefix).returns("C:/temp")

expect(Util.choria_config_paths_for_user).to eq([
File.expand_path("~/.choriarc"),
File.join("c:/temp", "etc", "client.conf")
])
end
end
end

describe '#config_file_for_user' do
it "should pick the first path that exist" do
Util.expects(:choria_config_paths_for_user).returns(["choria1", "choria2"])
Util.expects(:mcollective_config_paths_for_user).returns(["mcollective1", "mcollective2"])

File.expects(:readable?).with("choria1").returns(false)
File.expects(:readable?).with("choria2").returns(false)
File.expects(:readable?).with("mcollective1").returns(false)
File.expects(:readable?).with("mcollective2").returns(true)

expect(Util.config_file_for_user).to eq("mcollective2")
end
end

Expand Down

0 comments on commit 07b77b4

Please sign in to comment.