3 class DebpackagePackager
6 def initialize(plugin, pluginpath = nil, signature = nil, verbose = false, keep_artifacts = nil, module_template = nil)
7 if PluginPackager.command_available?('debuild')
10 @libdir = pluginpath || '/usr/share/mcollective/plugins/mcollective/'
11 @signature = signature
12 @package_name = "#{@plugin.mcname}-#{@plugin.metadata[:name]}"
13 @keep_artifacts = keep_artifacts
15 raise("Cannot build package. 'debuild' is not present on the system.")
21 # - craete buildroot/debian
22 # - create the relative directories with package contents
23 # - create install files for each of the plugins that are going to be built
24 # - create debian build files
26 # - create pre and post install files
27 # - run the build script
28 # - move packages to cwd
32 puts "Building packages for #{@package_name} plugin."
34 @tmpdir = Dir.mktmpdir('mcollective_packager')
35 @build_dir = File.join(@tmpdir, "#{@package_name}_#{@plugin.metadata[:version]}")
39 @plugin.packagedata.each do |type, data|
41 create_install_file(type, data)
42 create_pre_and_post_install(type)
49 puts "Completed building all packages for #{@package_name} plugin."
52 puts 'Keeping build artifacts.'
53 puts "Build artifacts saved - #{@tmpdir}"
55 puts 'Removing build artifacts.'
63 def create_debian_files
64 ['control', 'Makefile', 'compat', 'rules', 'copyright', 'changelog'].each do |f|
70 FileUtils.cd(@build_dir) do
71 PluginPackager.execute_verbosely(@verbose) do
73 if @signature.is_a?(String)
74 PluginPackager.safe_system("debuild --no-lintian -i -k#{@signature}")
76 PluginPackager.safe_system("debuild --no-lintian -i")
79 PluginPackager.safe_system("debuild --no-lintian -i -us -uc")
85 # Creates a string used by the control file to specify dependencies
86 # Dependencies can be formatted as :
90 def build_dependency_string(data)
92 PluginPackager.filter_dependencies('debian', data[:dependencies]).each do |dep|
93 if dep[:version] && dep[:revision]
94 dependencies << "#{dep[:name]} (>=#{dep[:version]}-#{dep[:revision]})"
96 dependencies << "#{dep[:name]} (>=#{dep[:version]})"
98 dependencies << dep[:name]
102 if data[:plugindependency]
103 dependencies << "#{data[:plugindependency][:name]} (= ${binary:Version})"
106 dependencies.join(', ')
109 # Creates an install file for each of the packages that are going to be created
111 def create_install_file(type, data)
112 install_file = "#{@package_name}-#{type}"
114 install_file = File.join(@build_dir, 'debian', "#{install_file}.install")
115 File.open(install_file, 'w') do |file|
116 data[:files].each do |f|
117 extended_filename = File.join(@libdir, File.expand_path(f).gsub(/^#{@plugin.target_path}/, ''))
118 file.puts "#{extended_filename} #{File.dirname(extended_filename)}"
121 rescue Errno::EACCES => e
122 puts "Could not create install file '#{install_file}'. Permission denied"
125 puts "Could not create install file '#{install_file}'."
130 # Move source package and debs to cwd
133 files_to_copy = Dir.glob(File.join(@tmpdir, '*.{deb,dsc,diff.gz,orig.tar.gz,changes}'))
134 FileUtils.cp(files_to_copy, '.')
136 puts 'Could not copy packages to working directory.'
141 # Create pre and post install files in $buildroot/debian
142 # from supplied scripts.
143 # Note that all packages built for the plugin will invoke
144 # the same pre and post install scripts.
145 def create_pre_and_post_install(type)
146 if @plugin.preinstall
147 if !File.exists?(@plugin.preinstall)
148 puts "pre-install script '#{@plugin.preinstall}' not found."
149 raise(Errno::ENOENT, @plugin.preinstall)
151 FileUtils.cp(@plugin.preinstall, File.join(@build_dir, 'debian', "#{@package_name}-#{type}.preinst"))
155 if @plugin.postinstall
156 if !File.exists?(@plugin.postinstall)
157 puts "post-install script '#{@plugin.postinstall}' not found."
158 raise(Errno::ENOENT, @plugin.postinstall)
160 FileUtils.cp(@plugin.postinstall, File.join(@build_dir, 'debian', "#{@package_name}-#{type}.postinst"))
166 # Expects directory : $mcollective-$agent_$version
167 # Creates file : $buildroot/$mcollective-$agent_$version.orig.tar.gz
169 name_and_version = "#{@package_name}_#{@plugin.metadata[:version]}"
170 tarfile = "#{name_and_version}.orig.tar.gz"
172 PluginPackager.execute_verbosely(@verbose) do
173 Dir.chdir(@tmpdir) do
174 PluginPackager.safe_system("tar -Pcvzf #{File.join(@tmpdir, tarfile)} #{name_and_version}")
177 rescue Exception => e
178 puts "Could not create tarball - #{tarfile}"
183 def create_file(filename)
185 file = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', 'debian', "#{filename}.erb")), nil, '-')
186 File.open(File.join(@build_dir, 'debian', filename), 'w') do |f|
187 f.puts file.result(binding)
190 puts "Could not create file - '#{filename}'"
195 # Move files contained in the plugin to the correct directory
196 # relative to the build root.
197 def prepare_tmpdirs(data)
198 data[:files].each do |file|
200 targetdir = File.join(@build_dir, @libdir, File.dirname(File.expand_path(file)).gsub(/^#{@plugin.target_path}/, ""))
201 FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir)
202 FileUtils.cp_r(file, targetdir)
203 rescue Errno::EACCES => e
204 puts "Could not create directory '#{targetdir}'. Permission denied"
206 rescue Errno::ENOENT => e
207 puts "Could not copy file '#{file}' to '#{targetdir}'. File does not exist"
210 puts 'Could not prepare build directory'
216 # Create the $buildroot/debian directory
217 def create_debian_dir
218 deb_dir = File.join(@build_dir, 'debian')
220 FileUtils.mkdir_p(deb_dir)
222 puts "Could not create directory '#{deb_dir}'"
229 FileUtils.rm_r(@tmpdir) if File.directory?(@tmpdir)
231 puts "Could not remove temporary build directory - '#{@tmpdir}'"