From 1099e9af9ae61e40d6324fd80bbc2a7557064ad9 Mon Sep 17 00:00:00 2001 From: JohnU Date: Tue, 8 Oct 2013 18:16:14 -0500 Subject: [PATCH] Adding ability to pass hypervisor as a command line argument --- README.rdoc | 1 + lib/chef/knife/vsphere_vm_clone.rb | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 75bda13f..81e8d232 100644 --- a/README.rdoc +++ b/README.rdoc @@ -105,6 +105,7 @@ e.g. knife vsphere vm clone NewNode UbuntuTemplate --cspec StaticSpec \ --no-host-key-verify - Disable host key verification --json-attributes - A JSON string to be added to the first run of chef-client --disable-customization - Disable default customization + --hypervisor - The hypervisor into which to put the cloned VM Clones an existing VM template into a new VM instance, optionally applying an existing customization specification. diff --git a/lib/chef/knife/vsphere_vm_clone.rb b/lib/chef/knife/vsphere_vm_clone.rb index 36b1dfe7..d2b580b4 100644 --- a/lib/chef/knife/vsphere_vm_clone.rb +++ b/lib/chef/knife/vsphere_vm_clone.rb @@ -39,6 +39,10 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand :description => "The source VM / Template to clone from", :required => true + option :hypervisor, + :long => "--hypervisor IP", + :description => "The hypervisor into which to put the cloned VM" + option :annotation, :long => "--annotation TEXT", :description => "Add TEXT in Notes field from annotation" @@ -262,9 +266,11 @@ def generate_clone_spec (src_config) rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => find_pool(get_config(:resource_pool))) else dcname = get_config(:vsphere_dc) + hypervisor_ip = config[:hypervisor] dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found" hosts = find_all_in_folder(dc.hostFolder, RbVmomi::VIM::ComputeResource) - rp = hosts.first.resourcePool + host = hosts.select{|host| host.name == hypervisor_ip}.first || hosts.first + rp = host.resourcePool rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp) end