class Facter::Resolvers::Ec2

Constants

EC2_METADATA_ROOT_URL
EC2_SESSION_TIMEOUT
EC2_USERDATA_ROOT_URL

Private Class Methods

build_path_component(line) click to toggle source
# File lib/facter/resolvers/ec2.rb, line 45
def build_path_component(line)
  array_match = /^(\d+)=.*$/.match(line)
  array_match ? "#{array_match[1]}/" : line.strip
end
determine_session_timeout() click to toggle source
# File lib/facter/resolvers/ec2.rb, line 54
def determine_session_timeout
  session_env = ENV['EC2_SESSION_TIMEOUT']
  session_env ? session_env.to_i : EC2_SESSION_TIMEOUT
end
get_data_from(url) click to toggle source
# File lib/facter/resolvers/ec2.rb, line 50
def get_data_from(url)
  Utils::Http.get_request(url, {}, { session: determine_session_timeout })
end
post_resolve(fact_name) click to toggle source
# File lib/facter/resolvers/ec2.rb, line 13
def post_resolve(fact_name)
  log.debug('Querying Ec2 metadata')
  @fact_list.fetch(fact_name) { read_facts(fact_name) }
end
query_for_metadata(url, container) click to toggle source
# File lib/facter/resolvers/ec2.rb, line 25
def query_for_metadata(url, container)
  metadata = get_data_from(url)
  metadata.each_line do |line|
    next if line.empty?

    http_path_component = build_path_component(line)
    next if http_path_component == 'security-credentials/'

    if http_path_component.end_with?('/')
      child = {}
      child[http_path_component] = query_for_metadata("#{url}#{http_path_component}", child)
      child.reject! { |key, _info| key == http_path_component }
      name = http_path_component.chomp('/')
      container[name] = child
    else
      container[http_path_component] = get_data_from("#{url}#{http_path_component}").strip
    end
  end
end
read_facts(fact_name) click to toggle source
# File lib/facter/resolvers/ec2.rb, line 18
def read_facts(fact_name)
  @fact_list[:metadata] = {}
  query_for_metadata(EC2_METADATA_ROOT_URL, @fact_list[:metadata])
  @fact_list[:userdata] = get_data_from(EC2_USERDATA_ROOT_URL).strip
  @fact_list[fact_name]
end