class Pec2::Ec2
Public Class Methods
new(profile:)
click to toggle source
# File lib/pec2/ec2.rb, line 15 def initialize(profile:) @logger = Logger.new(STDOUT) @ec2 = Aws::EC2::Client.new(get_param(profile)) end
Public Instance Methods
get_document()
click to toggle source
# File lib/pec2/ec2.rb, line 31 def get_document JSON.parse(get_metadata('/latest/dynamic/instance-identity/document/')) end
get_metadata(path)
click to toggle source
# File lib/pec2/ec2.rb, line 35 def get_metadata(path) begin result = {} Timeout.timeout(TIME_OUT) { body = open('http://169.254.169.254' + path).read return body } return result rescue Timeout::Error => e raise "not EC2 instance" end end
instances_hash(condition)
click to toggle source
# File lib/pec2/ec2.rb, line 20 def instances_hash(condition) filter = [] condition.each do |key, value| filter << { name: "tag:#{key}", values: ["#{value}"] } end filter << { name: 'instance-state-name', values: ['running'] } @ec2.describe_instances( filters: filter ).data.to_h[:reservations].map { |instance| Pec2Mash.new(instance[:instances].first) } end
Private Instance Methods
get_param(profile)
click to toggle source
# File lib/pec2/ec2.rb, line 50 def get_param(profile) param = {} if profile param[:profile] = profile else param[:region] = if ENV['AWS_REGION'] ENV['AWS_REGION'] else Metadata.get_document['region'] end end param end