class MCollective::Application::Plugin - mcollective version 2.12.5

# class MCollective::Application::Plugin

### Public Instance Methods

doc_command() click to toggle source

Show application list and plugin help

        # File lib/mcollective/application/plugin.rb
    226 def doc_command
    227   known_plugin_types = [["Agents", :agent], ["Aggregate", :aggregate],
    228                         ["Connectors", :connector], ["Data Queries", :data],
    229                         ["Discovery Methods", :discovery], ["Validator Plugins", :validator]]
    230
    231   if configuration.include?(:target) && configuration[:target] != "."
    232     if configuration[:target] =~ /^(.+?)\/(.+)$/
    233       ddl = load_plugin_ddl($2.to_sym, $1)
    234     else
    235       found_plugin_type = nil
    236
    237       known_plugin_types.each do |plugin_type|
    238         PluginManager.find(plugin_type[1], "ddl").each do |ddl|
    239           pluginname = ddl.gsub(/_#{plugin_type[1]}$/, "")
    240           if pluginname == configuration[:target]
    241             abort "Duplicate plugin name found, please specify a full path like agent/rpcutil" if found_plugin_type
    242             found_plugin_type = plugin_type[1]
    243           end
    244         end
    245       end
    246
    247       abort "Could not find a plugin named '%s' in any supported plugin type" % configuration[:target] unless found_plugin_type
    248       ddl = load_plugin_ddl(configuration[:target], found_plugin_type)
    249     end
    250
    251     if ddl
    252       puts ddl.help(configuration[:rpctemplate])
    253     else
    254       abort "Could not find a '%s' plugin named '%s'" % configuration[:target].split('/')
    255     end
    256
    257   else
    258     puts "Please specify a plugin. Available plugins are:"
    259     puts
    260
    261     load_errors = []
    262
    263     known_plugin_types.each do |plugin_type|
    264       puts "%s:" % plugin_type[0]
    265
    266       PluginManager.find(plugin_type[1], "ddl").each do |ddl|
    267         begin
    268           help = DDL.new(ddl, plugin_type[1])
    269           pluginname = ddl.gsub(/_#{plugin_type[1]}$/, "")
    270           puts "  %-25s %s" % [pluginname, help.meta[:description]]
    271         rescue => e
    272           load_errors << [plugin_type[1], ddl, e]
    273         end
    274       end
    275
    276       puts
    277     end
    278
    279     unless load_errors.empty?
    280       puts "Plugin Load Errors:"
    281
    282       load_errors.each do |e|
    283         puts "  %-25s %s" % ["#{e[0]}/#{e[1]}", Util.colorize(:yellow, e[2])]
    284       end
    285     end
    286   end
    287 end

generate_command() click to toggle source

Generate a plugin skeleton

        # File lib/mcollective/application/plugin.rb
    170 def generate_command
    171   raise "undefined plugin type. cannot generate plugin. valid types are 'agent' and 'data'" if configuration["target"] == '.'
    172
    173   unless configuration[:pluginname]
    174     puts "No plugin name specified. Using 'new_plugin'"
    175     configuration[:pluginname] = "new_plugin"
    176   end
    177
    178   load_plugin_config_values
    179
    180   case configuration[:target].downcase
    181   when 'agent'
    182     Generators::AgentGenerator.new(configuration[:pluginname], configuration[:actions], configuration[:pluginname],
    183                                    configuration[:description], configuration[:author], configuration[:license],
    184                                    configuration[:version], configuration[:url], configuration[:timeout])
    185   when 'data'
    186     raise "data plugin must have at least one output" unless configuration[:outputs]
    187     Generators::DataGenerator.new(configuration[:pluginname], configuration[:outputs], configuration[:pluginname],
    188                                    configuration[:description], configuration[:author], configuration[:license],
    189                                    configuration[:version], configuration[:url], configuration[:timeout])
    190   else
    191     raise "invalid plugin type. cannot generate plugin '#{configuration[:target]}'"
    192   end
    193 end

identify_plugin() click to toggle source

If plugintype is StandardDefinition, identify which of the special plugin types we are dealing with based on directory structure. To keep it simple we limit it to one type per target directory. Return the name of the type of plugin as a string

        # File lib/mcollective/application/plugin.rb
    334 def identify_plugin
    335   plugintype = Dir.glob(File.join(configuration[:target], "*")).select do |file|
    336     File.directory?(file) && file.match(/(connector|facts|registration|security|audit|pluginpackager|data|discovery|validator)/)
    337   end
    338
    339   raise RuntimeError, "more than one plugin type detected in directory" if plugintype.size > 1
    340   raise RuntimeError, "no plugins detected in directory" if plugintype.size < 1
    341
    342   File.basename(plugintype[0])
    343 end

info_command() click to toggle source

Display info about plugin

        # File lib/mcollective/application/plugin.rb
    163 def info_command
    164   plugin = prepare_plugin
    165   packager = PluginPackager["#{configuration[:format].capitalize}Packager"]
    166   packager.new(plugin).package_information
    167 end

load_plugin_config_values() click to toggle source

Load preset metadata values from config if they are present This makes it possible to override metadata values in a local client config file.

Example : plugin.metadata.license = Apache 2

        # File lib/mcollective/application/plugin.rb
    350 def load_plugin_config_values
    351   config = Config.instance
    352   [:pluginname, :description, :author, :license, :version, :url, :timeout].each do |confoption|
    353     configuration[confoption] = config.pluginconf["metadata.#{confoption}"] unless configuration[confoption]
    354   end
    355 end

load_plugin_ddl(plugin, type) click to toggle source

`Agents` are just called 'agent' but newer plugin types are called plugin_plugintype for example facter_facts etc so this will first try the old way then the new way.

        # File lib/mcollective/application/plugin.rb
    213 def load_plugin_ddl(plugin, type)
    214   [plugin, "#{plugin}_#{type}"].each do |p|
    215     ddl = DDL.new(p, type, false)
    216     if ddl.findddlfile(p, type)
    217       ddl.loadddlfile
    218       return ddl
    219     end
    220   end
    221
    222   return nil
    223 end

main() click to toggle source

        # File lib/mcollective/application/plugin.rb
    357 def main
    358     abort "No action specified, please run 'mco help plugin' for help" unless configuration.include?(:action)
    359
    360     cmd = "#{configuration[:action]}_command"
    361
    362     if respond_to? cmd
    363       send cmd
    364     else
    365       abort "Invalid action #{configuration[:action]}, please run 'mco help plugin' for help."
    366     end
    367 end

package_command() click to toggle source

Package plugin

        # File lib/mcollective/application/plugin.rb
    196 def package_command
    197   if configuration[:sign] && Config.instance.pluginconf.include?("debian_packager.keyname")
    198     configuration[:sign] = Config.instance.pluginconf["debian_packager.keyname"]
    199     configuration[:sign] = "\"#{configuration[:sign]}\"" unless configuration[:sign].match(/\".*\"/)
    200   end
    201
    202   plugin = prepare_plugin
    203   (configuration[:pluginpath] = configuration[:pluginpath] + "/") if (configuration[:pluginpath] && !configuration[:pluginpath].match(/^.*\/$/))
    204   packager = PluginPackager["#{configuration[:format].capitalize}Packager"]
    205   packager.new(plugin, configuration[:pluginpath], configuration[:sign],
    206                options[:verbose], configuration[:keep_artifacts],
    207                configuration[:module_template]).create_packages
    208 end

plugin_directory_exists?(plugin_type) click to toggle source

        # File lib/mcollective/application/plugin.rb
    313 def plugin_directory_exists?(plugin_type)
    314   File.directory?(File.join(PluginPackager.get_plugin_path(configuration[:target]), plugin_type))
    315 end

post_option_parser(configuration) click to toggle source

Handle alternative format that optparser can't parse.

        # File lib/mcollective/application/plugin.rb
    136 def post_option_parser(configuration)
    137   if ARGV.length >= 1
    138     configuration[:action] = ARGV.delete_at(0)
    139
    140     configuration[:target] = ARGV.delete_at(0) || "."
    141
    142     if configuration[:action] == "generate"
    143       unless ARGV[0] && ARGV[0].match(/(actions|outputs)=(.+)/i)
    144         unless configuration[:pluginname]
    145           configuration[:pluginname] = ARGV.delete_at(0)
    146         else
    147           ARGV.delete_at(0)
    148         end
    149       end
    150
    151       ARGV.each do |argument|
    152         if argument.match(/(actions|outputs)=(.+)/i)
    153           configuration[$1.downcase.to_sym]= $2.split(",")
    154         else
    155           raise "Could not parse --arg '#{argument}'"
    156         end
    157       end
    158     end
    159   end
    160 end

prepare_plugin() click to toggle source

Creates the correct package plugin object.

        # File lib/mcollective/application/plugin.rb
    290 def prepare_plugin
    291   plugintype = set_plugin_type unless configuration[:plugintype]
    292   configuration[:format] = "ospackage" unless configuration[:format]
    293   PluginPackager.load_packagers
    294   plugin_class = PluginPackager[configuration[:plugintype]]
    295
    296   if configuration[:dependency] && configuration[:dependency].size == 1
    297     configuration[:dependency] = configuration[:dependency][0].split(" ")
    298   elsif configuration[:dependency]
    299     configuration[:dependency].map!{|dep| {:name => dep, :version => nil}}
    300   end
    301
    302   mcdependency = {:mcname => configuration[:mcname], :mcversion => configuration[:mcversion]}
    303
    304   # Deprecation warning for --iteration
    305   if configuration[:iteration]
    306     puts 'Warning. The --iteration flag has been deprecated. Please use --revision instead.'
    307     configuration[:revision] = configuration[:iteration] unless configuration[:revision]
    308   end
    309
    310   plugin_class.new(configuration, mcdependency, plugintype)
    311 end

set_plugin_type() click to toggle source

Identify plugin type if not provided.

        # File lib/mcollective/application/plugin.rb
    318 def set_plugin_type
    319   if plugin_directory_exists?("agent") || plugin_directory_exists?("application")
    320     configuration[:plugintype] = "AgentDefinition"
    321     return "Agent"
    322   elsif plugin_directory_exists?(plugintype = identify_plugin)
    323     configuration[:plugintype] = "StandardDefinition"
    324     return plugintype
    325   else
    326     raise RuntimeError, "target directory is not a valid mcollective plugin"
    327   end
    328 end