class Stax::Cmd::Ecs

Constants

COLORS

Public Instance Methods

clusters() click to toggle source
# File lib/stax/mixin/ecs.rb, line 97
def clusters
  print_table Aws::Ecs.clusters(my.ecs_cluster_name).map { |c|
    [
      c.cluster_name,
      color(c.status, COLORS),
      "instances:#{c.registered_container_instances_count}",
      "pending:#{c.pending_tasks_count}",
      "running:#{c.running_tasks_count}",
    ]
  }
end
containers() click to toggle source
# File lib/stax/mixin/ecs.rb, line 212
def containers
  my.ecs_services.each do |s|
    Aws::Ecs.tasks(
      cluster: my.ecs_cluster_name,
      service_name: s.physical_resource_id,
      desired_status: options[:status].upcase,
    ).each do |t|
      task = t.task_arn.split('/').last
      debug("Containers for task #{task}")
      print_table t.containers.map { |c|
        [
          c.name,
          c.container_arn.split('/').last,
          color(c.last_status, COLORS),
          c.network_interfaces.map(&:private_ipv_4_address).join(','),
          t.task_definition_arn.split('/').last,
          c.exit_code,
          c.reason,
        ]
      }
    end
  end
end
definitions() click to toggle source
# File lib/stax/mixin/ecs.rb, line 156
def definitions
  print_table my.ecs_task_definitions.map { |r|
    t = Aws::Ecs.task_definition(r.physical_resource_id)
    [r.logical_resource_id, t.family, t.revision, color(t.status, COLORS)]
  }
end
deployments() click to toggle source
# File lib/stax/mixin/ecs.rb, line 145
def deployments
  my.ecs_service_objects.each do |s|
    debug("Deployments for #{s.service_name}")
    print_table s.deployments.map { |d|
      count = "#{d.running_count}/#{d.desired_count} (#{d.pending_count})"
      [d.id, d.status, count, d.created_at, d.updated_at, d.task_definition.split('/').last]
    }
  end
end
ecs_task_definition(id) click to toggle source
# File lib/stax/mixin/ecs.rb, line 87
def ecs_task_definition(id)
  Aws::Cfn.id(my.stack_name, id)
end
env() click to toggle source
# File lib/stax/mixin/ecs.rb, line 164
def env
  my.ecs_task_families.each do |family|
    Aws::Ecs.task_definition(family).container_definitions.each do |c|
      debug("Environment for #{family} #{c.name}")
      print_table c.environment.map { |e|
        [e.name, e.value]
      }.sort
    end
  end
end
events() click to toggle source
# File lib/stax/mixin/ecs.rb, line 118
def events
  my.ecs_service_objects.each do |s|
    debug("Events for #{s.service_name}")
    s.events.first(options[:number]).reverse.map(&method(:print_event))
  end
end
instances() click to toggle source
# File lib/stax/mixin/ecs.rb, line 237
def instances
  print_table Aws::Ecs.instances(my.ecs_cluster_name).map { |i|
    [
      i.container_instance_arn.split('/').last,
      i.ec2_instance_id,
      i.agent_connected,
      color(i.status, COLORS),
      i.running_tasks_count,
      "(#{i.pending_tasks_count})",
      "agent #{i.version_info.agent_version}",
      i.version_info.docker_version,
    ]
  }
end
print_event(e) click to toggle source
run_task(id) click to toggle source
# File lib/stax/mixin/ecs.rb, line 253
def run_task(id)
  Aws::Ecs.run(my.ecs_cluster_name, Aws::Cfn.id(my.stack_name, id)).tap do |tasks|
    puts tasks.map(&:container_instance_arn)
  end
end
scale(*ids) click to toggle source
# File lib/stax/mixin/ecs.rb, line 268
def scale(*ids)
  my.ecs_services_with_ids(*ids).each do |s|
    debug("Scaling service #{s.physical_resource_id.split('/').last}")
    Aws::Ecs.update_service(
      cluster: my.ecs_cluster_name,
      service: s.physical_resource_id,
      desired_count: options[:desired],
    ).tap do |s|
      puts "desired: #{s.desired_count}"
    end
  end
end
secrets() click to toggle source
# File lib/stax/mixin/ecs.rb, line 176
def secrets
  my.ecs_task_families.each do |family|
    Aws::Ecs.task_definition(family).container_definitions.each do |c|
      debug("Secrets for #{family} #{c.name}")
      print_table c.secrets.map { |e|
        [e.name, e.value_from]
      }.sort
    end
  end
end
services() click to toggle source
# File lib/stax/mixin/ecs.rb, line 110
def services
  print_table my.ecs_service_objects.map { |s|
    [s.service_name, color(s.status, COLORS), s.task_definition.split('/').last, "#{s.running_count}/#{s.desired_count}"]
  }
end
stop_task(task) click to toggle source
# File lib/stax/mixin/ecs.rb, line 260
def stop_task(task)
  Aws::Ecs.stop(my.ecs_cluster_name, task).tap do |task|
    puts task.container_instance_arn
  end
end
tail(service = nil) click to toggle source
# File lib/stax/mixin/ecs.rb, line 126
def tail(service = nil)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  service ||= my.ecs_service_names.first
  latest_event = Aws::Ecs.services(my.ecs_cluster_name, [service]).first.events.first
  print_event(latest_event)
  last_seen = latest_event.id
  loop do
    sleep 5
    unseen = []
    Aws::Ecs.services(my.ecs_cluster_name, [service]).first.events.each do |e|
      break if e.id == last_seen
      unseen.unshift(e)
    end
    unseen.each(&method(:print_event))
    last_seen = unseen.last.id unless unseen.empty?
  end
end
tasks() click to toggle source
# File lib/stax/mixin/ecs.rb, line 189
def tasks
  my.ecs_services.each do |s|
    name = s.physical_resource_id.split('/').last
    debug("Tasks for service #{name}")
    Aws::Ecs.tasks(
      cluster: my.ecs_cluster_name,
      service_name: s.physical_resource_id,
      desired_status: options[:status].upcase,
    ).map { |t|
      [
        t.task_arn.split('/').last,
        t.task_definition_arn.split('/').last,
        t.container_instance_arn&.split('/')&.last || '--',
        color(t.last_status, COLORS),
        "(#{t.desired_status})",
        t.started_by,
      ]
    }.tap(&method(:print_table))
  end
end