3 class RpmpackagePackager
6 def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil)
7 if @buildtool = select_command
9 @package_name = "#{@plugin.mcname}-#{@plugin.metadata[:name]}"
10 @package_name_and_version = "#{@package_name}-#{@plugin.metadata[:version]}"
12 @libdir = pluginpath || '/usr/libexec/mcollective/mcollective/'
13 @signature = signature
16 @keep_artifacts = keep_artifacts
18 raise("Cannot build package. 'rpmbuild' or 'rpmbuild-md5' is not present on the system")
22 # Determine the build tool present on the system
24 if PluginPackager.command_available?('rpmbuild-md5')
26 elsif PluginPackager.command_available?('rpmbuild')
34 `rpm --eval '%_rpmdir'`.chomp
38 `rpm --eval '%_srcrpmdir'`.chomp
42 # - create temporary buildroot
43 # - create the spec file
44 # - create the tarball
45 # - run the build script
46 # - move pacakges to cwd
50 puts "Building packages for #{@package_name} plugin."
52 @tmpdir = Dir.mktmpdir('mcollective_packager')
59 puts "Completed building all packages for #{@package_name} plugin."
62 puts 'Keeping build artifacts'
63 puts "Build artifacts saved - #{@tmpdir}"
75 PluginPackager.execute_verbosely(@verbose) do
76 PluginPackager.safe_system("#{@buildtool} -ta#{" --quiet" unless @verbose}#{" --sign" if @signature} #{tarfile}")
79 puts 'Build process has failed'
85 # Expects directory $mcollective-$agent-$version
86 # Creates file : $tmpbuildroot/$mcollective-$agent-$version
88 tarfile = File.join(@tmpdir, "#{@package_name_and_version}.tgz")
90 PluginPackager.execute_verbosely(@verbose) do
92 PluginPackager.safe_system("tar -cvzf #{tarfile} #{@package_name_and_version}")
96 puts "Could not create tarball - '#{tarfile}'"
102 # Move rpm's and srpm's to cwd
105 files_to_copy = Dir.glob(File.join(@rpmdir, 'noarch', "#{@package_name}-*-#{@plugin.metadata[:version]}-#{@plugin.revision}.noarch.rpm"))
106 files_to_copy << File.join(@srpmdir, "#{@package_name}-#{@plugin.metadata[:version]}-#{@plugin.revision}.src.rpm")
107 FileUtils.cp(files_to_copy, '.')
109 puts 'Could not copy packages to working directory'
114 # Create the specfile and place as $tmpbuildroot/$mcollective-$agent-$version/$mcollective-$agent-$version.spec
116 spec_file = File.join(@tmpdir, @package_name_and_version, "#{@package_name_and_version}.spec")
118 spec_template = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', 'redhat', 'rpm_spec.erb')), nil, '-')
119 File.open(spec_file, 'w') do |f|
120 f.puts spec_template.result(binding)
123 puts "Could not create specfile - '#{spec_file}'"
128 # Move files contained in the plugin to the correct directory
129 # relative to the build root.
131 plugin_files.each do |file|
133 targetdir = File.join(@tmpdir, @package_name_and_version, @libdir, File.dirname(File.expand_path(file)).gsub(@plugin.target_path, ""))
134 FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir)
135 FileUtils.cp_r(file, targetdir)
136 rescue Errno::EACCES => e
137 puts "Could not create directory '#{targetdir}'. Permission denied"
139 rescue Errno::ENOENT => e
140 puts "Could not copy file '#{file}' to '#{targetdir}'. File does not exist"
143 puts 'Could not prepare temporary build directory'
149 # Extract all the package files from the plugin's package data hash
152 @plugin.packagedata.each do |name, data|
153 files += data[:files].reject{ |f| File.directory?(f) }
158 # Extract the package specific files from the file list and omits directories
159 def package_files(files)
162 if !File.directory?(f)
163 package_files << File.join(@libdir, File.expand_path(f).gsub(/#{@plugin.target_path}|\.\//, ''))
171 FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
173 puts "Could not remove temporary build directory - '#{@tmpdir}'"