3 class ModulepackagePackager
6 def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil)
7 assert_new_enough_puppet
9 @package_name = "#{@plugin.mcname}_#{@plugin.metadata[:name]}".gsub(/-/, '_')
11 @keep_artifacts = keep_artifacts
12 @module_template = module_template || File.join(File.dirname(__FILE__), 'templates', 'module')
16 # - create module directory
17 # - run 'puppet module build'
18 # - move generated package back to cwd
21 puts "Building module for #{@package_name} plugin."
23 @tmpdir = Dir.mktmpdir('mcollective_packager')
28 puts "Completed building module for #{@package_name} plugin."
31 puts 'Keeping build artifacts'
32 puts "Build artifacts saved - #{@tmpdir}"
41 def assert_new_enough_puppet
42 unless PluginPackager.command_available?('puppet')
43 raise("Cannot build package. 'puppet' is not present on the system.")
46 s = Shell.new('puppet --version')
48 actual_version = s.stdout.chomp
50 required_version = '3.3.0'
51 if Util.versioncmp(actual_version, required_version) < 0
52 raise("Cannot build package. puppet #{required_version} or greater required. We have #{actual_version}.")
57 targetdir = File.join(@tmpdir, 'manifests')
58 FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir)
60 # for each subpackage make a subclass
61 @plugin.packagedata.each do |klass,data|
62 data[:files].each do |file|
63 relative_path = file.sub(/^\.\//, '')
64 targetdir = File.join(@tmpdir, 'files', klass.to_s, 'mcollective', File.dirname(relative_path))
65 FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir)
66 FileUtils.cp_r(file, targetdir)
70 render_template('_manifest.pp.erb', File.join(@tmpdir, 'manifests', "#{klass}.pp"))
73 # render all the templates we have
74 Dir.glob(File.join(@module_template, '*.erb')).each do |template|
75 filename = File.basename(template, '.erb')
76 next if filename =~ /^_/ # starting with underscore makes it private
77 render_template("#{filename}.erb", File.join(@tmpdir, filename))
81 def render_template(template, path)
83 erb = ERB.new(File.read(File.join(@module_template, template)), nil, '-')
84 File.open(path, 'w') do |f|
85 f.puts erb.result(binding)
88 puts "Could not render template to path - '#{path}'"
95 PluginPackager.execute_verbosely(@verbose) do
97 PluginPackager.safe_system('puppet module build')
101 puts 'Build process has failed'
106 # Move built package to cwd
109 package_file = File.join(@tmpdir, 'pkg', "#{@plugin.vendor}-#{@package_name}-#{@plugin.metadata[:version]}.tar.gz")
110 FileUtils.cp(package_file, '.')
112 puts 'Could not copy package to working directory'
119 FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
121 puts "Could not remove temporary build directory - '#{@tmpdir}'"