4 load_application(appname)
5 PluginManager["#{appname}_application"]
12 load_application(appname)
14 e.backtrace.first << Util.colorize(:red, " <----")
15 STDERR.puts "Application '#{appname}' failed to load:"
17 STDERR.puts Util.colorize(:red, " #{e} (#{e.class})")
19 STDERR.puts " %s" % [e.backtrace.join("\n ")]
23 PluginManager["#{appname}_application"].run
26 def self.load_application(appname)
27 return if PluginManager.include?("#{appname}_application")
31 PluginManager.loadclass "MCollective::Application::#{appname.capitalize}"
32 PluginManager << {:type => "#{appname}_application", :class => "MCollective::Application::#{appname.capitalize}"}
35 # Returns an array of applications found in the lib dirs
39 PluginManager.find("application")
43 STDERR.puts "Failed to generate application list: #{e.class}: #{e}"
47 # Filters a string of opts out using Shellwords
48 # keeping only things related to --config and -c
49 def self.filter_extra_options(opts)
51 words = Shellwords.shellwords(opts)
52 words.each_with_index do |word,idx|
54 return "--config=#{words[idx + 1]}"
55 elsif word == "--config"
56 return "--config=#{words[idx + 1]}"
59 elsif word =~ /\-\-config=/
67 # We need to know the config file in order to know the libdir
68 # so that we can find applications.
70 # The problem is the CLI might be stuffed with options only the
71 # app in the libdir might understand so we have a chicken and
74 # We're parsing and filtering MCOLLECTIVE_EXTRA_OPTS removing
75 # all but config related options and parsing the options looking
76 # just for the config file.
78 # We're handling failures gracefully and finally restoring the
79 # ARG and MCOLLECTIVE_EXTRA_OPTS to the state they were before
82 # This is mostly a hack, when we're redoing how config works
83 # this stuff should be made less sucky
85 return if Config.instance.configured
87 original_argv = ARGV.clone
88 original_extra_opts = ENV["MCOLLECTIVE_EXTRA_OPTS"].clone rescue nil
91 parser = OptionParser.new
92 parser.on("--config CONFIG", "-c", "Config file") do |f|
96 parser.program_name = $0
100 # avoid option parsers own internal version handling that sux
101 parser.on("-v", "--verbose")
103 if original_extra_opts
105 # optparse will parse the whole ENV in one go and refuse
106 # to play along with the retry trick I do below so in
107 # order to handle unknown options properly I parse out
108 # only -c and --config deleting everything else and
109 # then restore the environment variable later when I
111 ENV["MCOLLECTIVE_EXTRA_OPTS"] = filter_extra_options(ENV["MCOLLECTIVE_EXTRA_OPTS"].clone)
112 parser.environment("MCOLLECTIVE_EXTRA_OPTS")
113 rescue Exception => e
114 Log.error("Failed to parse MCOLLECTIVE_EXTRA_OPTS: #{e}")
117 ENV["MCOLLECTIVE_EXTRA_OPTS"] = original_extra_opts.clone
122 rescue OptionParser::InvalidOption => e
127 original_argv.each {|a| ARGV << a}
129 configfile = Util.config_file_for_user unless configfile
131 Config.instance.loadconfig(configfile)