Class: VagrantPlugins::VSphere::Action::Clone

Inherits:
Object
  • Object
show all
Includes:
Util::MachineHelpers, Util::VimHelpers
Defined in:
lib/vSphere/action/clone.rb

Instance Method Summary collapse

Methods included from Util::MachineHelpers

#wait_for_ssh

Methods included from Util::VimHelpers

#get_customization_spec_info_by_name, #get_datacenter, #get_datastore, #get_resource_pool, #get_vm_by_uuid

Constructor Details

#initialize(app, env) ⇒ Clone

Returns a new instance of Clone.



13
14
15
# File 'lib/vSphere/action/clone.rb', line 13

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
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
55
56
# File 'lib/vSphere/action/clone.rb', line 17

def call(env)
  machine = env[:machine]
  config = machine.provider_config
  connection = env[:vSphere_connection]
  name = get_name machine, config, env[:root_path]
  dc = get_datacenter connection, machine
  template = dc.find_vm config.template_name
  raise Errors::VSphereError, :'missing_template' if template.nil?
  vm_base_folder = get_vm_base_folder dc, template, config
  raise Errors::VSphereError, :'invalid_base_path' if vm_base_folder.nil?

  begin
    location = get_location connection, machine, config, template
    spec = RbVmomi::VIM.VirtualMachineCloneSpec :location => location, :powerOn => true, :template => false
    customization_info = get_customization_spec_info_by_name connection, machine

    spec[:customization] = get_customization_spec(machine, customization_info) unless customization_info.nil?

    env[:ui].info I18n.t('vsphere.creating_cloned_vm')
    env[:ui].info " -- #{config.clone_from_vm ? "Source" : "Template"} VM: #{template.pretty_path}"
    env[:ui].info " -- Target VM: #{vm_base_folder.pretty_path}/#{name}"

    new_vm = template.CloneVM_Task(:folder => vm_base_folder, :name => name, :spec => spec).wait_for_completion
  rescue Errors::VSphereError => e
    raise
  rescue Exception => e
    raise Errors::VSphereError.new, e.message
  end

  #TODO: handle interrupted status in the environment, should the vm be destroyed?

  machine.id = new_vm.config.uuid

  # wait for SSH to be available
  wait_for_ssh env

  env[:ui].info I18n.t('vsphere.vm_clone_success')

  @app.call env
end