From 06def27d80a17a6a44afca74a3a492778a619c3f Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Thu, 17 Nov 2016 18:17:12 +0000 Subject: [PATCH] Wait for sys prep is now renamed wait for customization. Customization can occur on other operating systems. You can choose to wait for customization to complete, as not everyone uses VSphere customizations. --- README.md | 4 ++++ lib/vSphere/config.rb | 3 +++ lib/vSphere/driver.rb | 33 +++++++++++++++++++++++++++++---- locales/en.yml | 8 +++----- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4ad6ca26..3eee434d 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,10 @@ This provider has the following settings, all are required unless noted: where the key must start with `guestinfo.`. VMs with VWware Tools installed can retrieve the value of these variables using the `vmtoolsd` command: `vmtoolsd --cmd 'info-get guestinfo.some.variable'`. * `notes` - _Optional_ string - Add arbitrary notes to the VM +* `wait_for_customization` - _Optional_ boolean - Wait for customization to complete before + continuing. Set to false by default. +* `wait_for_customization_timeout` - _Optional_ integer - Timeout in seconds to wait for + customization to complete before continuing. Set to 600 by default. * `management_network_adapter_slot` - _Optional_ integer - zero based array of the card index. This will be the network card to get the ip address from to use for communication between Vagrant and the vm. If this is not set we will use the one detected by VSphere. diff --git a/lib/vSphere/config.rb b/lib/vSphere/config.rb index c66f0b5f..435f5a0f 100644 --- a/lib/vSphere/config.rb +++ b/lib/vSphere/config.rb @@ -124,6 +124,8 @@ def initialize(serial_port_config) attr_accessor :mem_reservation attr_accessor :extra_config attr_accessor :notes + attr_accessor :wait_for_customization + attr_accessor :wait_for_customization_timeout attr_accessor :destroy_unused_network_interfaces attr_accessor :destroy_unused_serial_ports @@ -134,6 +136,7 @@ def initialize(serial_port_config) attr_reader :custom_attributes def initialize + wait_for_customization_timeout = 600 @destroy_unused_network_interfaces = UNSET_VALUE @destroy_unused_serial_ports = UNSET_VALUE @network_adapters = {} diff --git a/lib/vSphere/driver.rb b/lib/vSphere/driver.rb index cdbc5633..ad2b6c52 100644 --- a/lib/vSphere/driver.rb +++ b/lib/vSphere/driver.rb @@ -210,11 +210,36 @@ def clone(root_path) new_vm = task.wait_for_completion end @logger.info("Finished cloning vm #{@machine.id}") + end - config.custom_attributes.each do |k, v| - new_vm.setCustomValue(key: k, value: v) - end + config.custom_attributes.each do |k, v| + new_vm.setCustomValue(key: k, value: v) end + + if config.wait_for_customization + @logger.info I18n.t('vsphere.wait_for_customization') + vem = connection.serviceContent.eventManager + + wait = true + waited_seconds = 0 + sleep_time = 5 + + while wait + events = vem.QueryEvents(filter:RbVmomi::VIM::EventFilterSpec(entity:RbVmomi::VIM::EventFilterSpecByEntity(entity: new_vm, recursion:RbVmomi::VIM::EventFilterSpecRecursionOption(:self)), eventTypeId: ['CustomizationSucceeded'])) + + if events.size > 0 + events.each do |e| + @logger.info e.fullFormattedMessage + end + wait = false + elsif waited_seconds >= config.wait_for_customization_timeout + fail Errors::VSphereError, :'customization_timeout' + else + sleep(sleep_time) + waited_seconds += sleep_time + end + end + end rescue Errors::VSphereError raise #rescue StandardError => e @@ -540,7 +565,7 @@ def get_customization_spec(machine, spec_info) end customization_spec - end + end def get_location(datastore, dc, machine, template) if machine.provider_config.linked_clone diff --git a/locales/en.yml b/locales/en.yml index b4275049..e0e43346 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -20,8 +20,8 @@ en: The VM has not been created vm_not_running: |- The VM is not running - wait_sysprep: |- - Waiting for sysprep + wait_for_customization: |- + Waiting for customization errors: missing_template: |- @@ -50,10 +50,8 @@ en: Cannot use Linked Clone with Storage DRS specified_mangement_interface_does_not_exist: |- specified management interface does not exist - sysprep_timeout: |- + customization_timeout: |- Customization of VM not succeeded within timeout. - wait_for_ip_address_timeout: |- - Timeout while waiting for ip address config: host: |-