Module: Facter::Util::EC2

Defined in:
lib/facter/util/ec2.rb

Overview

Provide a set of utility static methods that help with resolving the EC2 fact.

Class Method Summary collapse

Class Method Details

.can_connect?(wait_sec = 2) ⇒ Boolean

Test if we can connect to the EC2 api. Return true if able to connect. On failure this function fails silently and returns false.

The wait_sec parameter provides you with an adjustable timeout.

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/facter/util/ec2.rb', line 13

def can_connect?(wait_sec=2)
  Facter.warnonce("#{self}.#{__method__} is deprecated; see the Facter::EC2 classes instead")
  url = "http://169.254.169.254:80/"
  Timeout::timeout(wait_sec) {open(url)}
  return true
rescue Timeout::Error
  return false
rescue
  return false
end

.has_ec2_arp?Boolean

Test if the host has an arp entry in its cache that matches the EC2 arp, which is normally fe:ff:ff:ff:ff:ff.

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/facter/util/ec2.rb', line 41

def has_ec2_arp?
  Facter.warnonce("#{self}.#{__method__} is deprecated; see the Facter::EC2 classes instead")
  kernel = Facter.value(:kernel)

  mac_address_re = case kernel
                   when /Windows/i
                     /fe-ff-ff-ff-ff-ff/i
                   else
                     /fe:ff:ff:ff:ff:ff/i
                   end

  arp_command = case kernel
                when /Windows/i, /SunOS/i
                  "arp -a"
                else
                  "arp -an"
                end

  if arp_table = Facter::Core::Execution.exec(arp_command)
    return true if arp_table.match(mac_address_re)
  end
  return false
end

.has_euca_mac?Boolean

Test if this host has a mac address used by Eucalyptus clouds, which normally is d0:0d.

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/facter/util/ec2.rb', line 26

def has_euca_mac?
  Facter.warnonce("#{self}.#{__method__} is deprecated; see the Facter::EC2 classes instead")
  !!(Facter.value(:macaddress) =~ %r{^[dD]0:0[dD]:})
end

.has_openstack_mac?Boolean

Test if this host has a mac address used by OpenStack, which normally starts with FA:16:3E (older versions of OpenStack may generate mac addresses starting with 02:16:3E)

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/facter/util/ec2.rb', line 34

def has_openstack_mac?
  Facter.warnonce("#{self}.#{__method__} is deprecated; see the Facter::EC2 classes instead")
  !!(Facter.value(:macaddress) =~ %r{^(02|[fF][aA]):16:3[eE]})
end

.userdata(version = "latest") ⇒ String

userdata returns a single string containing the body of the response of the GET request for the URI 169.254.169.254/latest/user-data/ If the metadata server responds with a 404 Not Found error code then this method retuns ‘nil`.

Defaults to “latest” and other examples are documented at aws.amazon.com/archives/Amazon%20EC2

Parameters:

  • version (String) (defaults to: "latest")

    containing the API version for the request.

Returns:

  • (String)

    containing the response body or ‘nil`



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facter/util/ec2.rb', line 79

def self.userdata(version="latest")
  Facter.warnonce("#{self}.#{__method__} is deprecated; see the Facter::EC2 classes instead")
  uri = "http://169.254.169.254/#{version}/user-data/"
  begin
    read_uri(uri)
  rescue OpenURI::HTTPError => detail
    case detail.message
    when /404 Not Found/i
      Facter.debug "No user-data present at #{uri}: server responded with #{detail.message}"
      return nil
    else
      raise detail
    end
  end
end