Class: VagrantZFS::Action::VM::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_zfs/action/vm/import.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Import

Returns a new instance of Import.



5
6
7
8
9
# File 'lib/vagrant_zfs/action/vm/import.rb', line 5

def initialize(app, env)
  @logger   = Log4r::Logger.new("vagrant_zfs::action::vm::import")
  @env = env
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/vagrant_zfs/action/vm/import.rb', line 19

def call(env)
  env[:ui].info I18n.t("vagrant.actions.vm.import.importing", :name => env[:vm].box.name)

  # Import the virtual machine
  ovf_file = env[:vm].box.directory.join("box.ovf").to_s

  fs = find_basebox_filesystem

  instance_name = env[:root_path].basename.to_s + "_#{Time.now.to_i}"
  env[:name] = instance_name

  VagrantZFS::ZFS.snapshot fs, instance_name

  user_home = ENV['HOME']
  vagrant_home = "#{user_home}/.vagrant.d"
  instance_root = vagrant_home + "/instances"

  clonename = "#{fs}/#{instance_name}"
  mountpoint = "#{instance_root}/#{instance_name}"
  VagrantZFS::ZFS.clone! "#{fs}@#{instance_name}", clonename
  VagrantZFS::ZFS.set_mountpoint clonename, mountpoint

  env[:vm].uuid = VagrantZFS::VBoxManage.createvm instance_name, instance_root
  hdd = instance_root + "/" + instance_name + "/box-disk1.vmdk"
  VagrantZFS::VBoxManage.setup env[:vm].uuid, hdd

  # # If we got interrupted, then the import could have been
  # # interrupted and its not a big deal. Just return out.
  # return if env[:interrupted]

  # Flag as erroneous and return if import failed
  raise Errors::VMImportFailure if !env[:vm].uuid

  # # Import completed successfully. Continue the chain
  @app.call(env)
end

#find_basebox_filesystemObject



11
12
13
14
15
16
17
# File 'lib/vagrant_zfs/action/vm/import.rb', line 11

def find_basebox_filesystem
  fs = VagrantZFS::ZFS.mounts.select do |mountpoint,fs|
    mountpoint == @env[:vm].box.directory.to_s
  end.first[1]
  @logger.debug "Found base box filesystem: #{fs}"
  fs
end

#recover(env) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant_zfs/action/vm/import.rb', line 56

def recover(env)
  if env[:vm].created?
    return if env["vagrant.error"].is_a?(Errors::VagrantError)

    # Interrupted, destroy the VM. We note that we don't want to
    # validate the configuration here.
    destroy_env = env.clone
    destroy_env[:validate] = false
    env[:action_runner].run(:destroy, destroy_env)
  end
end