2 class Application::Plugin<Application
4 exclude_argument_sections "common", "filter", "rpc"
6 description "MCollective Plugin Application"
8 mco plugin package [options] <directory>
9 mco plugin info <directory>
10 mco plugin doc <plugin>
11 mco plugin doc <type/plugin>
12 mco plugin generate agent <pluginname> [actions=val,val]
13 mco plugin generate data <pluginname> [outputs=val,val]
15 info : Display plugin information including package details.
16 package : Create all available plugin packages.
17 doc : Display documentation for a specific plugin.
21 :description => 'Plugin name',
22 :arguments => ['-n', '--name NAME'],
26 :description => 'Post install script',
27 :arguments => ['--postinstall POSTINSTALL'],
31 :description => 'Pre install script',
32 :arguments => ['--preinstall PREINSTALL'],
36 :description => 'Revision number',
37 :arguments => ['--revision REVISION'],
41 :description => 'DEPRECATED - Use --revision instead',
42 :arguments => ['--iteration ITERATION'],
46 :description => 'Vendor name',
47 :arguments => ['--vendor VENDOR'],
51 :description => 'MCollective plugin path',
52 :arguments => ['--pluginpath PATH'],
56 :description => 'MCollective type (mcollective, pe-mcollective) that the packages depend on',
57 :arguments => ['--mcname NAME'],
61 :description => 'Version of MCollective that the packages depend on',
62 :arguments => ['--mcversion MCVERSION'],
66 :description => 'Adds a dependency to the plugin',
67 :arguments => ['--dependency DEPENDENCIES'],
71 :description => 'Package output format. Defaults to rpmpackage or debpackage',
72 :arguments => ['--format OUTPUTFORMAT'],
76 :description => 'Embed a signature in the package',
77 :arguments => ['--sign'],
81 :description => 'Template to use.',
82 :arguments => ['--template HELPTEMPLATE'],
86 :description => 'Plugin description',
87 :arguments => ['--description DESCRIPTION'],
91 :description => 'The author of the plugin',
92 :arguments => ['--author AUTHOR'],
96 :description => 'The license under which the plugin is distributed',
97 :arguments => ['--license LICENSE'],
101 :description => 'The version of the plugin',
102 :arguments => ['--pluginversion VERSION'],
106 :description => 'Url at which information about the plugin can be found',
107 :arguments => ['--url URL'],
111 :description => "The plugin's timeout",
112 :arguments => ['--timeout TIMEOUT'],
116 :description => 'Actions to be generated for an Agent Plugin',
117 :arguments => ['--actions [ACTIONS]'],
121 :description => 'Outputs to be generated for an Data Plugin',
122 :arguments => ['--outputs [OUTPUTS]'],
125 option :keep_artifacts,
126 :dsecription => "Don't remove artifacts after building packages",
127 :arguments => ['--keep-artifacts'],
130 # Handle alternative format that optparser can't parse.
131 def post_option_parser(configuration)
133 configuration[:action] = ARGV.delete_at(0)
135 configuration[:target] = ARGV.delete_at(0) || "."
137 if configuration[:action] == "generate"
138 unless ARGV[0] && ARGV[0].match(/(actions|outputs)=(.+)/i)
139 unless configuration[:pluginname]
140 configuration[:pluginname] = ARGV.delete_at(0)
146 ARGV.each do |argument|
147 if argument.match(/(actions|outputs)=(.+)/i)
148 configuration[$1.downcase.to_sym]= $2.split(",")
150 raise "Could not parse --arg '#{argument}'"
157 # Display info about plugin
159 plugin = prepare_plugin
160 packager = PluginPackager["#{configuration[:format].capitalize}Packager"]
161 packager.new(plugin).package_information
164 # Generate a plugin skeleton
166 raise "undefined plugin type. cannot generate plugin. valid types are 'agent' and 'data'" if configuration["target"] == '.'
168 unless configuration[:pluginname]
169 puts "No plugin name specified. Using 'new_plugin'"
170 configuration[:pluginname] = "new_plugin"
173 load_plugin_config_values
175 case configuration[:target].downcase
177 Generators::AgentGenerator.new(configuration[:pluginname], configuration[:actions], configuration[:pluginname],
178 configuration[:description], configuration[:author], configuration[:license],
179 configuration[:version], configuration[:url], configuration[:timeout])
181 raise "data plugin must have at least one output" unless configuration[:outputs]
182 Generators::DataGenerator.new(configuration[:pluginname], configuration[:outputs], configuration[:pluginname],
183 configuration[:description], configuration[:author], configuration[:license],
184 configuration[:version], configuration[:url], configuration[:timeout])
186 raise "invalid plugin type. cannot generate plugin '#{configuration[:target]}'"
192 if configuration[:sign] && Config.instance.pluginconf.include?("debian_packager.keyname")
193 configuration[:sign] = Config.instance.pluginconf["debian_packager.keyname"]
194 configuration[:sign] = "\"#{configuration[:sign]}\"" unless configuration[:sign].match(/\".*\"/)
197 plugin = prepare_plugin
198 (configuration[:pluginpath] = configuration[:pluginpath] + "/") if (configuration[:pluginpath] && !configuration[:pluginpath].match(/^.*\/$/))
199 packager = PluginPackager["#{configuration[:format].capitalize}Packager"]
200 packager.new(plugin, configuration[:pluginpath], configuration[:sign], options[:verbose], configuration[:keep_artifacts]).create_packages
203 # Agents are just called 'agent' but newer plugin types are
204 # called plugin_plugintype for example facter_facts etc so
205 # this will first try the old way then the new way.
206 def load_plugin_ddl(plugin, type)
207 [plugin, "#{plugin}_#{type}"].each do |p|
208 ddl = DDL.new(p, type, false)
209 if ddl.findddlfile(p, type)
218 # Show application list and plugin help
220 known_plugin_types = [["Agents", :agent], ["Aggregate", :aggregate], ["Data Queries", :data], ["Discovery Methods", :discovery], ["Validator Plugins", :validator]]
222 if configuration.include?(:target) && configuration[:target] != "."
223 if configuration[:target] =~ /^(.+?)\/(.+)$/
224 ddl = load_plugin_ddl($2.to_sym, $1)
226 found_plugin_type = nil
228 known_plugin_types.each do |plugin_type|
229 PluginManager.find(plugin_type[1], "ddl").each do |ddl|
230 pluginname = ddl.gsub(/_#{plugin_type[1]}$/, "")
231 if pluginname == configuration[:target]
232 abort "Duplicate plugin name found, please specify a full path like agent/rpcutil" if found_plugin_type
233 found_plugin_type = plugin_type[1]
238 abort "Could not find a plugin named '%s' in any supported plugin type" % configuration[:target] unless found_plugin_type
239 ddl = load_plugin_ddl(configuration[:target], found_plugin_type)
243 puts ddl.help(configuration[:rpctemplate])
245 abort "Could not find a '%s' plugin named '%s'" % configuration[:target].split('/')
249 puts "Please specify a plugin. Available plugins are:"
254 known_plugin_types.each do |plugin_type|
255 puts "%s:" % plugin_type[0]
257 PluginManager.find(plugin_type[1], "ddl").each do |ddl|
259 help = DDL.new(ddl, plugin_type[1])
260 pluginname = ddl.gsub(/_#{plugin_type[1]}$/, "")
261 puts " %-25s %s" % [pluginname, help.meta[:description]]
263 load_errors << [plugin_type[1], ddl, e]
270 unless load_errors.empty?
271 puts "Plugin Load Errors:"
273 load_errors.each do |e|
274 puts " %-25s %s" % ["#{e[0]}/#{e[1]}", Util.colorize(:yellow, e[2])]
280 # Creates the correct package plugin object.
282 plugintype = set_plugin_type unless configuration[:plugintype]
283 configuration[:format] = "ospackage" unless configuration[:format]
284 PluginPackager.load_packagers
285 plugin_class = PluginPackager[configuration[:plugintype]]
287 if configuration[:dependency] && configuration[:dependency].size == 1
288 configuration[:dependency] = configuration[:dependency][0].split(" ")
289 elsif configuration[:dependency]
290 configuration[:dependency].map!{|dep| {:name => dep, :version => nil}}
293 mcdependency = {:mcname => configuration[:mcname], :mcversion => configuration[:mcversion]}
295 #Deprecation warning for --iteration
296 if configuration[:iteration]
297 puts 'Warning. The --iteration flag has been deprecated. Please use --revision instead.'
298 configuration[:revision] = configuration[:iteration] unless configuration[:revision]
301 plugin_class.new(configuration, mcdependency, plugintype)
304 def directory_for_type(type)
305 File.directory?(File.join(configuration[:target], type))
308 # Identify plugin type if not provided.
310 if directory_for_type("agent") || directory_for_type("application")
311 configuration[:plugintype] = "AgentDefinition"
313 elsif directory_for_type(plugintype = identify_plugin)
314 configuration[:plugintype] = "StandardDefinition"
317 raise RuntimeError, "target directory is not a valid mcollective plugin"
321 # If plugintype is StandardDefinition, identify which of the special
322 # plugin types we are dealing with based on directory structure.
323 # To keep it simple we limit it to one type per target directory.
325 plugintype = Dir.glob(File.join(configuration[:target], "*")).select do |file|
326 File.directory?(file) && file.match(/(connector|facts|registration|security|audit|pluginpackager|data|discovery|validator)/)
329 raise RuntimeError, "more than one plugin type detected in directory" if plugintype.size > 1
330 raise RuntimeError, "no plugins detected in directory" if plugintype.size < 1
332 File.basename(plugintype[0])
335 # Load preset metadata values from config if they are present
336 # This makes it possible to override metadata values in a local
337 # client config file.
339 # Example : plugin.metadata.license = Apache 2
340 def load_plugin_config_values
341 config = Config.instance
342 [:pluginname, :description, :author, :license, :version, :url, :timeout].each do |confoption|
343 configuration[confoption] = config.pluginconf["metadata.#{confoption}"] unless configuration[confoption]
348 abort "No action specified, please run 'mco help plugin' for help" unless configuration.include?(:action)
350 cmd = "#{configuration[:action]}_command"
355 abort "Invalid action #{configuration[:action]}, please run 'mco help plugin' for help."