ec2.rb

Path: lib/ohai/plugins/ec2.rb
Last Update: Mon Jun 21 05:15:44 +0000 2010
dot/f_1.png
Author:Tim Dysinger (<tim@dysinger.net>)
Author:Benjamin Black (<bb@opscode.com>)
Author:Christopher Brown (<cb@opscode.com>)
Copyright:Copyright (c) 2009 Opscode, Inc.
License:Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Required files

open-uri   socket  

Methods

Constants

EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR)
EC2_METADATA_URL = "http://#{EC2_METADATA_ADDR}/2008-02-01/meta-data" unless defined?(EC2_METADATA_URL)
EC2_USERDATA_URL = "http://#{EC2_METADATA_ADDR}/2008-02-01/user-data" unless defined?(EC2_USERDATA_URL)
EC2_ARRAY_VALUES = %w(security-groups)

Public Instance methods

[Source]

    # File lib/ohai/plugins/ec2.rb, line 34
34: def can_metadata_connect?(addr, port, timeout=2)
35:   t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
36:   saddr = Socket.pack_sockaddr_in(port, addr)
37:   connected = false
38: 
39:   begin
40:     t.connect_nonblock(saddr)    
41:   rescue Errno::EINPROGRESS
42:     r,w,e = IO::select(nil,[t],nil,timeout)
43:     if !w.nil?
44:       connected = true
45:     else
46:       begin
47:         t.connect_nonblock(saddr)
48:       rescue Errno::EISCONN
49:         t.close
50:         connected = true
51:       rescue SystemCallError
52:       end
53:     end
54:   rescue SystemCallError
55:   end
56: 
57:   connected
58: end

[Source]

    # File lib/ohai/plugins/ec2.rb, line 60
60: def has_ec2_mac?
61:   network[:interfaces].values.each do |iface|
62:     unless iface[:arp].nil?
63:       return true if iface[:arp].value?("fe:ff:ff:ff:ff:ff")
64:     end
65:   end
66:   false
67: end

[Source]

    # File lib/ohai/plugins/ec2.rb, line 94
94: def looks_like_ec2?
95:   # Try non-blocking connect so we don't "block" if 
96:   # the Xen environment is *not* EC2
97:   has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80)
98: end

[Source]

    # File lib/ohai/plugins/ec2.rb, line 69
69: def metadata(id='')
70:   OpenURI.open_uri("#{EC2_METADATA_URL}/#{id}").read.split("\n").each do |o|
71:     key = "#{id}#{o.gsub(/\=.*$/, '/')}"
72:     if key[-1..-1] != '/'
73:       ec2[key.gsub(/\-|\//, '_').to_sym] = 
74:         if EC2_ARRAY_VALUES.include? key
75:           OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read.split("\n")
76:         else
77:           OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read
78:         end
79:     else
80:       metadata(key)
81:     end
82:   end
83: end

[Source]

    # File lib/ohai/plugins/ec2.rb, line 85
85: def userdata()
86:   ec2[:userdata] = nil
87:   # assumes the only expected error is the 404 if there's no user-data
88:   begin
89:     ec2[:userdata] = OpenURI.open_uri("#{EC2_USERDATA_URL}/").read
90:   rescue OpenURI::HTTPError
91:   end
92: end

[Validate]