/usr/lib/ruby/vendor_ruby/specinfra/ec2_metadata.rb is in ruby-specinfra 2.66.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | # -*- coding: utf-8 -*-
module Specinfra
class Ec2Metadata
def initialize(host_inventory)
@host_inventory = host_inventory
@base_uri = 'http://169.254.169.254/latest/meta-data/'
@metadata = {}
end
def get
@metadata = get_metadata
self
end
def [](key)
if @metadata[key].nil?
begin
require "specinfra/ec2_metadata/#{key}"
inventory_class = Specinfra::Ec2Metadata.const_get(key.to_s.to_camel_case)
@metadata[key] = inventory_class.new(@host_inventory).get
rescue LoadError
@metadata[key] = nil
end
end
@metadata[key]
end
def empty?
@metadata.empty?
end
def each
keys.each do |k|
yield k, @metadata[k]
end
end
def each_key
keys.each do |k|
yield k
end
end
def each_value
keys.each do |k|
yield @metadata[k]
end
end
def keys
@metadata.keys
end
def inspect
@metadata
end
private
def get_metadata(path='')
metadata = {}
keys = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}").stdout.split("\n")
keys.each do |key|
if key =~ %r{/$}
metadata[key[0..-2]] = get_metadata(path + key)
else
if key =~ %r{=}
key = key.split('=')[0] + '/'
metadata[key[0..-2]] = get_metadata(path + key)
else
ret = get_endpoint(path)
metadata[key] = get_endpoint(path + key) if ret
end
end
end
metadata
end
def get_endpoint(path)
ret = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}")
if ret.success?
ret.stdout
else
nil
end
end
end
end
|