Class: VagrantPlugins::ProviderZone::Action::Package
- Inherits:
-
Object
- Object
- VagrantPlugins::ProviderZone::Action::Package
- Defined in:
- lib/vagrant-zones/action/package.rb
Overview
This is used to package the VM into a box
Instance Method Summary collapse
- #assemble_box(boxname, tmp_dir) ⇒ Object
- #call(env) ⇒ Object
- #execute ⇒ Object
-
#initialize(app, env) ⇒ Package
constructor
A new instance of Package.
- #snapshot_create(datasetpath, datetime, uii, config) ⇒ Object
- #snapshot_delete(datasetpath, uii, datetime) ⇒ Object
- #snapshot_send(datasetpath, destination, datetime, uii, config) ⇒ Object
Constructor Details
#initialize(app, env) ⇒ Package
Returns a new instance of Package.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/vagrant-zones/action/package.rb', line 12 def initialize(app, env) @logger = Log4r::Logger.new('vagrant_zones::action::import') @app = app @executor = Executor::Exec.new @pfexec = if Process.uid.zero? '' elsif system('sudo -v') 'sudo' else 'pfexec' end env['package.output'] ||= 'package.box' end |
Instance Method Details
#assemble_box(boxname, tmp_dir) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/vagrant-zones/action/package.rb', line 124 def assemble_box(boxname, tmp_dir) is_linux = `bash -c '[[ "$(uname -a)" =~ "Linux" ]]'` Dir.chdir(tmp_dir) do files = Dir.glob(File.join('.', '*')) tar_command = is_linux ? 'tar -cvzf' : 'tar -cvzEf' `#{tar_command} ../#{boxname} #{files.join(' ')}` end end |
#call(env) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/vagrant-zones/action/package.rb', line 30 def call(env) boxname = env['package.output'] mpc = env[:machine].provider_config files = {} raise "#{boxname}: Already exists" if File.exist?(boxname) ## Create Snapshot FileUtils.mkdir_p("#{Dir.pwd}/_tmp_package") datasetpath = "#{mpc.boot['array']}/#{mpc.boot['dataset']}/#{env[:machine].name}" datetime = Time.new.strftime('%Y-%m-%d-%H:%M:%S') snapshot_create(datasetpath, datetime, env[:ui], mpc) snapshot_send(datasetpath, "#{Dir.pwd}/_tmp_package/box.zss", datetime, env[:ui], mpc) ## Include User Extra Files env['package.include'].each do |file| source = Pathname.new(file) dest = if source.relative? source else source.basename end files[file] = dest end # Verify the mapping files.each_key do |from| raise Vagrant::Errors::PackageIncludeMissing, file: from unless File.exist?(from) end files.each do |from, dest| include_directory = Pathname.new("#{Dir.pwd}/_tmp_package/") to = include_directory.join(dest) FileUtils.mkdir_p(to.parent) if File.directory?(from) FileUtils.cp_r(Dir.glob(from), to.parent, preserve: true) else FileUtils.cp(from, to, preserve: true) end end ## Create a Vagrantfile or load from Users Defined File vagrantfile_content = <<~'CODE' require 'yaml' require_relative 'Hosts' settings = YAML::load(File.read("#{File.dirname(__FILE__)}/Hosts.yml")) Vagrant.configure("2") do |config| Hosts.configure(config, settings) end CODE File.write("#{Dir.pwd}/_tmp_package/Vagrantfile", vagrantfile_content) files[env['package.vagrantfile']] = '_Vagrantfile' if env['package.vagrantfile'] info_content_hash = { 'boxname' => boxname, 'Author' => mpc.vagrant_cloud_creator, 'Vagrant-Zones' => 'This box was built with Vagrant-Zones: https://github.com/STARTcloud/vagrant-zones' } File.write("#{Dir.pwd}/_tmp_package/info.json", info_content_hash.to_json) = { 'provider' => 'zone', 'architecture' => 'amd64', 'brand' => mpc.brand, 'format' => 'zss', 'url' => "https://app.vagrantup.com/#{mpc.vagrant_cloud_creator}/boxes/#{mpc.boxshortname}" } ['kernel'] = mpc.kernel if !mpc.kernel.nil? && mpc.kernel != false File.write("#{Dir.pwd}/_tmp_package/metadata.json", .to_json) ## Create the Box file assemble_box(boxname, "#{Dir.pwd}/_tmp_package") FileUtils.rm_rf("#{Dir.pwd}/_tmp_package") env[:ui].info("Box created, You can now add the box: 'vagrant box add #{boxname} --name newbox'") @app.call(env) end |
#execute ⇒ Object
26 27 28 |
# File 'lib/vagrant-zones/action/package.rb', line 26 def execute(...) @executor.execute(...) end |
#snapshot_create(datasetpath, datetime, uii, config) ⇒ Object
107 108 109 110 111 |
# File 'lib/vagrant-zones/action/package.rb', line 107 def snapshot_create(datasetpath, datetime, uii, config) uii.info('Creating a Snapshot of the box.') result = execute(true, "#{@pfexec} zfs snapshot -r #{datasetpath}/boot@vagrant_box#{datetime}") uii.info("#{@pfexec} zfs snapshot -r #{datasetpath}/boot@vagrant_box#{datetime}") if result.zero? && config.debug end |
#snapshot_delete(datasetpath, uii, datetime) ⇒ Object
113 114 115 116 |
# File 'lib/vagrant-zones/action/package.rb', line 113 def snapshot_delete(datasetpath, uii, datetime) result = execute(true, "#{@pfexec} zfs destroy -r -f #{datasetpath}/boot@vagrant_box#{datetime}") uii.info("#{@pfexec} zfs destroy -r -f #{datasetpath}/boot@vagrant_box#{datetime}") if result.zero? && config.debug end |
#snapshot_send(datasetpath, destination, datetime, uii, config) ⇒ Object
118 119 120 121 122 |
# File 'lib/vagrant-zones/action/package.rb', line 118 def snapshot_send(datasetpath, destination, datetime, uii, config) uii.info('Sending Snapshot to ZFS Send Stream image.') result = execute(true, "#{@pfexec} zfs send #{datasetpath}/boot@vagrant_box#{datetime} > #{destination}") uii.info("#{@pfexec} zfs send -r #{datasetpath}/boot@vagrant_box#{datetime} > #{destination}") if result.zero? && config.debug end |