From f89d6fdf9ae15bca9fb0c73dc15e878c538b7926 Mon Sep 17 00:00:00 2001 From: James Augustine Date: Wed, 10 Aug 2016 10:18:46 -0500 Subject: [PATCH] Wait for windows sysprep during clone Machines reboot during the sysprep process. Vagrant needs to wait for this to finish before attempting to access the machine. --- lib/vSphere/action/clone.rb | 33 +++++++++++++++++++++++++++++++++ locales/en.yml | 4 ++++ spec/spec_helper.rb | 3 ++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/vSphere/action/clone.rb b/lib/vSphere/action/clone.rb index a3a23a94..7678ec52 100644 --- a/lib/vSphere/action/clone.rb +++ b/lib/vSphere/action/clone.rb @@ -92,6 +92,8 @@ def call(env) machine.id = new_vm.config.uuid + wait_for_sysprep(env, new_vm, connection, 600, 10) if machine.config.vm.guest.eql?(:windows) + env[:ui].info I18n.t('vsphere.vm_clone_success') @app.call env @@ -99,6 +101,37 @@ def call(env) private + def wait_for_sysprep(env, vm, vim_connection, timeout, sleep_time) + vem = vim_connection.serviceContent.eventManager + + wait = true + waited_seconds = 0 + + env[:ui].info I18n.t('vsphere.wait_sysprep') + while wait + events = query_customization_succeeded(vm, vem) + + if events.size > 0 + events.each do |e| + env[:ui].info e.fullFormattedMessage + end + wait = false + elsif waited_seconds >= timeout + fail Errors::VSphereError, :'sysprep_timeout' + else + sleep(sleep_time) + waited_seconds += sleep_time + end + end + end + + def query_customization_succeeded(vm, vem) + vem.QueryEvents(filter: + RbVmomi::VIM::EventFilterSpec(entity: + RbVmomi::VIM::EventFilterSpecByEntity(entity: vm, recursion: + RbVmomi::VIM::EventFilterSpecRecursionOption(:self)), eventTypeId: ['CustomizationSucceeded'])) + end + def get_customization_spec(machine, spec_info) customization_spec = spec_info.spec.clone diff --git a/locales/en.yml b/locales/en.yml index 35ccabf2..02070b84 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -20,6 +20,8 @@ en: The VM has not been created vm_not_running: |- The VM is not running + wait_sysprep: |- + Waiting for sysprep errors: missing_template: |- @@ -48,6 +50,8 @@ en: Cannot use Linked Clone with Storage DRS multiple_interface_with_real_nic_ip_set: |- real_nic_ip filtering set with multiple valid VM interfaces available + sysprep_timeout: |- + Customization of VM not succeeded within timeout. config: host: |- diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index de1227f5..bf7635b6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -75,7 +75,8 @@ def call communicator: nil, networks: [[:private_network, { ip: '0.0.0.0' }]], boot_timeout: 1, - graceful_halt_timeout: 0.1), + graceful_halt_timeout: 0.1, + guest: nil), validate: [] ) @app = double 'app', call: true