class Aws::MetadataExport

Constants

VERSION

Public Instance Methods

export(export_tags: true) click to toggle source
# File lib/aws/metadata_export.rb, line 7
def export(export_tags: true)
  set_umask
  write_metadata
  write_tags if export_tags
end

Private Instance Methods

aws_client() click to toggle source

:nocov:

# File lib/aws/metadata_export.rb, line 42
def aws_client
  @aws_client ||= Aws::EC2::Client.new(region: region)
end
az() click to toggle source
# File lib/aws/metadata_export.rb, line 65
def az
  @az ||= Ec2Metadata['placement']['availability_zone']
end
instance_id() click to toggle source
# File lib/aws/metadata_export.rb, line 69
def instance_id
  @instance_id ||= Ec2Metadata['instance_id']
end
local_ipv4() click to toggle source
# File lib/aws/metadata_export.rb, line 73
def local_ipv4
  @local_ipv4 ||= Ec2Metadata['local_ipv4']
end
output_dir() click to toggle source

:nocov:

# File lib/aws/metadata_export.rb, line 47
def output_dir
  @output_dir ||= begin
    dir = ENV['AWS_METADATA_EXPORT_DIR'] || File.join(
      %w(/ var run aws_metadata_export)
    )
    FileUtils.mkpath(dir)
    dir
  end
end
region() click to toggle source
# File lib/aws/metadata_export.rb, line 77
def region
  @region ||= az.chop
end
set_umask() click to toggle source
# File lib/aws/metadata_export.rb, line 15
def set_umask
  if ENV.key?('AWS_METADATA_EXPORT_UMASK')
    File.umask ENV['AWS_METADATA_EXPORT_UMASK'].to_i(8)
  else
    File.umask 00077
  end
end
tags() click to toggle source

:nocov:

# File lib/aws/metadata_export.rb, line 82
def tags
  @tags ||= Aws::EC2::Resource
            .new(client: aws_client)
            .instance(instance_id).tags
end
tags_dir() click to toggle source
# File lib/aws/metadata_export.rb, line 57
def tags_dir
  @tags_dir ||= begin
    dir = File.join(output_dir, 'tags')
    FileUtils.mkpath(dir)
    dir
  end
end
write_metadata() click to toggle source
# File lib/aws/metadata_export.rb, line 23
def write_metadata
  %w(instance_id local_ipv4 az region).each do |meta|
    metafile = File.join(output_dir, meta)
    File.open(metafile, 'w') do |fh|
      fh.write send meta.to_sym
    end
  end
end
write_tags() click to toggle source
# File lib/aws/metadata_export.rb, line 32
def write_tags
  tags.each do |tag|
    tagfile = File.join(tags_dir, tag['key'])
    File.open(tagfile, 'w') do |fh|
      fh.write tag['value']
    end
  end
end