Path: | lib/ohai/plugins/ec2.rb |
Last Update: | Tue May 11 18:27:37 +0000 2010 |
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.
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) |
# File lib/ohai/plugins/ec2.rb, line 33 33: def can_metadata_connect?(addr, port, timeout=2) 34: t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0) 35: saddr = Socket.pack_sockaddr_in(port, addr) 36: connected = false 37: 38: begin 39: t.connect_nonblock(saddr) 40: rescue Errno::EINPROGRESS 41: r,w,e = IO::select(nil,[t],nil,timeout) 42: if !w.nil? 43: connected = true 44: else 45: begin 46: t.connect_nonblock(saddr) 47: rescue Errno::EISCONN 48: t.close 49: connected = true 50: rescue SystemCallError 51: end 52: end 53: rescue SystemCallError 54: end 55: 56: connected 57: end
# File lib/ohai/plugins/ec2.rb, line 59 59: def has_ec2_mac? 60: network[:interfaces].values.each do |iface| 61: unless iface[:arp].nil? 62: return true if iface[:arp].value?("fe:ff:ff:ff:ff:ff") 63: end 64: end 65: false 66: end
# File lib/ohai/plugins/ec2.rb, line 89 89: def looks_like_ec2? 90: # Try non-blocking connect so we don't "block" if 91: # the Xen environment is *not* EC2 92: has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80) 93: end
# File lib/ohai/plugins/ec2.rb, line 68 68: def metadata(id='') 69: OpenURI.open_uri("#{EC2_METADATA_URL}/#{id}").read.split("\n").each do |o| 70: key = "#{id}#{o.gsub(/\=.*$/, '/')}" 71: if key[-1..-1] != '/' 72: ec2[key.gsub(/\-|\//, '_').to_sym] = 73: OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read 74: else 75: metadata(key) 76: end 77: end 78: end