Class: ChefProvisioningVsphere::VmHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/vsphere_driver/vm_helper.rb

Overview

Helps save data in provisioning a machine

Constant Summary collapse

RESCUE_EXCEPTIONS_ON_ESTABLISH =

An array of all the known EXCEPTIONS for connecting via Chef-Provisioning-vSphere

[
  Errno::EACCES, Errno::EADDRINUSE, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
  Errno::ECONNRESET, Errno::ENETUNREACH, Errno::EHOSTUNREACH, Errno::EPIPE,
  Errno::EPERM, Errno::EFAULT, Errno::EIO, Errno::EHOSTDOWN,
  Net::SSH::Disconnect, Net::SSH::AuthenticationFailed, Net::SSH::ConnectionTimeout,
  Timeout::Error, IPAddr::AddressFamilyError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



9
10
11
# File 'lib/chef/provisioning/vsphere_driver/vm_helper.rb', line 9

def ip
  @ip
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/chef/provisioning/vsphere_driver/vm_helper.rb', line 9

def port
  @port
end

Instance Method Details

#find_port?(vm, options) ⇒ true

Finds the port for to connect to the vm

Parameters:

  • vm (Object)

    Uses the VM object from Chef provisioning.

  • options (Hash)

    Uses the VM options from Chef provisioning.

Returns:

  • (true)

    The port to connect to the VM whether Windows or *nix.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef/provisioning/vsphere_driver/vm_helper.rb', line 31

def find_port?(vm, options)
  @port = options[:ssh][:port]
  customization_spec = options[:customization_spec]
  if vm.config.guestId.start_with?("win")
    if customization_spec.is_a?(Hash)
      winrm_transport =
        customization_spec[:winrm_transport].nil? ? :negotiate : customization_spec[:winrm_transport].to_sym
    end
    winrm_transport ||= :negotiate
    default_win_port = winrm_transport == :ssl ? "5986" : "5985"
    @port = default_win_port if @port.nil?
  elsif port.nil?
    @port = "22"
  end
  true
end

#open_port?(host, port, timeout = 5) ⇒ true

Attempt to connects to the open port

Parameters:

  • host (String)

    Uses the host string to connect.

  • port (String)

    Uses the port number to connect.

  • timeout (Integer) (defaults to: 5)

    The number of seconds before timeout.

Returns:

  • (true)

    Returns true when the socket is available to connect.



54
55
56
57
58
59
# File 'lib/chef/provisioning/vsphere_driver/vm_helper.rb', line 54

def open_port?(host, port, timeout = 5)
  return false if host.to_s.empty?
  true if ::Socket.tcp(host, port, connect_timeout: timeout)
rescue *RESCUE_EXCEPTIONS_ON_ESTABLISH
  false
end

#port?Boolean

If the port is true

Returns:

  • (Boolean)


22
23
24
# File 'lib/chef/provisioning/vsphere_driver/vm_helper.rb', line 22

def port?
  @port
end