Module: Facter::Util::EC2

### Home Classes Methods

### In Files

- lib/facter/util/ec2.rb

### Methods

- ::can_connect?
- ::has_ec2_arp?
- ::has_euca_mac?
- ::has_openstack_mac?
- ::userdata

### Files

- LICENSE
- Rakefile
- THANKS

### Class/Module Index

Quicksearch

- Facter
- Facter::Application
- Facter::Manufacturer
- Facter::Memory
- Facter::NetMask
- Facter::Util
- Facter::Util::CFPropertyList
- Facter::Util::CFPropertyList::CFArray
- Facter::Util::CFPropertyList::CFBoolean
- Facter::Util::CFPropertyList::CFData
- Facter::Util::CFPropertyList::CFDate
- Facter::Util::CFPropertyList::CFDictionary
- Facter::Util::CFPropertyList::CFInteger
- Facter::Util::CFPropertyList::CFReal
- Facter::Util::CFPropertyList::CFString
- Facter::Util::CFPropertyList::CFType
- Facter::Util::CFPropertyList::XML
- Facter::Util::Collection
- Facter::Util::CompositeLoader
- Facter::Util::Config
- Facter::Util::Confine
- Facter::Util::DirectoryLoader
- Facter::Util::DirectoryLoader::NoSuchDirectoryError
- Facter::Util::EC2
- Facter::Util::Fact
- Facter::Util::FileRead
- Facter::Util::IP
- Facter::Util::IP::Windows
- Facter::Util::IP::Windows::Bindings4
- Facter::Util::IP::Windows::Bindings6
- Facter::Util::Loader
- Facter::Util::Macaddress
- Facter::Util::Macaddress::Darwin
- Facter::Util::Macaddress::Windows
- Facter::Util::Macosx
- Facter::Util::MonkeyPatches
- Facter::Util::MonkeyPatches::Lines
- Facter::Util::NothingLoader
- Facter::Util::Parser
- Facter::Util::Parser::Base
- Facter::Util::Parser::JsonParser
- Facter::Util::Parser::KeyValuePairOutputFormat
- Facter::Util::Parser::NothingParser
- Facter::Util::Parser::PowershellParser
- Facter::Util::Parser::ScriptParser
- Facter::Util::Parser::TextParser
- Facter::Util::Parser::YamlParser
- Facter::Util::Processor
- Facter::Util::Registry
- Facter::Util::Resolution
- Facter::Util::SolarisZones
- Facter::Util::Uptime
- Facter::Util::Values
- Facter::Util::Virtual
- Facter::Util::Vlans
- Facter::Util::WMI
- Facter::Util::Xendomains
- Plist
- Plist::Emit
- Plist::Listener
- Plist::PArray
- Plist::PData
- Plist::PDate
- Plist::PDict
- Plist::PFalse
- Plist::PInteger
- Plist::PKey
- Plist::PList
- Plist::PReal
- Plist::PString
- Plist::PTag
- Plist::PTrue
- Plist::StreamParser
- CFFormatError
- CFPlistError
- CFTypeError
- IO
- Object
- String

No matching classes.

# Facter::Util::EC2

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

### Public Class Methods

can_connect?(wait_sec=2) click to toggle source

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.

    # File lib/facter/util/ec2.rb, line 13
    def can_connect?(wait_sec=2)
      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?() click to toggle source

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+.

    # File lib/facter/util/ec2.rb, line 38
    def has_ec2_arp?
      kernel = Facter.value(:kernel)

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

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

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

has_euca_mac?() click to toggle source

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

    # File lib/facter/util/ec2.rb, line 25
    def has_euca_mac?
      !!(Facter.value(:macaddress) =~ %{^[dD]0:0[dD]:})
    end

has_openstack_mac?() click to toggle source

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)

    # File lib/facter/util/ec2.rb, line 32
    def has_openstack_mac?
      !!(Facter.value(:macaddress) =~ %{^(02|[fF][aA]):16:3[eE]})
    end

userdata(version="latest") click to toggle source

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`.

@param version [String] containing the API version for the request. Defaults to "latest" and other examples are documented at aws.amazon.com/archives/Amazon%20EC2

@api public

@return [String] containing the response body or `nil`

    # File lib/facter/util/ec2.rb, line 75
    def self.userdata(version="latest")
      uri = "http://169.254.169.254/#{version}/user-data/"
      begin
        read_uri(uri)
      rescue OpenURI::HTTPError => detail
        case detail.message
        when /404 Not Found/
          Facter.debug "No user-data present at #{uri}: server responded with #{detail.message}"
          return nil
        else
          raise detail
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.