class EcsCmd::Service

rubocop:disable Metrics/ClassLength

Public Class Methods

new(cluster, name, region) click to toggle source
# File lib/ecs_cmd/service.rb, line 8
def initialize(cluster, name, region)
  @client = Aws::ECS::Client.new(region: region)
  @ec2_client = Aws::EC2::Client.new(region: region)
  @cluster = cluster
  @name = name
  @service_stats = @client.describe_services(cluster: cluster, services: [name])[0]
  raise 'service does not appear to exist' if @service_stats.empty?
end

Public Instance Methods

arn() click to toggle source
# File lib/ecs_cmd/service.rb, line 17
def arn
  @service_stats[0]['service_arn']
end
container_instance(task_arn) click to toggle source

return container instance arn for given task id

# File lib/ecs_cmd/service.rb, line 80
def container_instance(task_arn)
instance = []
@client.describe_tasks(cluster: @cluster, tasks: [task_arn])[0].each do |e|
  instance << e[:container_instance_arn]
end
instance[0]
end
container_instance_id(arn) click to toggle source
# File lib/ecs_cmd/service.rb, line 88
def container_instance_id(arn)
  instance = [arn.to_s]
  @client.describe_container_instances(cluster: @cluster, container_instances: instance)[0][0][:ec2_instance_id]
end
container_instance_ids() click to toggle source
# File lib/ecs_cmd/service.rb, line 93
def container_instance_ids
  ids = []
  @client.describe_container_instances(cluster: @cluster, container_instances: container_instances)[0].each do |e|
    ids << e[:ec2_instance_id]
  end
  ids.uniq
end
container_instance_ip(instance_id) click to toggle source
# File lib/ecs_cmd/service.rb, line 101
def container_instance_ip(instance_id)
  id = [instance_id]
  @ec2_client.describe_instances(instance_ids: id)[:reservations][0][:instances][0][:private_ip_address]
end
container_instance_ips() click to toggle source
# File lib/ecs_cmd/service.rb, line 106
def container_instance_ips
  ips = []
  @ec2_client.describe_instances(instance_ids: container_instance_ids)[:reservations][0][:instances].each do |e|
    ips << e[:private_ip_address]
  end
  ips
end
container_instances() click to toggle source

list all container instance arns for given service's tasks

# File lib/ecs_cmd/service.rb, line 72
def container_instances
  instances = []
  @client.describe_tasks(cluster: @cluster, tasks: tasks)[0].each do |e|
    instances << e[:container_instance_arn]
  end
  instances
end
deployment_configuration() click to toggle source
# File lib/ecs_cmd/service.rb, line 42
def deployment_configuration
  @service_stats[0]['deployment_configuration']
end
deployments() click to toggle source
# File lib/ecs_cmd/service.rb, line 25
def deployments
  t = []
  @service_stats[0]['deployments'].each do |e|
    t << ['id', e['id']]
    t << ['status', e['status']]
    t << ['task definition', e['task_definition']]
    t << ['desired count', e['desired_count']]
    t << ['pending count', e['pending_count']]
    t << ['running count', e['running_count']]
    t << ['created at', e['created_at']]
    t << ['updated at', e['updated_at']]
    t << ["\n"]
  end
  table = Terminal::Table.new headings: ['DEPLOYMENTS', ''], rows: t
  table
end
desired_count() click to toggle source
# File lib/ecs_cmd/service.rb, line 46
def desired_count
  @service_stats[0]['desired_count']
end
events() click to toggle source
# File lib/ecs_cmd/service.rb, line 118
def events
  @service_stats[0]['events'].each do |e|
    puts "#{e['created_at']}: #{e['message']}"
  end
end
health_check_grace_period() click to toggle source
# File lib/ecs_cmd/service.rb, line 114
def health_check_grace_period
  @service_stats[0]['health_check_grace_period_seconds']
end
launch_type() click to toggle source
# File lib/ecs_cmd/service.rb, line 128
def launch_type
  @service_stats[0]['deployments'][0]['launch_type']
end
list_service() click to toggle source
# File lib/ecs_cmd/service.rb, line 166
def list_service
  puts overview_table
  puts task_def_table
  puts deployments
  puts tasks_table
end
name() click to toggle source
# File lib/ecs_cmd/service.rb, line 124
def name
  @service_stats[0]['service_name']
end
overview_table() click to toggle source
# File lib/ecs_cmd/service.rb, line 149
def overview_table
  row1 = []
  row1 << [name, status, running_count, desired_count, pending_count,
           deployment_configuration['maximum_percent'], deployment_configuration['minimum_healthy_percent']]
  table1 = Terminal::Table.new headings: ['NAME', 'STATUS', 'RUNNING COUNT',
                                          'DESIRED COUNT', 'PENDING COUNT',
                                          'MAX HEALTHY', 'MIN HEALTHY'], rows: row1
  table1
end
pending_count() click to toggle source
# File lib/ecs_cmd/service.rb, line 54
def pending_count
  @service_stats[0]['pending_count']
end
running_count() click to toggle source
# File lib/ecs_cmd/service.rb, line 50
def running_count
  @service_stats[0]['running_count']
end
status() click to toggle source
# File lib/ecs_cmd/service.rb, line 21
def status
  @service_stats[0]['status']
end
task_def_table() click to toggle source
# File lib/ecs_cmd/service.rb, line 159
def task_def_table
  row2 = []
  row2 << [task_definition]
  table2 = Terminal::Table.new headings: ['TASK DEFINITION'], rows: row2
  table2
end
task_definition() click to toggle source
# File lib/ecs_cmd/service.rb, line 58
def task_definition
  @service_stats[0]['task_definition']
end
task_family() click to toggle source
# File lib/ecs_cmd/service.rb, line 62
def task_family
  # known issue, won't work with / in task family names
  # TODO: improve this later
  @service_stats[0]['task_definition'].split('/')[1].split(':')[0]
end
tasks() click to toggle source

list task arns for a service

# File lib/ecs_cmd/service.rb, line 68
def tasks
  @client.list_tasks(cluster: @cluster, service_name: @name)[0]
end
tasks_table() click to toggle source
# File lib/ecs_cmd/service.rb, line 132
def tasks_table
  t = []
  if launch_type == 'FARGATE'
    tasks.each do |e|
      t << [e]
    end
    table = Terminal::Table.new headings: ['TASK_ID'], rows: t
  else
    tasks.each do |e|
      t << [e, container_instance_id(container_instance(e)),
            container_instance_ip(container_instance_id(container_instance(e)))]
    end
    table = Terminal::Table.new headings: ['TASK_ID', 'INSTANCE_ID', 'IP'], rows: t
  end
  table
end