Module: VagrantPlugins::Skytap::Cap::HostMetadata

Defined in:
lib/vagrant-skytap/cap/host_metadata.rb

Constant Summary collapse

'169.254.169.254'
OPEN_TIMEOUT =
5
READ_TIMEOUT =
15

Class Method Summary collapse

Class Method Details

.host_metadata(machine) ⇒ Hash

If Vagrant is running in a Skytap VM, returns metadata from which an

API::Vm

can be constructed. Otherwise returns nil.

Parameters:

  • machine (Vagrant::Machine)

    The guest machine (ignored).

Returns:

  • (Hash)

    or [NilClass]



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-skytap/cap/host_metadata.rb', line 40

def self.(machine)
  logger = Log4r::Logger.new("vagrant_skytap::cap::host_metadata")

  # A Skytap VM can request information about itself from the metadata
  # service at http://gw/skytap. If the network is set to use custom
  # DNS, 'gw' may resolve to something else, in which case we fall back
  # to the service's link local address.
  ['gw', METADATA_LINK_LOCAL_ADDRESS].each do |host|
    begin
      http = Net::HTTP.new(host)
      http.open_timeout = OPEN_TIMEOUT
      http.read_timeout = READ_TIMEOUT

      # Test for a working web server before actually hitting the
      # metadata service. The response is expected to be 404.
      logger.debug("Checking for HTTP service on host '#{host}' ...")
      http.request(Net::HTTP::Get.new("/"))

      begin
        logger.debug("Fetching VM metadata from http://#{host}/skytap ...")
        response = http.request(Net::HTTP::Get.new("/skytap"))
      rescue Timeout::Error => ex
        logger.debug("The request timed out.")
        raise Errors::MetadataServiceUnavailable
      end

      if response.is_a?(Net::HTTPOK)
        if (attrs = JSON.parse(response.body)) && attrs.key?('configuration_url')
          logger.debug('Metadata retrieved successfully.')
          return attrs
        end
        logger.debug('The response did not contain VM metadata.')
        logger.debug("Response body: #{response.body}")
      else
        logger.debug("The server responded with status #{response.code}.")
        logger.debug("Response body: #{response.body}")
        raise Errors::MetadataServiceUnavailable if response.is_a?(Net::HTTPServerError)
      end
    rescue SystemCallError, SocketError, Timeout::Error, JSON::ParserError => ex
      logger.debug(ex)
      logger.debug("Response body: #{response.body}") if response.try(:body)
    end
  end

  logger.debug("Could not obtain VM metadata. Host is not a Skytap VM.")
  nil
end