Skip to content

Commit

Permalink
Merge pull request #725 from tas50/new_ec2_metadata
Browse files Browse the repository at this point in the history
Add support for the last 2 EC2 metadata versions
  • Loading branch information
tas50 committed Feb 4, 2016
2 parents 9b21b51 + 8acdcfe commit d21eea3
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/ohai/mixin/ec2_metadata.rb
Expand Up @@ -41,8 +41,9 @@ module Mixin
module Ec2Metadata

EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR)
EC2_SUPPORTED_VERSIONS = %w[ 1.0 2007-01-19 2007-03-01 2007-08-29 2007-10-10 2007-12-15
2008-02-01 2008-09-01 2009-04-04 2011-01-01 2011-05-01 2012-01-12 ]
EC2_SUPPORTED_VERSIONS = %w( 1.0 2007-01-19 2007-03-01 2007-08-29 2007-10-10 2007-12-15
2008-02-01 2008-09-01 2009-04-04 2011-01-01 2011-05-01 2012-01-12
2014-11-05 2014-02-25 )

EC2_ARRAY_VALUES = %w(security-groups)
EC2_ARRAY_DIR = %w(network/interfaces/macs)
Expand Down Expand Up @@ -70,14 +71,14 @@ def can_metadata_connect?(addr, port, timeout=2)
end
rescue SystemCallError
end
Ohai::Log.debug("can_metadata_connect? == #{connected}")
Ohai::Log.debug("ec2 metadata mixin: can_metadata_connect? == #{connected}")
connected
end

def best_api_version
response = http_client.get("/")
if response.code == '404'
Ohai::Log.debug("Received HTTP 404 from metadata server while determining API version, assuming 'latest'")
Ohai::Log.debug("ec2 metadata mixin: Received HTTP 404 from metadata server while determining API version, assuming 'latest'")
return "latest"
elsif response.code != '200'
raise "Unable to determine EC2 metadata version (returned #{response.code} response)"
Expand All @@ -88,9 +89,9 @@ def best_api_version
versions = response.body.split("\n").sort
until (versions.empty? || EC2_SUPPORTED_VERSIONS.include?(versions.last)) do
pv = versions.pop
Ohai::Log.debug("EC2 shows unsupported metadata version: #{pv}") unless pv == 'latest'
Ohai::Log.debug("ec2 metadata mixin: EC2 shows unsupported metadata version: #{pv}") unless pv == 'latest'
end
Ohai::Log.debug("EC2 metadata version: #{versions.last}")
Ohai::Log.debug("ec2 metadata mixin: EC2 metadata version: #{versions.last}")
if versions.empty?
raise "Unable to determine EC2 metadata version (no supported entries found)"
end
Expand All @@ -115,7 +116,7 @@ def metadata_get(id, api_version)
when '200'
response.body
when '404'
Ohai::Log.debug("Encountered 404 response retreiving EC2 metadata path: #{path} ; continuing.")
Ohai::Log.debug("ec2 metadata mixin: Encountered 404 response retreiving EC2 metadata path: #{path} ; continuing.")
nil
else
raise "Encountered error retrieving EC2 metadata (#{path} returned #{response.code} response)"
Expand All @@ -124,9 +125,9 @@ def metadata_get(id, api_version)

def fetch_metadata(id='', api_version=nil)
api_version ||= best_api_version
return Hash.new if api_version.nil?
return {} if api_version.nil?

metadata = Hash.new
metadata = {}
retrieved_metadata = metadata_get(id, api_version)
if retrieved_metadata
retrieved_metadata.split("\n").each do |o|
Expand Down Expand Up @@ -173,7 +174,7 @@ def fetch_dir_metadata(id, api_version)
end

def fetch_json_dir_metadata(id, api_version)
metadata = Hash.new
metadata = {}
retrieved_metadata = metadata_get(id, api_version)
if retrieved_metadata
retrieved_metadata.split("\n").each do |o|
Expand All @@ -192,7 +193,7 @@ def fetch_json_dir_metadata(id, api_version)
end
end

def fetch_userdata()
def fetch_userdata
api_version = best_api_version
return nil if api_version.nil?
response = http_client.get("/#{api_version}/user-data/")
Expand Down

0 comments on commit d21eea3

Please sign in to comment.