class Chef::Knife::VsphereVmMove

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

Public Instance Methods

convert_vm(vm) click to toggle source

Convert VM

@param [Object] vm The VM object to convert the VM

# 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) click to toggle source

Move VM

@param [Object] vm The VM object to convert the VM

# 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
run() click to toggle source

The main run method for vm_move

# 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