Class: Support::CloneVm
- Inherits:
-
Object
- Object
- Support::CloneVm
- Defined in:
- lib/support/clone_vm.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#vim ⇒ Object
readonly
Returns the value of attribute vim.
Instance Method Summary collapse
- #clone ⇒ Object
-
#initialize(conn_opts, options) ⇒ CloneVm
constructor
A new instance of CloneVm.
Constructor Details
#initialize(conn_opts, options) ⇒ CloneVm
Returns a new instance of CloneVm.
7 8 9 10 11 12 |
# File 'lib/support/clone_vm.rb', line 7 def initialize(conn_opts, ) @options = # Connect to vSphere @vim ||= RbVmomi::VIM.connect conn_opts end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/support/clone_vm.rb', line 5 def @options end |
#vim ⇒ Object (readonly)
Returns the value of attribute vim.
5 6 7 |
# File 'lib/support/clone_vm.rb', line 5 def vim @vim end |
Instance Method Details
#clone ⇒ Object
14 15 16 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/support/clone_vm.rb', line 14 def clone # set the datacenter name dc = vim.serviceInstance.find_datacenter([:datacenter]) src_vm = dc.find_vm([:template]) raise format("Unable to find template: %s", [:template]) if src_vm.nil? # Specify where the machine is going to be created relocate_spec = RbVmomi::VIM.VirtualMachineRelocateSpec relocate_spec.host = [:targethost] # Change to delta disks for linked clones relocate_spec.diskMoveType = :moveChildMostDiskBacking if [:clone_type] == :linked # Set the resource pool relocate_spec.pool = [:resource_pool] clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(location: relocate_spec, powerOn: [:poweron], template: false) # Set the folder to use dest_folder = [:folder].nil? ? src_vm.parent : [:folder][:id] puts "Cloning the template '#{[:template]}' to create the VM..." task = src_vm.CloneVM_Task(folder: dest_folder, name: [:name], spec: clone_spec) task.wait_for_completion # get the IP address of the machine for bootstrapping # machine name is based on the path, e.g. that includes the folder name = [:folder].nil? ? [:name] : format("%s/%s", [:folder][:name], [:name]) new_vm = dc.find_vm(name) if new_vm.nil? puts format("Unable to find machine: %s", name) else puts "Waiting for network interfaces to become available..." sleep 2 while new_vm.guest.net.empty? || !new_vm.guest.ipAddress new_vm.guest.net[0].ipConfig.ipAddress.detect do |addr| addr.origin != "linklayer" end.ipAddress end end |