Class: EcsDeploy::Service

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

Constant Summary collapse

CHECK_INTERVAL =
5

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}, region: nil) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ecs_deploy/service.rb', line 8

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},
  region: nil
)
  @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
  @revision = revision
  @region = region || EcsDeploy.config.default_region || ENV["AWS_DEFAULT_REGION"]
  @response = nil

  @client = Aws::ECS::Client.new(region: @region)
end

Instance Attribute Details

#cluster ⇒ Object (readonly)

Returns the value of attribute cluster.



6
7
8
# File 'lib/ecs_deploy/service.rb', line 6

def cluster
  @cluster
end

#region ⇒ Object (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/ecs_deploy/service.rb', line 6

def region
  @region
end

#service_name ⇒ Object (readonly)

Returns the value of attribute service_name.



6
7
8
# File 'lib/ecs_deploy/service.rb', line 6

def service_name
  @service_name
end

Class Method Details

.wait_all_running(services) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ecs_deploy/service.rb', line 74

def self.wait_all_running(services)
  services.group_by { |s| [s.cluster, s.region] }.each do |(cl, region), ss|
    client = Aws::ECS::Client.new(region: region)
    service_names = ss.map(&:service_name)
    client.wait_until(:services_stable, cluster: cl, services: service_names) do |w|
      w.before_attempt do
        EcsDeploy.logger.info "wait service stable [#{service_names.join(", ")}]"
      end
    end
  end
end

Instance Method Details

#current_task_definition_arn ⇒ Object



27
28
29
30
# File 'lib/ecs_deploy/service.rb', line 27

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

#deploy ⇒ Object



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
57
58
# File 'lib/ecs_deploy/service.rb', line 32

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,
  }
  if res.services.empty?
    service_options.merge!({
      service_name: @service_name,
      desired_count: @desired_count.to_i,
    })
    if @load_balancers
      service_options.merge!({
        role: EcsDeploy.config.ecs_service_role,
        load_balancers: @load_balancers,
      })
    end
    @response = @client.create_service(service_options)
    EcsDeploy.logger.info "create service [#{@service_name}] [#{@region}] [#{Paint['OK', :green]}]"
  else
    service_options.merge!({service: @service_name})
    service_options.merge!({desired_count: @desired_count}) if @desired_count
    @response = @client.update_service(service_options)
    EcsDeploy.logger.info "update service [#{@service_name}] [#{@region}] [#{Paint['OK', :green]}]"
  end
end

#wait_running ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ecs_deploy/service.rb', line 60

def wait_running
  return if @response.nil?

  service = @response.service

  @client.wait_until(:services_stable, cluster: @cluster, services: [service.service_name]) do |w|
    w.delay = 10

    w.before_attempt do
      EcsDeploy.logger.info "wait service stable [#{service.service_name}]"
    end
  end
end