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

Raises:

  • (Error::VSphereError)


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
# 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
  dc = get_datacenter connection, machine
  template = dc.find_vm config.template_name

  raise Error::VSphereError, :message => I18n.t('vsphere.errors.missing_template') if template.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: #{config.template_name}"
    env[:ui].info " -- Name: #{name}"

    new_vm = template.CloneVM_Task(:folder => template.parent, :name => name, :spec => spec).wait_for_completion
  rescue Exception => e
    puts e.message
    raise Errors::VSphereError, :message => 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