d0030908095f9c2c779a8710b5e0fd064c61c0f3
[packages/precise/mcollective.git] / lib / mcollective / pluginpackager.rb
1 module MCollective
2   module PluginPackager
3     # Plugin definition classes
4     autoload :AgentDefinition, "mcollective/pluginpackager/agent_definition"
5     autoload :StandardDefinition, "mcollective/pluginpackager/standard_definition"
6
7     # Package implementation plugins
8     def self.load_packagers
9       PluginManager.find_and_load("pluginpackager")
10     end
11
12     def self.[](klass)
13       const_get("#{klass}")
14     end
15
16     # Fetch and return metadata from plugin DDL
17     def self.get_metadata(path, type)
18       ddl = DDL.new("package", type.to_sym, false)
19
20       begin
21         ddl_file = File.read(Dir.glob(File.join(path, type, "*.ddl")).first)
22       rescue Exception
23         raise "failed to load ddl file in plugin directory : #{File.join(path, type)}"
24       end
25       ddl.instance_eval ddl_file
26
27       return ddl.meta, ddl.requirements[:mcollective]
28     end
29
30     # Checks if a directory is present and not empty
31     def self.check_dir_present(path)
32       (File.directory?(path) && !Dir.glob(File.join(path, "*")).empty?)
33     end
34
35     # Quietly calls a block if verbose parameter is false
36     def self.do_quietly?(verbose, &block)
37       unless verbose
38         old_stdout = $stdout.clone
39         $stdout.reopen(File.new("/dev/null", "w"))
40         begin
41           block.call
42         rescue Exception => e
43           $stdout.reopen old_stdout
44           raise e
45         ensure
46           $stdout.reopen old_stdout
47         end
48       else
49         block.call
50       end
51     end
52
53     # Checks if a build tool is present on the system
54     def self.build_tool?(build_tool)
55       ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
56         builder = File.join(path, build_tool)
57         if File.exists?(builder)
58           return true
59         end
60       end
61       false
62     end
63
64     def self.safe_system(*args)
65       raise RuntimeError, "Failed: #{args.join(' ')}" unless system *args
66     end
67   end
68 end