Class: Cucloud::EcsUtils
- Inherits:
-
Object
- Object
- Cucloud::EcsUtils
- Defined in:
- lib/cucloud/ecs_utils.rb
Overview
ECSUtils class - anything ecs related goes here!
Defined Under Namespace
Classes: InvalidTaskDefinitionError
Instance Method Summary collapse
-
#generate_td_options_hash(task_definition) ⇒ Hash
Generate task definition options hash (that can be sumitted to AWS SDK) from existing definition.
-
#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.
-
#get_service(cluster_name, service_name) ⇒ Aws::ECS::Types::Service
Get definition for service based on service name.
-
#get_task_definition(family_prefix, revision = nil) ⇒ Aws::ECS::Types::TaskDefinition
Get task definition details for given revision of task.
-
#initialize(ecs_client = Aws::ECS::Client.new) ⇒ EcsUtils
constructor
Constructor for EcsUtils class.
-
#register_task_definition(task_definition) ⇒ Hash
Create new task definition in AWS.
-
#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.
Constructor Details
#initialize(ecs_client = Aws::ECS::Client.new) ⇒ EcsUtils
Constructor for EcsUtils class
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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cucloud/ecs_utils.rb', line 48 def (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 = task_definition.to_h # request cannot have arn, revision or keys with empty values .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
35 36 37 38 39 40 41 42 43 |
# File 'lib/cucloud/ecs_utils.rb', line 35 def (task_definition, container_name, new_image_dtr_uri) = (task_definition) # Definitions can contain more than one container. Update the targetted def. target_container_index = [:container_definitions].index { |c| c[:name] == container_name } [:container_definitions][target_container_index][:image] = new_image_dtr_uri end |
#get_service(cluster_name, service_name) ⇒ Aws::ECS::Types::Service
Get definition for service based on service name
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
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
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
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 |