class Ecs::Easy::Cluster::Base

Attributes

cfn_client[R]
configure[R]
ecs_client[R]
instance[RW]
max_instances[RW]
min_instances[RW]
name[R]
stack_name[R]

Public Class Methods

new( name, configure ) { |self| ... } click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 7
def initialize( name, configure )
  @name = name
  @configure = configure
  @ecs_client = Aws::ECS::Client.new(
    region: configure.region,
    credentials: configure.credentials
  )
  @stack_name = configure.cfn_stack_name_prefix + name
  @cfn_client = Aws::CloudFormation::Client.new(
    region: configure.region,
    credentials: configure.credentials
  )

  # default
  @min_instances = 1
  @max_instances = 1

  yield( self ) if block_given?
end

Public Instance Methods

destroy!() click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 105
def destroy!
  cfn_client.delete_stack(stack_name: stack_name)
  cfn_client.wait_until(
    :stack_delete_complete, 
    stack_name: stack_name
  )
end
exists?() click to toggle source

Check if the cluster exists A cluster existence should be decided as cloudformation stack existence

# File lib/ecs/easy/cluster/base.rb, line 29
def exists?
  res = cfn_client.describe_stacks( stack_name: stack_name )
  return res.stacks.length > 0
rescue => e
  return false
end
num_instances() click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 58
def num_instances
  ecs_client.list_container_instances(cluster: name).container_instance_arns.length
end
ready?() click to toggle source

Check if all the container instances on the cluster are ready

# File lib/ecs/easy/cluster/base.rb, line 37
def ready?
  return false unless exists?
  instance_arns = ecs_client.list_container_instances(cluster: name).container_instance_arns
  return false if instance_arns.nil? or instance_arns.empty?
  res = ecs_client.describe_container_instances(
    cluster: name,
    container_instances: instance_arns
  )
  # unless res.respond_to?("container_instances") or
  #   res.container_instances.nil?
  #   return false
  # end
  res.container_instances.all? {|i| i.status == "ACTIVE" }
end
run_task!( task_definition, overrides={} ) click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 75
def run_task!( task_definition, overrides={} )
  res = ecs_client.run_task(
    cluster: name,
    task_definition: task_definition,
    overrides: overrides
  )
end
scale!() click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 113
def scale!
  size = (num_instances+1 <= max_instances) ? num_instances+1 : max_instances

  params = instance.cfn_parameters( name, "AsgMaxSize" => size.to_s)
  template_body = instance.template_body
  cfn_client.update_stack(
    stack_name: stack_name,
    template_body: template_body,
    parameters: params,
    capabilities: ["CAPABILITY_IAM"],
  )
  cfn_client.wait_until(
    :stack_update_complete, 
    stack_name: stack_name
  )

  # Check the scale completion every 2 seconds until max 30 times
  30.times do
    return true if num_instances == size
    sleep 2
  end

  return false
rescue Aws::Waiters::Errors::WaiterFailed => e
  puts e
end
shrink!() click to toggle source

Deregister some container instances if they are idling TODO: Support idling check of container instances

# File lib/ecs/easy/cluster/base.rb, line 142
def shrink!
  params = instance.cfn_parameters( name, "AsgMaxSize" => min_instances.to_s)
  template_body = instance.template_body
  cfn_client.update_stack(
    stack_name: stack_name,
    template_body: template_body,
    parameters: params,
    capabilities: ["CAPABILITY_IAM"],
  )
  cfn_client.wait_until(
    :stack_update_complete, 
    stack_name: stack_name
  )
rescue Aws::Waiters::Errors::WaiterFailed => e
  puts e
end
task_exists?( task ) click to toggle source

Check if the task exists on the cluster

# File lib/ecs/easy/cluster/base.rb, line 53
def task_exists?( task )
  task_arns = ecs_client.list_tasks(cluster: name).task_arns
  task_arns.include?( task )
end
up!() click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 83
def up!
  unless exists?
    ecs_client.create_cluster( cluster_name: name )

    params = instance.cfn_parameters( name, "AsgMaxSize" => min_instances.to_s)
    template_body = instance.template_body
    cfn_client.create_stack(
      stack_name: stack_name,
      template_body: template_body,
      parameters: params,
      capabilities: ["CAPABILITY_IAM"],
      on_failure: "DELETE",
    )
    cfn_client.wait_until(
      :stack_create_complete, 
      stack_name: stack_name
    )
  end

  return exists?
end
wait_until_ready( retry_count=30, sleep_sec=2 ) click to toggle source

Wait until all the container instances ready

# File lib/ecs/easy/cluster/base.rb, line 63
def wait_until_ready( retry_count=30, sleep_sec=2 )
  retry_count.times do
    break if ready?
    sleep sleep_sec
  end
end
wait_until_task_running( arn ) click to toggle source
# File lib/ecs/easy/cluster/base.rb, line 70
def wait_until_task_running( arn )
  raise "Task Not Found. Possibly the task is already stopped." unless task_exists?( arn )
  ecs_client.wait_until(:tasks_running, tasks: [arn])
end