From 9c9169ad94b73b253abcd04fde9ad75c365869a6 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 21 Apr 2017 16:49:48 +0100 Subject: [PATCH] Support for --include and --vagrantfile in package action This provides the same behaviour seen in other providers (e.g. virtualbox). Files are placed into a "_include" subdirectory and the user provided Vagrantfile (if any) will be loaded from the main Vagranfile in the box. Also fixed a typo in the `assemble_box` method. --- lib/vagrant-libvirt/action/package_domain.rb | 26 +++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-libvirt/action/package_domain.rb b/lib/vagrant-libvirt/action/package_domain.rb index cb99841..94d3199 100644 --- a/lib/vagrant-libvirt/action/package_domain.rb +++ b/lib/vagrant-libvirt/action/package_domain.rb @@ -39,12 +39,29 @@ module VagrantPlugins # remove hw association with interface # working for centos with lvs default disks `virt-sysprep --no-logfile --operations defaults,-ssh-userdir -a #{@tmp_img}` + # add any user provided file + extra = '' + @tmp_include = @tmp_dir + '/_include' + if env['package.include'] + extra = './_include' + Dir.mkdir(@tmp_include) + env['package.include'].each do |f| + env[:ui].info("Including user file: #{f}") + FileUtils.cp(f, @tmp_include) + end + end + if env['package.vagrantfile'] + extra = './_include' + Dir.mkdir(@tmp_include) unless File.directory?(@tmp_include) + env[:ui].info('Including user Vagrantfile') + FileUtils.cp(env['package.vagrantfile'], @tmp_include + '/Vagrantfile') + end Dir.chdir(@tmp_dir) info = JSON.parse(`qemu-img info --output=json #{@tmp_img}`) img_size = (Float(info['virtual-size'])/(1024**3)).ceil File.write(@tmp_dir + '/metadata.json', metadata_content(img_size)) File.write(@tmp_dir + '/Vagrantfile', vagrantfile_content) - assebmle_box(boxname) + assemble_box(boxname, extra) FileUtils.mv(@tmp_dir + '/' + boxname, '../' + boxname) FileUtils.rm_rf(@tmp_dir) env[:ui].info('Box created') @@ -53,8 +70,8 @@ module VagrantPlugins @app.call(env) end - def assebmle_box(boxname) - `tar cvzf "#{boxname}" --totals ./metadata.json ./Vagrantfile ./box.img` + def assemble_box(boxname, extra) + `tar cvzf "#{boxname}" --totals ./metadata.json ./Vagrantfile ./box.img #{extra}` end def vagrantfile_content @@ -67,6 +84,9 @@ module VagrantPlugins libvirt.storage_pool_name = "default" end end + + user_vagrantfile = File.expand_path('../_include/Vagrantfile', __FILE__) + load user_vagrantfile if File.exists?(user_vagrantfile) EOF end