Class: Cucloud::EcsUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/cucloud/ecs_utils.rb

Overview

ECSUtils class - anything ecs related goes here!

Defined Under Namespace

Classes: InvalidTaskDefinitionError

Instance Method Summary collapse

Constructor Details

#initialize(ecs_client = Aws::ECS::Client.new) ⇒ EcsUtils

Constructor for EcsUtils class

Parameters:

  • ecs_client (Aws::ECS::Client) (defaults to: Aws::ECS::Client.new)

    AWS ECS SDK Client



10
11
12
13
# File 'lib/cucloud/ecs_utils.rb', line 10

def initialize(ecs_client = Aws::ECS::Client.new)
  ## DI for testing purposes
  @ecs = ecs_client
end

Instance Method Details

#generate_td_options_hash(task_definition) ⇒ Hash

Generate task definition options hash (that can be sumitted to AWS SDK) from existing definition

Parameters:

  • task_definition (Aws::ECS::Types::TaskDefinition)

    Task definition object

Returns:

  • (Hash)

    An options hash that can be submitted via AWS sdk



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cucloud/ecs_utils.rb', line 48

def generate_td_options_hash(task_definition)
  # make sure we got a valid launch config
  unless task_definition.is_a? Aws::ECS::Types::TaskDefinition
    raise InvalidTaskDefinitionError.new, 'Provided task definition is not valid'
  end

  # convert to hash (required for aws sdk) and update necessary values
  options_hash = task_definition.to_h

  # request cannot have arn, revision or keys with empty values
  options_hash.delete_if do |key, value|
    key == :task_definition_arn || key == :revision || key == :status || key == :requires_attributes || value == ''
  end
end

#generate_td_options_hash_with_new_image(task_definition, container_name, new_image_dtr_uri) ⇒ Hash

Generate task definition options hash (that can be sumitted to AWS SDK) w/ new image

Parameters:

  • task_definition (Aws::ECS::Types::TaskDefinition)

    Task definition object

  • container_name (String)

    Name of container for which image should be updated

  • new_image_dtr_uri (String)

    Location of new image in registry

Returns:

  • (Hash)

    An options hash that can be submitted via AWS sdk



35
36
37
38
39
40
41
42
43
# File 'lib/cucloud/ecs_utils.rb', line 35

def generate_td_options_hash_with_new_image(task_definition, container_name, new_image_dtr_uri)
  options_hash = generate_td_options_hash(task_definition)

  # Definitions can contain more than one container. Update the targetted def.
  target_container_index = options_hash[:container_definitions].index { |c| c[:name] == container_name }
  options_hash[:container_definitions][target_container_index][:image] = new_image_dtr_uri

  options_hash
end

#get_service(cluster_name, service_name) ⇒ Aws::ECS::Types::Service

Get definition for service based on service name

Parameters:

  • cluster_name (String)

    Name of cluster on which this service is configured

  • service_name (String)

    Name of service

Returns:

  • (Aws::ECS::Types::Service)

    Service definition



81
82
83
84
# File 'lib/cucloud/ecs_utils.rb', line 81

def get_service(cluster_name, service_name)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/ECS/Client.html#describe_services-instance_method
  @ecs.describe_services(cluster: cluster_name, services: [service_name])[:services].first
end

#get_task_definition(family_prefix, revision = nil) ⇒ Aws::ECS::Types::TaskDefinition

Get task definition details for given revision of task. If revision is nil, return latest task

Parameters:

  • family_prefix (String)

    Task family prefix

  • revision (Integer) (defaults to: nil)

    Specific revision

Returns:

  • (Aws::ECS::Types::TaskDefinition)

    Task definition object



19
20
21
22
23
24
25
26
27
28
# File 'lib/cucloud/ecs_utils.rb', line 19

def get_task_definition(family_prefix, revision = nil)
  task = if revision.nil?
           family_prefix
         else
           "#{family_prefix}:#{revision}"
         end

  # https://docs.aws.amazon.com/sdkforruby/api/Aws/ECS/Client.html#describe_task_definition-instance_method
  @ecs.describe_task_definition(task_definition: task)['task_definition']
end

#register_task_definition(task_definition) ⇒ Hash

Create new task definition in AWS

Parameters:

  • options (Hash)

    Options hash to be passed along in request

Returns:

  • (Hash)

    Hash w/ task definition arn, family and revision



66
67
68
69
70
71
72
73
74
75
# File 'lib/cucloud/ecs_utils.rb', line 66

def register_task_definition(task_definition)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/ECS/Client.html#register_task_definition-instance_method
  new_def = @ecs.register_task_definition(task_definition)['task_definition']

  {
    arn: new_def['task_definition_arn'],
    family: new_def['family'],
    revision: new_def['revision']
  }
end

#update_service_task_definition!(cluster_name, service_name, task_arn) ⇒ Aws::ECS::Types::Service

Update the task definition associated with a service - this effectively deploys new task on service

Parameters:

  • cluster_name (String)

    Name of cluster on which this service is configured

  • service_name (String)

    Name of service

  • task_arn (String)

    Task ARN to be used by service

Returns:

  • (Aws::ECS::Types::Service)

    Updated service



91
92
93
94
95
96
# File 'lib/cucloud/ecs_utils.rb', line 91

def update_service_task_definition!(cluster_name, service_name, task_arn)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/ECS/Client.html#update_service-instance_method
  @ecs.update_service(cluster: cluster_name,
                      service: service_name,
                      task_definition: task_arn)['service']
end