Class: EcsDeploy::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_deploy/service.rb

Defined Under Namespace

Classes: TooManyAttemptsError

Constant Summary collapse

CHECK_INTERVAL =
5
MAX_DESCRIBE_SERVICES =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster:, service_name:, task_definition_name: nil, revision: nil, load_balancers: nil, desired_count: nil, deployment_configuration: {maximum_percent: 200, minimum_healthy_percent: 100}, launch_type: nil, placement_constraints: [], placement_strategy: [], network_configuration: nil, health_check_grace_period_seconds: nil, scheduling_strategy: 'REPLICA', enable_ecs_managed_tags: nil, tags: nil, propagate_tags: nil, region: nil, delete: false, enable_execute_command: false) ⇒ Service

Returns a new instance of Service.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ecs_deploy/service.rb', line 12

def initialize(
  cluster:, service_name:, task_definition_name: nil, revision: nil,
  load_balancers: nil,
  desired_count: nil, deployment_configuration: {maximum_percent: 200, minimum_healthy_percent: 100},
  launch_type: nil,
  placement_constraints: [],
  placement_strategy: [],
  network_configuration: nil,
  health_check_grace_period_seconds: nil,
  scheduling_strategy: 'REPLICA',
  enable_ecs_managed_tags: nil,
  tags: nil,
  propagate_tags: nil,
  region: nil,
  delete: false,
  enable_execute_command: false
)
  @cluster = cluster
  @service_name = service_name
  @task_definition_name = task_definition_name || service_name
  @load_balancers = load_balancers
  @desired_count = desired_count
  @deployment_configuration = deployment_configuration
  @launch_type = launch_type
  @placement_constraints = placement_constraints
  @placement_strategy = placement_strategy
  @network_configuration = network_configuration
  @health_check_grace_period_seconds = health_check_grace_period_seconds
  @scheduling_strategy = scheduling_strategy
  @revision = revision
  @enable_ecs_managed_tags = enable_ecs_managed_tags
  @tags = tags
  @propagate_tags = propagate_tags
  @enable_execute_command = enable_execute_command

  @response = nil

  region ||= EcsDeploy.config.default_region
  @client = region ? Aws::ECS::Client.new(region: region) : Aws::ECS::Client.new
  @region = @client.config.region

  @delete = delete
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



10
11
12
# File 'lib/ecs_deploy/service.rb', line 10

def cluster
  @cluster
end

#deleteObject (readonly)

Returns the value of attribute delete.



10
11
12
# File 'lib/ecs_deploy/service.rb', line 10

def delete
  @delete
end

#regionObject (readonly)

Returns the value of attribute region.



10
11
12
# File 'lib/ecs_deploy/service.rb', line 10

def region
  @region
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



10
11
12
# File 'lib/ecs_deploy/service.rb', line 10

def service_name
  @service_name
end

Class Method Details

.wait_all_running(services) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ecs_deploy/service.rb', line 145

def self.wait_all_running(services)
  services.group_by { |s| [s.cluster, s.region] }.flat_map do |(cl, region), ss|
    client = Aws::ECS::Client.new(region: region)
    ss.reject(&:delete).map(&:service_name).each_slice(MAX_DESCRIBE_SERVICES).map do |chunked_service_names|
      Thread.new do
        EcsDeploy.config.ecs_wait_until_services_stable_max_attempts.times do
          EcsDeploy.logger.info "wait service stable [#{chunked_service_names.join(", ")}]"
          resp = client.describe_services(cluster: cl, services: chunked_service_names)
          resp.services.each do |s|
            # cf. https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-ecs/lib/aws-sdk-ecs/waiters.rb#L91-L96
            if s.deployments.size == 1 && s.running_count == s.desired_count
              chunked_service_names.delete(s.service_name)
            end
          end
          break if chunked_service_names.empty?
          sleep EcsDeploy.config.ecs_wait_until_services_stable_delay
        end
        raise TooManyAttemptsError unless chunked_service_names.empty?
      end
    end
  end.each(&:join)
end

Instance Method Details

#current_task_definition_arnObject



56
57
58
59
# File 'lib/ecs_deploy/service.rb', line 56

def current_task_definition_arn
  res = @client.describe_services(cluster: @cluster, services: [@service_name])
  res.services[0].task_definition
end

#delete_serviceObject



114
115
116
117
118
119
120
121
# File 'lib/ecs_deploy/service.rb', line 114

def delete_service
  if @scheduling_strategy != 'DAEMON'
    @client.update_service(cluster: @cluster, service: @service_name, desired_count: 0)
    sleep 1
  end
  @client.delete_service(cluster: @cluster, service: @service_name)
  EcsDeploy.logger.info "delete service [#{@service_name}] [#{@region}] [#{Paint['OK', :green]}]"
end

#deployObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ecs_deploy/service.rb', line 61

def deploy
  res = @client.describe_services(cluster: @cluster, services: [@service_name])
  service_options = {
    cluster: @cluster,
    task_definition: task_definition_name_with_revision,
    deployment_configuration: @deployment_configuration,
    network_configuration: @network_configuration,
    health_check_grace_period_seconds: @health_check_grace_period_seconds,
    enable_execute_command: @enable_execute_command,
  }
  if res.services.select{ |s| s.status == 'ACTIVE' }.empty?
    return if @delete

    service_options.merge!({
      service_name: @service_name,
      desired_count: @desired_count.to_i,
      launch_type: @launch_type,
      placement_constraints: @placement_constraints,
      placement_strategy: @placement_strategy,
      enable_ecs_managed_tags: @enable_ecs_managed_tags,
      tags: @tags,
      propagate_tags: @propagate_tags,
    })

    if @load_balancers && EcsDeploy.config.ecs_service_role
      service_options.merge!({
        role: EcsDeploy.config.ecs_service_role,
      })
    end

    if @load_balancers
      service_options.merge!({
        load_balancers: @load_balancers,
      })
    end

    if @scheduling_strategy == 'DAEMON'
      service_options[:scheduling_strategy] = @scheduling_strategy
      service_options.delete(:desired_count)
    end
    @response = @client.create_service(service_options)
    EcsDeploy.logger.info "create service [#{@service_name}] [#{@region}] [#{Paint['OK', :green]}]"
  else
    return delete_service if @delete

    service_options.merge!({service: @service_name})
    service_options.merge!({desired_count: @desired_count}) if @desired_count
    update_tags(@service_name, @tags)
    @response = @client.update_service(service_options)
    EcsDeploy.logger.info "update service [#{@service_name}] [#{@region}] [#{Paint['OK', :green]}]"
  end
end

#update_tags(service_name, tags) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ecs_deploy/service.rb', line 123

def update_tags(service_name, tags)
  service_arn = @client.describe_services(cluster: @cluster, services: [service_name]).services.first.service_arn
  if service_arn.split('/').size == 2
    if tags
      EcsDeploy.logger.warn "#{service_name} doesn't support tagging operations, so tags are ignored. Long arn format must be used for tagging operations."
    end
    return
  end

  tags ||= []
  current_tag_keys = @client.list_tags_for_resource(resource_arn: service_arn).tags.map(&:key)
  deleted_tag_keys = current_tag_keys - tags.map { |t| t[:key] }

  unless deleted_tag_keys.empty?
    @client.untag_resource(resource_arn: service_arn, tag_keys: deleted_tag_keys)
  end

  unless tags.empty?
    @client.tag_resource(resource_arn: service_arn, tags: tags)
  end
end