Class: EcsDeploy::AutoScaler::AutoScalingConfig
- Inherits:
-
Struct
- Object
- Struct
- EcsDeploy::AutoScaler::AutoScalingConfig
- Includes:
- ConfigBase
- Defined in:
- lib/ecs_deploy/auto_scaler.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#name ⇒ Object
Returns the value of attribute name.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #client ⇒ Object
- #detach_and_terminate_instances(instance_ids) ⇒ Object
- #detach_and_terminate_orphan_instances(service_config) ⇒ Object
- #ec2_client ⇒ Object
- #instances(reload: false) ⇒ Object
- #update_auto_scaling_group(total_service_count, service_config) ⇒ Object
Methods included from ConfigBase
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer
297 298 299 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 297 def buffer @buffer end |
#name ⇒ Object
Returns the value of attribute name
297 298 299 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 297 def name @name end |
#region ⇒ Object
Returns the value of attribute region
297 298 299 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 297 def region @region end |
Instance Method Details
#client ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 300 def client Aws::AutoScaling::Client.new( access_key_id: EcsDeploy.config.access_key_id, secret_access_key: EcsDeploy.config.secret_access_key, region: region ) end |
#detach_and_terminate_instances(instance_ids) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 372 def detach_and_terminate_instances(instance_ids) return if instance_ids.empty? client.detach_instances( auto_scaling_group_name: name, instance_ids: instance_ids, should_decrement_desired_capacity: true ) AutoScaler.logger.info "Detach instances from ASG #{name}: #{instance_ids.inspect}" sleep 3 ec2_client.terminate_instances(instance_ids: instance_ids) AutoScaler.logger.info "Terminated instances: #{instance_ids.inspect}" rescue => e AutoScaler.error_logger.error(e) end |
#detach_and_terminate_orphan_instances(service_config) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 391 def detach_and_terminate_orphan_instances(service_config) container_instance_ids = service_config.fetch_container_instances.map(&:ec2_instance_id) orphans = instances(reload: true).reject { |i| container_instance_ids.include?(i.instance_id) }.map(&:instance_id) return if orphans.empty? targets = ec2_client.describe_instances(instance_ids: orphans).reservations[0].instances.select do |i| (Time.now - i.launch_time) > 600 end detach_and_terminate_instances(targets.map(&:instance_id)) rescue => e AutoScaler.error_logger.error(e) end |
#ec2_client ⇒ Object
308 309 310 311 312 313 314 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 308 def ec2_client Aws::EC2::Client.new( access_key_id: EcsDeploy.config.access_key_id, secret_access_key: EcsDeploy.config.secret_access_key, region: region ) end |
#instances(reload: false) ⇒ Object
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 316 def instances(reload: false) if reload || @instances.nil? resp = client.describe_auto_scaling_groups({ auto_scaling_group_names: [name], }) @instances = resp.auto_scaling_groups[0].instances else @instances end end |
#update_auto_scaling_group(total_service_count, service_config) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/ecs_deploy/auto_scaler.rb', line 327 def update_auto_scaling_group(total_service_count, service_config) desired_capacity = total_service_count + buffer.to_i current_asg = client.describe_auto_scaling_groups({ auto_scaling_group_names: [name], }).auto_scaling_groups[0] if current_asg.desired_capacity > desired_capacity diff = current_asg.desired_capacity - desired_capacity container_instances = service_config.fetch_container_instances deregisterable_instances = container_instances.select do |i| i.pending_tasks_count == 0 && i.running_tasks_count == 0 end AutoScaler.logger.info "Fetch deregisterable instances: #{deregisterable_instances.map(&:ec2_instance_id).inspect}" deregistered_instance_ids = [] deregisterable_instances.each do |i| break if deregistered_instance_ids.size >= diff begin service_config.client.deregister_container_instance(cluster: service_config.cluster, container_instance: i.container_instance_arn, force: false) deregistered_instance_ids << i.ec2_instance_id rescue Aws::ECS::Errors::InvalidParameterException end end AutoScaler.logger.info "Deregistered instances: #{deregistered_instance_ids.inspect}" detach_and_terminate_instances(deregistered_instance_ids) AutoScaler.logger.info "Update auto scaling group \"#{name}\": desired_capacity -> #{desired_capacity}" elsif current_asg.desired_capacity < desired_capacity client.update_auto_scaling_group( auto_scaling_group_name: name, min_size: 0, max_size: [current_asg.max_size, desired_capacity].max, desired_capacity: desired_capacity, ) AutoScaler.logger.info "Update auto scaling group \"#{name}\": desired_capacity -> #{desired_capacity}" end rescue => e AutoScaler.error_logger.error(e) end |