Class: Chef::Knife::VsphereVmMove

Inherits:
BaseVsphereCommand show all
Defined in:
lib/chef/knife/vsphere_vm_move.rb

Overview

Moves the VM to another folder or datastore VsphereVmMove extends the BaseVspherecommand

Instance Method Summary collapse

Methods inherited from BaseVsphereCommand

#choose_datastore, common_options, #conn_opts, #datacenter, #fatal_exit, #find_all_in_folder, #find_datastore, #find_datastorecluster, #find_datastores_regex, #find_device, #find_folder, #find_in_folder, #find_network, #find_pool, #find_pool_folder, #find_pools_and_clusters, #get_config, #get_password_from_stdin, #get_path_to_object, #linux?, #number_to_human_size, #password, #tcp_test_port, #tcp_test_port_vm, #traverse_folders_for_computeresources, #traverse_folders_for_dc, #traverse_folders_for_pools, #vim_connection, #windows?

Instance Method Details

#convert_vm(vm) ⇒ Object

Convert VM

Parameters:

  • vm (Object)

    The VM object to convert the VM



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/vsphere_vm_move.rb', line 47

def convert_vm(vm)
  dc = datacenter
  hosts = find_all_in_folder(dc.hostFolder, RbVmomi::VIM::ComputeResource)
  rp = hosts.first.resourcePool
  rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(pool: rp)

  if get_config(:thin_provision)
    puts "Thin provsisioning #{vm.name}"
    rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(datastore: find_datastore(get_config(:datastore)), transform: :sparse)
  end

  if get_config(:thick_provision)
    puts "Thick provsisioning #{vm.name}"
    rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(datastore: find_datastore(get_config(:datastore)), transform: :flat)
  end

  task = vm.RelocateVM_Task(spec: rspec)
  task.wait_for_completion
end

#move_vm(vm) ⇒ Object

Move VM

Parameters:

  • vm (Object)

    The VM object to convert the VM



70
71
72
73
74
75
76
# File 'lib/chef/knife/vsphere_vm_move.rb', line 70

def move_vm(vm)
  dest_name = config[:dest_name] || vm.name
  dest_folder = config[:dest_folder].nil? ? (vm.parent) : (find_folder(get_config(:dest_folder)))

  vm.Rename_Task(newName: dest_name).wait_for_completion unless vm.name == dest_name
  dest_folder.MoveIntoFolder_Task(list: [vm]).wait_for_completion unless vm.parent == dest_folder
end

#runObject

The main run method for vm_move



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef/knife/vsphere_vm_move.rb', line 80

def run
  $stdout.sync = true
  vmname = @name_args[0]
  if vmname.nil?
    show_usage
    fatal_exit("You must specify a virtual machine name")
  end

  vm = get_vm_by_name(vmname, get_config(:folder)) || fatal_exit("Could not find #{vmname}")

  if get_config(:thin_provision) || get_config(:thick_provision)
    convert_vm(vm)
  else
    move_vm(vm)
  end

  puts "VM #{vm.name} moved successfully"
end