kitchen-vcenter 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58aef4a3a971cd8ec311e63906b3e99a2944e6f7baebfcc8f172f7c7a87afed2
4
- data.tar.gz: a96f89b0ee46b5f49539822a5b1b7e5115a1b0aca1a819b9ee9d2582c5a7b1b9
3
+ metadata.gz: ce5fdad668c4456c0855c02fd42acef15e17c55c2d2f72e517d73c30e398a0a5
4
+ data.tar.gz: 30f0914834ce79ad2ffc4953687c5ea32fdada3bb998facba74eb2044b1f91a7
5
5
  SHA512:
6
- metadata.gz: 2c5c46c15b32446531e120a60296ae8c6c00851de50c8176be8efe008dccf0fd82cad68bb541bf0081a668d786ffebb56df1d967c80c3da54d31fdd25b75f42e
7
- data.tar.gz: 2aed6ccf7a31d14d0d21f391e28566be717b63c3104cd334b22a3a49e58f93923981b32367cd6a3b5d0f138876cd744aad5da213e9009de6c09c0c7671f180c8
6
+ metadata.gz: 71def8125a412f237cdde9c73d8c1d16da3488a01aa575183b73d0a72242b4785ebc715fd09c57e0d1427926d3636d9ea73eadd817d34b8073e8a6b8946727a5
7
+ data.tar.gz: 4364e84ce871804e7c88062a44655007be5f8733c68415a577f429b167727c465b7031c234451b565f9fcda2438a53eb0538c1202e855980ca7b020054817498
@@ -20,5 +20,5 @@
20
20
  # The main kitchen-vcenter module
21
21
  module KitchenVcenter
22
22
  # The version of this version of test-kitchen we assume enterprises want.
23
- VERSION = "2.0.1"
23
+ VERSION = "2.0.2"
24
24
  end
@@ -48,6 +48,9 @@ module Kitchen
48
48
  default_config :cluster, nil
49
49
  default_config :network_name, nil
50
50
  default_config :tags, nil
51
+ default_config :vm_wait_timeout, 90
52
+ default_config :vm_wait_interval, 2.0
53
+ default_config :vm_rollback, false
51
54
 
52
55
  # The main create method
53
56
  #
@@ -99,14 +102,30 @@ module Kitchen
99
102
  resource_pool: config[:resource_pool],
100
103
  clone_type: config[:clone_type],
101
104
  network_name: config[:network_name],
105
+ wait_timeout: config[:vm_wait_timeout],
106
+ wait_interval: config[:vm_wait_interval],
102
107
  }
103
108
 
104
- # Create an object from which the clone operation can be called
105
- new_vm = Support::CloneVm.new(connection_options, options)
106
- new_vm.clone
109
+ begin
110
+ # Create an object from which the clone operation can be called
111
+ new_vm = Support::CloneVm.new(connection_options, options)
112
+ new_vm.clone
107
113
 
108
- state[:hostname] = new_vm.ip
109
- state[:vm_name] = new_vm.name
114
+ state[:hostname] = new_vm.ip
115
+ state[:vm_name] = new_vm.name
116
+
117
+ rescue # Kitchen::ActionFailed => e
118
+ if config[:vm_rollback] == true
119
+ error format("Rolling back VM %s after critical error", config[:vm_name])
120
+
121
+ # Inject name of failed VM for destroy to work
122
+ state[:vm_name] = config[:vm_name]
123
+
124
+ destroy(state)
125
+ end
126
+
127
+ raise
128
+ end
110
129
 
111
130
  unless config[:tags].nil? || config[:tags].empty?
112
131
  tag_api = VSphereAutomation::CIS::TaggingTagApi.new(api_client)
@@ -143,6 +162,10 @@ module Kitchen
143
162
  def destroy(state)
144
163
  return if state[:vm_name].nil?
145
164
 
165
+ # Reset resource pool, as it's not needed for the destroy action but might be a remnant of earlier calls to "connect"
166
+ # Temporary fix until setting cluster + resource_pool at the same time is implemented (lines #64/#187)
167
+ config[:resource_pool] = nil
168
+
146
169
  save_and_validate_parameters
147
170
  connect
148
171
 
@@ -1,8 +1,9 @@
1
+ require "kitchen"
1
2
  require "rbvmomi"
2
3
 
3
4
  class Support
4
5
  class CloneVm
5
- attr_reader :vim, :options, :vm, :name, :path
6
+ attr_reader :vim, :options, :vm, :name, :path, :ip
6
7
 
7
8
  def initialize(conn_opts, options)
8
9
  @options = options
@@ -12,6 +13,32 @@ class Support
12
13
  @vim ||= RbVmomi::VIM.connect conn_opts
13
14
  end
14
15
 
16
+ def get_ip(vm)
17
+ @ip = nil
18
+
19
+ unless vm.guest.net.empty? || !vm.guest.ipAddress
20
+ @ip = vm.guest.net[0].ipConfig.ipAddress.detect do |addr|
21
+ addr.origin != "linklayer"
22
+ end.ipAddress
23
+ end
24
+
25
+ ip
26
+ end
27
+
28
+ def wait_for_ip(vm, timeout = 30.0, interval = 2.0)
29
+ start = Time.new
30
+
31
+ ip = nil
32
+ loop do
33
+ ip = get_ip(vm)
34
+ break if ip || (Time.new - start) >= timeout
35
+ sleep interval
36
+ end
37
+
38
+ raise "Timeout waiting for IP address or no VMware Tools installed on guest" if ip.nil?
39
+ raise format("Error getting accessible IP address, got %s. Check DHCP server and scope exhaustion", ip) if ip =~ /^169\.254\./
40
+ end
41
+
15
42
  def clone
16
43
  # set the datacenter name
17
44
  dc = vim.serviceInstance.find_datacenter(options[:datacenter])
@@ -82,18 +109,20 @@ class Support
82
109
  # Set the folder to use
83
110
  dest_folder = options[:folder].nil? ? dc.vmFolder : options[:folder][:id]
84
111
 
85
- puts "Cloning '#{options[:template]}' to create the VM..."
112
+ Kitchen.logger.info format("Cloning '%s' to create the VM...", options[:template])
86
113
  if options[:clone_type] == :instant
87
114
  vcenter_data = vim.serviceInstance.content.about
88
115
  raise "Instant clones only supported with vCenter 6.7 or higher" unless vcenter_data.version.to_f >= 6.7
89
- puts "- Detected #{vcenter_data.fullName}"
116
+ Kitchen.logger.debug format("Detected %s", vcenter_data.fullName)
90
117
 
91
118
  resources = dc.hostFolder.children
92
- hosts = resources.select { |resource| resource.class.to_s == "ComputeResource" }.map { |c| c.host }.flatten
119
+ hosts = resources.select { |resource| resource.class.to_s =~ /ComputeResource$/ }.map { |c| c.host }.flatten
93
120
  targethost = hosts.select { |host| host.summary.config.name == options[:targethost].name }.first
121
+ raise "No matching ComputeResource found in host folder" if targethost.nil?
122
+
94
123
  esx_data = targethost.summary.config.product
95
124
  raise "Instant clones only supported with ESX 6.7 or higher" unless esx_data.version.to_f >= 6.7
96
- puts "- Detected #{esx_data.fullName}"
125
+ Kitchen.logger.debug format("Detected %s", esx_data.fullName)
97
126
 
98
127
  # Other tools check for VMWare Tools status, but that will be toolsNotRunning on frozen VMs
99
128
  raise "Need a running VM for instant clones" unless src_vm.runtime.powerState == "poweredOn"
@@ -127,17 +156,13 @@ class Support
127
156
  @vm = dc.find_vm(path)
128
157
 
129
158
  if vm.nil?
130
- puts format("Unable to find machine: %s", path)
159
+ raise format("Unable to find machine: %s", path)
131
160
  else
132
- puts "Waiting for network interfaces to become available..."
133
- sleep 2 while vm.guest.net.empty? || !vm.guest.ipAddress
134
- end
135
- end
161
+ Kitchen.logger.info format("Waiting for VMware tools/network interfaces to become available (timeout: %d seconds)...", options[:wait_timeout])
136
162
 
137
- def ip
138
- vm.guest.net[0].ipConfig.ipAddress.detect do |addr|
139
- addr.origin != "linklayer"
140
- end.ipAddress
163
+ wait_for_ip(vm, options[:wait_timeout], options[:wait_interval])
164
+ Kitchen.logger.info format("Created machine %s with IP %s", name, ip)
165
+ end
141
166
  end
142
167
  end
143
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-vcenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software