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: [], capacity_provider_strategy: nil, 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
55
56
# 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: [],
  capacity_provider_strategy: nil,
  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
  @capacity_provider_strategy = capacity_provider_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

#deploy_started_atObject (readonly)

Returns the value of attribute deploy_started_at.



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

def deploy_started_at
  @deploy_started_at
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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ecs_deploy/service.rb', line 182

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(", ")}] [#{cl}]"
          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
            service = ss.detect {|sc| sc.service_name == s.service_name }
            service.log_events(s)
          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



58
59
60
61
# File 'lib/ecs_deploy/service.rb', line 58

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

#delete_serviceObject



141
142
143
144
145
146
147
148
# File 'lib/ecs_deploy/service.rb', line 141

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}] [#{@cluster}] [#{@region}] [#{Paint['OK', :green]}]"
end

#deployObject



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
113
114
115
116
117
118
119
120
121
122
# File 'lib/ecs_deploy/service.rb', line 63

def deploy
  @deploy_started_at = Time.now
  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,
    capacity_provider_strategy: @capacity_provider_strategy,
    enable_execute_command: @enable_execute_command,
    enable_ecs_managed_tags: @enable_ecs_managed_tags,
    placement_constraints: @placement_constraints,
    placement_strategy: @placement_strategy,
  }

  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 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,
      tags: @tags,
      propagate_tags: @propagate_tags,
    })

    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}] [#{@cluster}] [#{@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
    service_options.merge!({propagate_tags: @propagate_tags}) if @propagate_tags

    current_service = res.services[0]
    service_options.merge!({force_new_deployment: true}) if need_force_new_deployment?(current_service)

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

#log_events(ecs_service) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/ecs_deploy/service.rb', line 172

def log_events(ecs_service)
  ecs_service.events.sort_by(&:created_at).each do |e|
    next if e.created_at <= deploy_started_at
    next if @last_event && e.created_at <= @last_event.created_at

    EcsDeploy.logger.info e.message
    @last_event = e
  end
end

#update_tags(service_name, tags) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ecs_deploy/service.rb', line 150

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