class KnuckleCluster::TaskRegistry

Attributes

agent_registry[R]
all_containers[R]
cluster_name[R]
ecs_client[R]

Public Class Methods

new(ecs_client:, cluster_name:, agent_registry:, hide: {}) click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 6
def initialize(ecs_client:, cluster_name:, agent_registry:, hide: {})
  @ecs_client     = ecs_client
  @cluster_name   = cluster_name
  @agent_registry = agent_registry
  @hide           = hide
end

Public Instance Methods

containers() click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 17
def containers
  tasks && all_containers
end
containers_where(task:) click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 25
def containers_where(task:)
  containers_by_task[task]
end
tasks() click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 13
def tasks
  @tasks ||= load_tasks.compact
end
where(container_instance_arn:) click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 21
def where(container_instance_arn:)
  tasks_by_container_instance_arn[container_instance_arn]
end

Private Instance Methods

containers_by_task() click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 85
def containers_by_task
  @containers_by_task ||= all_containers.group_by(&:task)
end
load_tasks() click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 33
def load_tasks
  task_arns = ecs_client.list_tasks(cluster: cluster_name).task_arns
  task_ids  = task_arns.map { |x| x[/.*\/(.*)/,1] }

  return [] if task_ids.empty?

  @all_containers = []
  index = 0

  ecs_client.describe_tasks(tasks: task_ids, cluster: cluster_name).tasks.flat_map do |task|
    agent = agent_registry.find_by(container_instance_arn: task.container_instance_arn)

    task_name = task.task_definition_arn[/.*\/(.*):\d/,1]

    if @hide[:task]
      regex = Regexp.new(@hide[:task])
      next if regex.match(task_name)
    end

    #Exclude any tasks that have no connectable containers
    containers = task.containers
    if @hide[:container]
      regex = Regexp.new(@hide[:container])
      containers.reject!{|container| regex.match(container.name)}
    end
    next unless containers.any?

    Task.new(
      arn:                    task.task_arn,
      container_instance_arn: task.container_instance_arn,
      agent:                  agent,
      definition:             task.task_definition_arn[/.*\/(.*):.*/,1],
      name:                   task_name,
      task_registry:          self,
    ).tap do |new_task|
      containers.each do |container|
        index += 1

        all_containers << Container.new(
          index: index,
          name:  container.name,
          task:  new_task,
        )
      end
    end
  end
end
tasks_by_container_instance_arn() click to toggle source
# File lib/knuckle_cluster/task_registry.rb, line 81
def tasks_by_container_instance_arn
  @tasks_by_container_instance_arn ||= tasks.group_by(&:container_instance_arn)
end