Module: Ohai::Mixin::CloudstackMetadata

Includes:
Ec2Metadata
Defined in:
lib/ohai/mixin/cloudstack_metadata.rb

Constant Summary collapse

CLOUDSTACK_METADATA_ADDR =
self.discover_dhcp_server

Constants included from Ec2Metadata

Ec2Metadata::EC2_ARRAY_DIR, Ec2Metadata::EC2_ARRAY_VALUES, Ec2Metadata::EC2_JSON_DIR, Ec2Metadata::EC2_METADATA_ADDR, Ec2Metadata::EC2_SUPPORTED_VERSIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ec2Metadata

#can_metadata_connect?, #fetch_dir_metadata, #fetch_json_dir_metadata, #fetch_metadata, #fetch_userdata, #metadata_get

Class Method Details

.discover_dhcp_serverObject


29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ohai/mixin/cloudstack_metadata.rb', line 29

def self.discover_dhcp_server
  response = ''
  if Ohai::Hints.hint?('cloudstack')
    begin
      request = DHCP::Discover.new

      listensock = UDPSocket.new
      sendsock   = UDPSocket.new

      listensock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
      sendsock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)

      sendsock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
      sendaddr   = "<broadcast>"
      listenport = 68

      listensock.bind('', listenport)
      sendsock.connect(sendaddr, 67)

      sendsock.send(request.pack, 0)

      data = listensock.recvfrom_nonblock(1500)
    rescue Exception => e
      if (defined?(IO::WaitReadable) && e.instance_of?(IO::WaitReadable)) ||
          (e.instance_of?(Errno::EAGAIN) || e.instance_of?(Errno::EWOULDBLOCK)) # This OR branch can be removed when ruby > 1.8.7
        unless IO.select([listensock], nil, nil, 10)
          # timeout reached
          Ohai::Log.debug("Timeout reached awaiting response from DHCP server")
        else
          # try to read from the socket again
          data = listensock.recvfrom_nonblock(1500)
        end
      else
        Ohai::Log.debug("Exceptions encountered when trying to connect to dhcp server. #{e.message}")
      end
    ensure
      sendsock.close
      listensock.close
      if data
        response = [DHCP::Message.from_udp_payload(data[0]).siaddr].pack('N').unpack('C4').join('.')
      end
    end
  end
  response
end

Instance Method Details

#best_api_versionObject


81
82
83
# File 'lib/ohai/mixin/cloudstack_metadata.rb', line 81

def best_api_version
  'latest'
end

#http_clientObject


77
78
79
# File 'lib/ohai/mixin/cloudstack_metadata.rb', line 77

def http_client
  Net::HTTP.start().tap { |h| h.read_timeout = 600 }
end