Class: EcsDeploy::AutoScaler::AutoScalingConfig

Inherits:
Struct
  • Object
show all
Includes:
ConfigBase
Defined in:
lib/ecs_deploy/auto_scaler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigBase

#initialize

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer

Returns:

  • (Object)

    the current value of buffer



300
301
302
# File 'lib/ecs_deploy/auto_scaler.rb', line 300

def buffer
  @buffer
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



300
301
302
# File 'lib/ecs_deploy/auto_scaler.rb', line 300

def name
  @name
end

#regionObject

Returns the value of attribute region

Returns:

  • (Object)

    the current value of region



300
301
302
# File 'lib/ecs_deploy/auto_scaler.rb', line 300

def region
  @region
end

Instance Method Details

#clear_clientObject



311
312
313
# File 'lib/ecs_deploy/auto_scaler.rb', line 311

def clear_client
  Thread.current["ecs_auto_scaler_auto_scaling_#{region}"] = nil
end

#clear_ec2_clientObject



323
324
325
# File 'lib/ecs_deploy/auto_scaler.rb', line 323

def clear_ec2_client
  Thread.current["ecs_auto_scaler_ec2_#{region}"] = nil
end

#clientObject



303
304
305
306
307
308
309
# File 'lib/ecs_deploy/auto_scaler.rb', line 303

def client
  Thread.current["ecs_auto_scaler_auto_scaling_#{region}"] ||= 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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/ecs_deploy/auto_scaler.rb', line 384

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)
  clear_client
  clear_ec2_client
end

#detach_and_terminate_orphan_instances(service_config) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/ecs_deploy/auto_scaler.rb', line 405

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)
  clear_client
  clear_ec2_client
end

#ec2_clientObject



315
316
317
318
319
320
321
# File 'lib/ecs_deploy/auto_scaler.rb', line 315

def ec2_client
  Thread.current["ecs_auto_scaler_ec2_#{region}"] ||= 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



327
328
329
330
331
332
333
334
335
336
# File 'lib/ecs_deploy/auto_scaler.rb', line 327

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



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
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/ecs_deploy/auto_scaler.rb', line 338

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)
  clear_client
end