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, elb_name: nil, elb_service_port: nil, elb_healthcheck_port: nil, elb_container_name: nil, desired_count: nil, deployment_configuration: {maximum_percent: 200, minimum_healthy_percent: 100}, region: nil) ⇒ Service



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

def initialize(
  cluster:, service_name:, task_definition_name: nil, revision: nil,
  elb_name: nil, elb_service_port: nil, elb_healthcheck_port: nil, elb_container_name: 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
  @elb_name = elb_name
  @elb_service_port = elb_service_port
  @elb_healthcheck_port = elb_healthcheck_port
  @elb_container_name = elb_container_name
  @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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ecs_deploy/service.rb', line 84

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



30
31
32
33
# File 'lib/ecs_deploy/service.rb', line 30

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

#deploy ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ecs_deploy/service.rb', line 35

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 @elb_name
      service_options.merge!({
        role: EcsDeploy.config.ecs_service_role,
        load_balancers: [
          {
            load_balancer_name: @elb_name,
            container_name: @elb_container_name,
            container_port: @elb_service_port,
          }
        ],
      })
    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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ecs_deploy/service.rb', line 69

def wait_running
  return if @response.nil?

  service = @response.service
  deployment = nil

  @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