Class: EcsDeploy::Service
- Inherits:
-
Object
- Object
- EcsDeploy::Service
- 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
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
-
#delete ⇒ Object
readonly
Returns the value of attribute delete.
-
#deploy_started_at ⇒ Object
readonly
Returns the value of attribute deploy_started_at.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#service_name ⇒ Object
readonly
Returns the value of attribute service_name.
Class Method Summary collapse
Instance Method Summary collapse
- #current_task_definition_arn ⇒ Object
- #delete_service ⇒ Object
- #deploy ⇒ Object
-
#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
constructor
A new instance of Service.
- #log_events(ecs_service) ⇒ Object
- #update_tags(service_name, tags) ⇒ Object
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 57 |
# 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_execute_command = enable_execute_command @response = nil region ||= EcsDeploy.config.default_region params ||= EcsDeploy.config.ecs_client_params @client = region ? Aws::ECS::Client.new(params.merge(region: region)) : Aws::ECS::Client.new(params) @region = @client.config.region @delete = delete end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
10 11 12 |
# File 'lib/ecs_deploy/service.rb', line 10 def cluster @cluster end |
#delete ⇒ Object (readonly)
Returns the value of attribute delete.
10 11 12 |
# File 'lib/ecs_deploy/service.rb', line 10 def delete @delete end |
#deploy_started_at ⇒ Object (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 |
#region ⇒ Object (readonly)
Returns the value of attribute region.
10 11 12 |
# File 'lib/ecs_deploy/service.rb', line 10 def region @region end |
#service_name ⇒ Object (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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/ecs_deploy/service.rb', line 183 def self.wait_all_running(services) services.group_by { |s| [s.cluster, s.region] }.flat_map do |(cl, region), ss| params ||= EcsDeploy.config.ecs_client_params client = Aws::ECS::Client.new(params.merge(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_arn ⇒ Object
59 60 61 62 |
# File 'lib/ecs_deploy/service.rb', line 59 def current_task_definition_arn res = @client.describe_services(cluster: @cluster, services: [@service_name]) res.services[0].task_definition end |
#delete_service ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/ecs_deploy/service.rb', line 142 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 |
#deploy ⇒ Object
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 123 |
# File 'lib/ecs_deploy/service.rb', line 64 def deploy @deploy_started_at = Time.now res = @client.describe_services(cluster: @cluster, services: [@service_name]) = { 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: , placement_constraints: @placement_constraints, placement_strategy: @placement_strategy, } if @load_balancers && EcsDeploy.config.ecs_service_role .merge!({ role: EcsDeploy.config.ecs_service_role, }) end if @load_balancers .merge!({ load_balancers: @load_balancers, }) end if res.services.select{ |s| s.status == 'ACTIVE' }.empty? return if @delete .merge!({ service_name: @service_name, desired_count: @desired_count.to_i, launch_type: @launch_type, tags: , propagate_tags: , }) if @scheduling_strategy == 'DAEMON' [:scheduling_strategy] = @scheduling_strategy .delete(:desired_count) end @response = @client.create_service() EcsDeploy.logger.info "create service [#{@service_name}] [#{@cluster}] [#{@region}] [#{Paint['OK', :green]}]" else return delete_service if @delete .merge!({service: @service_name}) .merge!({desired_count: @desired_count}) if @desired_count .merge!({propagate_tags: }) if current_service = res.services[0] .merge!({force_new_deployment: true}) if need_force_new_deployment?(current_service) (@service_name, ) @response = @client.update_service() EcsDeploy.logger.info "update service [#{@service_name}] [#{@cluster}] [#{@region}] [#{Paint['OK', :green]}]" end end |
#log_events(ecs_service) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/ecs_deploy/service.rb', line 173 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. @last_event = e end end |
#update_tags(service_name, tags) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ecs_deploy/service.rb', line 151 def (service_name, ) service_arn = @client.describe_services(cluster: @cluster, services: [service_name]).services.first.service_arn if service_arn.split('/').size == 2 if 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 ||= [] current_tag_keys = @client.(resource_arn: service_arn)..map(&:key) deleted_tag_keys = current_tag_keys - .map { |t| t[:key] } unless deleted_tag_keys.empty? @client.untag_resource(resource_arn: service_arn, tag_keys: deleted_tag_keys) end unless .empty? @client.tag_resource(resource_arn: service_arn, tags: ) end end |