Class: ChefProvisioningVsphere::VsphereDriver
- Inherits:
-
Chef::Provisioning::Driver
- Object
- Chef::Provisioning::Driver
- ChefProvisioningVsphere::VsphereDriver
- Includes:
- Chef::Mixin::ShellOut
- Defined in:
- lib/chef/provisioning/vsphere_driver/driver.rb
Overview
Provisions machines in vSphere.
Instance Attribute Summary collapse
-
#connect_options ⇒ Object
readonly
Returns the value of attribute connect_options.
Class Method Summary collapse
-
.canonicalize_url(driver_url, config) ⇒ Object
Create a new Vsphere provisioner.
- .from_url(driver_url, config) ⇒ Object
- .symbolize_keys(h) ⇒ Object
Instance Method Summary collapse
- #add_machine_spec_location(vm, machine_spec) ⇒ Object
- #all_ips_for(vm) ⇒ Object
-
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Acquire a machine, generally by provisioning it.
- #attempt_ip(machine_options, action_handler, vm, machine_spec) ⇒ Object
-
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it.
- #deep_symbolize(hash_like) ⇒ Object
- #destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #find_or_create_vm(bootstrap_options, machine_spec, action_handler) ⇒ Object
- #full_description(machine_spec, bootstrap_options) ⇒ Object
- #has_ip?(ip, vm) ⇒ Boolean
-
#initialize(driver_url, config) ⇒ VsphereDriver
constructor
A new instance of VsphereDriver.
- #machine_msg(name, id, action) ⇒ Object
- #merge_options!(machine_options) ⇒ Object
- #ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #restart_server(action_handler, machine_spec, machine_options) ⇒ Object
- #setup_extra_nics(action_handler, bootstrap_options, vm, machine) ⇒ Object
- #start_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #stop_machine(action_handler, machine_spec, machine_options) ⇒ Object
- #transport_respond?(machine_options, vm, action_handler, machine_spec) ⇒ Boolean
- #wait_for_domain(bootstrap_options, vm, machine_spec, action_handler) ⇒ Object
- #wait_for_ip(vm, machine_options, machine_spec, action_handler) ⇒ Object
Constructor Details
#initialize(driver_url, config) ⇒ VsphereDriver
Returns a new instance of VsphereDriver.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 56 def initialize(driver_url, config) super(driver_url, config) uri = URI(driver_url) @connect_options = { provider: 'vsphere', host: uri.host, port: uri.port, use_ssl: uri.use_ssl, insecure: uri.insecure, path: uri.path } if @connect_options[:user] = [:user] @connect_options[:password] = [:password] end end |
Instance Attribute Details
#connect_options ⇒ Object (readonly)
Returns the value of attribute connect_options.
75 76 77 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 75 def @connect_options end |
Class Method Details
.canonicalize_url(driver_url, config) ⇒ Object
Create a new Vsphere provisioner.
## Parameters connect_options - hash of options to be passed to RbVmomi::VIM.connect
:host - required - hostname of the vSphere API server
:port - optional - port on the vSphere API server (default: 443)
:path - optional - path on the vSphere API server (default: /sdk)
:use_ssl - optional - true to use ssl in connection to vSphere API server (default: true)
:insecure - optional - true to ignore ssl certificate validation errors in connection to vSphere API server (default: false)
:user - required - user name to use in connection to vSphere API server
:password - required - password to use in connection to vSphere API server
32 33 34 35 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 32 def self.canonicalize_url(driver_url, config) config = symbolize_keys(config) [driver_url || URI::VsphereUrl.from_config(config).to_s, config] end |
.from_url(driver_url, config) ⇒ Object
17 18 19 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 17 def self.from_url(driver_url, config) VsphereDriver.new(driver_url, config) end |
.symbolize_keys(h) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 37 def self.symbolize_keys(h) Hash === h ? Hash[ h.map do |k, v| [k.respond_to?(:to_sym) ? k.to_sym : k, symbolize_keys(v)] end ] : h end |
Instance Method Details
#add_machine_spec_location(vm, machine_spec) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 169 def add_machine_spec_location(vm, machine_spec) machine_spec.location = { 'driver_url' => driver_url, 'driver_version' => VERSION, 'server_id' => vm.config.instanceUuid, 'is_windows' => is_windows?(vm), 'allocated_at' => Time.now.utc.to_s, 'ipaddress' => vm.guest.ipAddress } end |
#all_ips_for(vm) ⇒ Object
404 405 406 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 404 def all_ips_for(vm) vm.guest.net.map(&:ipAddress).flatten end |
#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object
Acquire a machine, generally by provisioning it. Returns a Machine object pointing at the machine, allowing useful actions like setup, converge, execute, file and directory. The Machine object will have a “node” property which must be saved to the server (if it is any different from the original node object).
## Parameters action_handler - the action_handler object that is calling this method; this
is generally a action_handler, but could be anything that can support the
ChefMetal::ActionHandler interface (i.e., in the case of the test
kitchen metal driver for acquiring and destroying VMs; see the base
class for what needs providing).
node - node object (deserialized json) representing this machine. If
the node has a provisioner_options hash in it, these will be used
instead of options provided by the provisioner. TODO compare and
fail if different?
node will have node['normal']['provisioner_options'] in it with any options.
It is a hash with this format:
-- provisioner_url: vsphere://host:port?ssl=[true|false]&insecure=[true|false]
-- bootstrap_options: hash of options to pass to RbVmomi::VIM::VirtualMachine::CloneTask()
:datacenter
:resource_pool
:cluster
:datastore
:template_name
:template_folder
:vm_folder
:winrm {...} (not yet implemented)
:ssh {...}
Example bootstrap_options for vSphere:
TODO: add other CloneTask params, e.g.: datastore, annotation, resource_pool, ...
'bootstrap_options' => {
'template_name' =>'centos6.small',
'template_folder' =>'Templates',
'vm_folder' => 'MyApp'
}
node['normal']['provisioner_output'] will be populated with information
about the created machine. For vSphere, it is a hash with this
format:
-- provisioner_url: vsphere:host:port?ssl=[true|false]&insecure=[true|false]
-- vm_folder: name of the vSphere folder containing the VM
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 123 def allocate_machine(action_handler, machine_spec, ) = deep_symbolize() if machine_spec.location Chef::Log.warn( "Checking to see if #{machine_spec.location} has been created..." ) vm = vm_for(machine_spec) if vm Chef::Log.warn 'returning existing machine' return vm else Chef::Log.warn machine_msg( machine_spec.name, machine_spec.location['server_id'], 'no longer exists. Recreating ...' ) end end = [:bootstrap_options] action_handler.report_progress full_description( machine_spec, ) vm = find_or_create_vm(, machine_spec, action_handler) add_machine_spec_location(vm, machine_spec) action_handler.performed_action(machine_msg( machine_spec.name, vm.config.instanceUuid, 'created' )) vm end |
#attempt_ip(machine_options, action_handler, vm, machine_spec) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 326 def attempt_ip(, action_handler, vm, machine_spec) vm_ip = ip_to_bootstrap([:bootstrap_options], vm) wait_for_ip(vm, , machine_spec, action_handler) unless has_ip?(vm_ip, vm) action_handler.report_progress 'rebooting...' if vm.guest.toolsRunningStatus != 'guestToolsRunning' msg = 'tools have stopped. current power state is ' msg << vm.runtime.powerState msg << ' and tools state is ' msg << vm.guest.toolsRunningStatus msg << '. powering up server...' action_handler.report_progress(msg) vsphere_helper.start_vm(vm) else restart_server(action_handler, machine_spec, ) end wait_for_ip(vm, , machine_spec, action_handler) end end |
#connect_to_machine(machine_spec, machine_options) ⇒ Object
Connect to machine without acquiring it
413 414 415 416 417 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 413 def connect_to_machine(machine_spec, ) = deep_symbolize() machine_for(machine_spec, ) end |
#deep_symbolize(hash_like) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 46 def deep_symbolize(hash_like) return {} if hash_like.nil? || hash_like.empty? r = {} hash_like.each do |key, value| value = deep_symbolize(value) if value.respond_to?(:values) r[key.to_sym] = value end r end |
#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 419 def destroy_machine(action_handler, machine_spec, ) = deep_symbolize() vm = vm_for(machine_spec) if vm action_handler.perform_action "Delete VM [#{vm.parent.name}/#{vm.name}]" do begin vsphere_helper.stop_vm(vm, [:stop_timeout]) vm.Destroy_Task.wait_for_completion rescue RbVmomi::Fault => fault raise fault unless fault.fault.class.wsdl_name == 'ManagedObjectNotFound' ensure machine_spec.location = nil end end end strategy = convergence_strategy_for(machine_spec, ) strategy.cleanup_convergence(action_handler, machine_spec) end |
#find_or_create_vm(bootstrap_options, machine_spec, action_handler) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 180 def find_or_create_vm(, machine_spec, action_handler) vm = vsphere_helper.find_vm( [:vm_folder], machine_spec.name ) if vm Chef::Log.info machine_msg( machine_spec.name, vm.config.instanceUuid, 'already created' ) else vm = clone_vm( action_handler, , machine_spec.name ) end vm end |
#full_description(machine_spec, bootstrap_options) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 201 def full_description(machine_spec, ) description = ["creating machine #{machine_spec.name} on #{driver_url}"] .to_hash.each_pair do |key, value| if value.is_a?(Hash) temp_value = value.clone temp_value[:password] = '*********' if value.key?(:password) else temp_value = value end description << " #{key}: #{temp_value.inspect}" end description end |
#has_ip?(ip, vm) ⇒ Boolean
408 409 410 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 408 def has_ip?(ip, vm) all_ips_for(vm).include?(ip) end |
#machine_msg(name, id, action) ⇒ Object
215 216 217 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 215 def machine_msg(name, id, action) "Machine - #{action} - #{name} (#{id} on #{driver_url})" end |
#merge_options!(machine_options) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 161 def () @config = Cheffish::MergedConfig.new( { machine_options: }, @config ) @config = deep_symbolize(@config.to_h) end |
#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 219 def ready_machine(action_handler, machine_spec, ) = deep_symbolize() vm = start_machine(action_handler, machine_spec, ) if vm.nil? raise "Machine #{machine_spec.name} does not have a server "\ 'associated with it, or server does not exist.' end = [:bootstrap_options] transport_respond?( , vm, action_handler, machine_spec ) machine = machine_for(machine_spec, ) setup_extra_nics(action_handler, , vm, machine) if has_static_ip() && !is_windows?(vm) setup_ubuntu_dns(machine, , machine_spec) end machine end |
#restart_server(action_handler, machine_spec, machine_options) ⇒ Object
462 463 464 465 466 467 468 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 462 def restart_server(action_handler, machine_spec, ) action_handler.perform_action "restart machine #{machine_spec.name} (#{driver_url})" do stop_machine(action_handler, machine_spec, ) start_machine(action_handler, machine_spec, ) machine_spec.location['started_at'] = Time.now.utc.to_s end end |
#setup_extra_nics(action_handler, bootstrap_options, vm, machine) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 249 def setup_extra_nics(action_handler, , vm, machine) networks = [:network_name] networks = [networks] if networks.is_a?(String) return if networks.nil? || networks.count < 2 new_nics = vsphere_helper.add_extra_nic( action_handler, vm_template_for(), , vm ) if is_windows?(vm) && !new_nics.nil? new_nics.each do |nic| nic_label = nic.device.deviceInfo.label machine.execute_always( "Disable-Netadapter -Name '#{nic_label}' -Confirm:$false" ) end end end |
#start_machine(action_handler, machine_spec, machine_options) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 450 def start_machine(action_handler, machine_spec, ) = deep_symbolize() vm = vm_for(machine_spec) if vm action_handler.perform_action "Power on VM [#{vm.parent.name}/#{vm.name}]" do vsphere_helper.start_vm(vm, [:bootstrap_options][:ssh][:port]) end end vm end |
#stop_machine(action_handler, machine_spec, machine_options) ⇒ Object
439 440 441 442 443 444 445 446 447 448 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 439 def stop_machine(action_handler, machine_spec, ) = deep_symbolize() vm = vm_for(machine_spec) if vm action_handler.perform_action "Shutdown guest OS and power off VM [#{vm.parent.name}/#{vm.name}]" do vsphere_helper.stop_vm(vm, [:stop_timeout]) end end end |
#transport_respond?(machine_options, vm, action_handler, machine_spec) ⇒ Boolean
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 270 def transport_respond?( , vm, action_handler, machine_spec ) = [:bootstrap_options] # this waits for vmware tools to start and the vm to presebnt an ip # This may just be the ip of a newly cloned machine # Customization below may change this to a valid ip wait_until_ready(action_handler, machine_spec, , vm) if !machine_spec.location['ipaddress'] || !has_ip?(machine_spec.location['ipaddress'], vm) # find the ip we actually want # this will be the static ip to assign # or the ip reported back by the vm if using dhcp # it *may* be nil if just cloned vm_ip = ip_to_bootstrap(, vm) transport = nil unless vm_ip.nil? transport = transport_for(machine_spec, [:ssh], vm_ip) end unless !transport.nil? && transport.available? && has_ip?(vm_ip, vm) attempt_ip(, action_handler, vm, machine_spec) end machine_spec.location['ipaddress'] = vm.guest.ipAddress action_handler.report_progress( "IP address obtained: #{machine_spec.location['ipaddress']}" ) end wait_for_domain(, vm, machine_spec, action_handler) begin wait_for_transport(action_handler, machine_spec, , vm) rescue Timeout::Error # Only ever reboot once, and only if it's been less than 10 minutes # since we stopped waiting if machine_spec.location['started_at'] || remaining_wait_time(machine_spec, ) < -(10 * 60) raise else Chef::Log.warn(machine_msg( machine_spec.name, vm.config.instanceUuid, 'started but SSH did not come up. Rebooting...' )) restart_server(action_handler, machine_spec, ) wait_until_ready(action_handler, machine_spec, , vm) wait_for_transport(action_handler, machine_spec, , vm) end end end |
#wait_for_domain(bootstrap_options, vm, machine_spec, action_handler) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 348 def wait_for_domain(, vm, machine_spec, action_handler) return unless [:customization_spec] domain = if [:customization_spec].is_a?(String) && is_windows?(vm) spec = vsphere_helper.find_customization_spec([:customization_spec]) spec.identity.identification.joinDomain elsif [:customization_spec].is_a?(String) && !is_windows?(vm) spec = vsphere_helper.find_customization_spec([:customization_spec]) spec.identity.domain else [:customization_spec][:domain] end return unless domain if is_windows?(vm) && domain != 'local' start = Time.now.utc trimmed_name = machine_spec.name.byteslice(0, 15) expected_name = "#{trimmed_name}.#{domain}" action_handler.report_progress( "waiting to domain join and be named #{expected_name}" ) until (Time.now.utc - start) > 30 || (vm.guest.hostName == expected_name) print '.' sleep 5 end end end |
#wait_for_ip(vm, machine_options, machine_spec, action_handler) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/chef/provisioning/vsphere_driver/driver.rb', line 378 def wait_for_ip(vm, , machine_spec, action_handler) = [:bootstrap_options] vm_ip = ip_to_bootstrap(, vm) ready_timeout = [:ready_timeout] || 300 msg = "waiting up to #{ready_timeout} seconds for customization" msg << " and find #{vm_ip}" unless vm_ip == vm.guest.ipAddress action_handler.report_progress msg start = Time.now.utc connectable = false until (Time.now.utc - start) > ready_timeout || connectable action_handler.report_progress( "IP addresses found: #{all_ips_for(vm)}" ) vm_ip ||= ip_to_bootstrap(, vm) if has_ip?(vm_ip, vm) connectable = transport_for( machine_spec, [:bootstrap_options][:ssh], vm_ip ).available? end sleep 5 end end |