require_relative 'instance' class AwsEc2List::Runner attr_reader :ec2, :credentials_path, :stdout, :env def initialize(ec2: Aws::EC2::Client.new, credentials_path: "#{Dir.home}/.aws/credentials", stdout: $stdout, env: ENV) @ec2 = ec2 @credentials_path = credentials_path @stdout = stdout @env = env end def call data_header = ["PROJECT", "STACK NAME", "PUBLIC DNS NAME", "LAUNCH TIME", "APP VERSION", "STATE"] data = instances.map do |d| [d.project, d.stack_name, d.public_dns_name, d.launch_time, d.app_version, d.state] end data = data.insert(0, data_header) puts Tabularize.it(data, :align => :left).map { |row| row.join ' | ' } end private def instances # Aws::EC2::Types::Instance # instance_id="i-0e5846a2b3988300d", # state=#, # private_dns_name="ip-172-16-92-73.ec2.internal", # public_dns_name="ec2-54-157-47-22.compute-1.amazonaws.com", # instance_type="m4.xlarge", # launch_time=2017-01-12 19:46:29 UTC, # subnet_id="subnet-844ef9df", # vpc_id="vpc-2d583b48", # private_ip_address="172.16.92.73", # public_ip_address="54.157.47.22", # architecture="x86_64", # client_token="40459b52-177d-4e54-a3ed-e07e9d434ecc_subnet-844ef9df_5", # tags=[ # #, # #, # #, # #, # #, # #, # #, # #, # #, # # # ], # security_groups=[ # #, # # # ] reservations.map(&:instances).flatten.map do |instance| AwsEc2List::Instance.new( instance_id: instance.instance_id, state: instance.state.name, private_dns_name: instance.private_dns_name, public_dns_name: instance.public_dns_name, instance_type: instance.instance_type, launch_time: instance.launch_time, tags: instance.tags ) end.sort{ |x, y| y.launch_time <=> x.launch_time } end def reservations describe_instances.reservations end def describe_instances ec2.describe_instances end end