Class: HashiCorp::VagrantVMwareDesktop::Action::Import

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/vagrant-vmware-desktop/action/import.rb

Overview

This class “imports” a VMware machine by copying the proper files over to the VM folder.

Instance Method Summary collapse

Methods included from Common

#to_s

Constructor Details

#initialize(app, env) ⇒ Import

Returns a new instance of Import.



17
18
19
20
# File 'lib/vagrant-vmware-desktop/action/import.rb', line 17

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("hashicorp::provider::vmware::import")
end

Instance Method Details

#call(env) ⇒ Object



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
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
106
107
108
109
# File 'lib/vagrant-vmware-desktop/action/import.rb', line 22

def call(env)
  # Create the folder to store the VM
  vm_folder = env[:machine].provider_config.clone_directory
  vm_folder ||= env[:machine].data_dir
  if VagrantVMwareDesktop.wsl? && !VagrantVMwareDesktop.wsl_drvfs_path?(vm_folder) #  !Vagrant::Util::Platform.wsl_windows_access_bypass?(vm_folder)
    @logger.info("import folder location cannot be used due to file system type (#{vm_folder})")
    vm_folder = File.join(VagrantVMwareDesktop.windows_to_wsl_path(Vagrant::Util::Platform.wsl_windows_appdata_local), "vagrant-vmware-desktop")
    @logger.info("import folder location has been updated to supported location: #{vm_folder}")
  end
  vm_folder = File.expand_path(vm_folder, env[:machine].env.root_path)
  vm_folder = Pathname.new(vm_folder)
  vm_folder.mkpath if !vm_folder.directory?
  vm_folder = vm_folder.realpath

  # Create a random name for our import. We protect against some
  # weird theoretical realities where this never gets us a unique
  # name.
  found = false
  10.times do |i|
    temp = vm_folder.join(SecureRandom.uuid)
    if !temp.exist?
      vm_folder = temp
      found = true
      break
    end
  end
  raise Errors::CloneFolderExists if !found
  vm_folder.mkpath

  # TODO: If cloning, we need to verify the clone machine is not running

  # Determine the primary VMX file for the box
  vmx_file = nil
  if env[:clone_id]
    vmx_file = Pathname.new(env[:clone_id])
  else
    vmx_file = env[:machine].box.["vmx_file"]
    if vmx_file
      vmx_file = env[:machine].box.directory.join(vmx_file)
    end

    if !vmx_file
      # Not specified by metadata, attempt to discover VMX file
      @logger.info("VMX file not in metadata, attempting to discover...")

      env[:machine].box.directory.children(true).each do |child|
        if child.basename.to_s =~ /^(.+?)\.vmx$/
          vmx_file = child
          break
        end
      end
    end
  end

  # If we don't have a VMX file, it is an error
  raise Errors::BoxVMXFileNotFound if !vmx_file || !vmx_file.file?

  # Otherwise, log it out and continue
  @logger.debug("Cloning into: #{vm_folder}")
  @logger.info("VMX file: #{vmx_file}")

  # Clone the VM
  clone_name = nil
  clone_name = env[:machine].config.vm.clone if env[:clone_id]
  clone_name = env[:machine].box.name if !clone_name
  env[:ui].info(I18n.t(
    "hashicorp.vagrant_vmware_desktop.cloning",
    :name => clone_name))
  env[:machine].id = env[:machine].provider.driver.clone(vmx_file, vm_folder, env[:machine].provider_config.linked_clone).to_s

  # If we were interrupted, then undo this
  destroy_import(env) if env[:interrupted]

  # Silence!
  env[:machine].provider.driver.suppress_messages

  # Copy the SSH key from the clone machine if we can
  if env[:clone_machine]
    key_path = env[:clone_machine].data_dir.join("private_key")
    if key_path.file?
      FileUtils.cp(
        key_path,
        env[:machine].data_dir.join("private_key"))
    end
  end

  @app.call(env)
end

#destroy_import(env) ⇒ Object

This undoes the import by destroying it.



131
132
133
134
135
136
137
# File 'lib/vagrant-vmware-desktop/action/import.rb', line 131

def destroy_import(env)
  destroy_env = env.dup
  destroy_env.delete(:interrupted)
  destroy_env[:config_validate] = false
  destroy_env[:force_confirm_destroy] = true
  env[:action_runner].run(Action.action_destroy, destroy_env)
end

#recover(env) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vagrant-vmware-desktop/action/import.rb', line 111

def recover(env)
  if env[:machine].provider.state.id != :not_created
    # Ignore errors that Vagrant knows about.
    return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)

    # Return if we already tried to destroyimport
    return if env[:import_destroyed]

    # Return if we're not supposed to destroy
    return if !env[:destroy_on_error]

    # Note that we already tried to destroy so we don't infinite loop
    env[:import_destroyed] = true

    # Undo the import
    destroy_import(env)
  end
end