Class: Ufo::CLI::Scale

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/cli/scale.rb

Instance Attribute Summary

Attributes inherited from Base

#task_definition

Instance Method Summary collapse

Methods included from Utils::Sure

#sure?

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Methods included from Ufo::Concerns

#build, #deploy, #info, #ps

Methods included from Ufo::Concerns::Names

#names

Methods included from AwsServices

#acm, #applicationautoscaling, #aws_options, #cfn, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb, #s3, #ssm_client, #waf_client

Methods included from AwsServices::Concerns

#find_stack, #find_stack_resources, #stack_resources, #status, #task_definition_arns

Constructor Details

#initialize(options = {}) ⇒ Scale

Returns a new instance of Scale.



5
6
7
8
9
10
# File 'lib/ufo/cli/scale.rb', line 5

def initialize(options={})
  super
  @desired = options[:desired]
  @min = options[:min]
  @max = options[:max]
end

Instance Method Details

#register_scalable_target(scalable_target) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ufo/cli/scale.rb', line 55

def register_scalable_target(scalable_target)
  # service/dev/app1-web-dev-EcsService-Q0XkN6VtxGWv|ecs:service:DesiredCount|ecs
  return unless scalable_target && scalable_target.physical_resource_id # stack still creating
  resource_id, scalable_dimension, service_namespace = scalable_target.physical_resource_id.split('|')
  applicationautoscaling.register_scalable_target(
    max_capacity: @max,
    min_capacity: @min,
    resource_id: resource_id,
    scalable_dimension: scalable_dimension,
    service_namespace: service_namespace,
  )
rescue Aws::ApplicationAutoScaling::Errors::ValidationException => e
  logger.error "ERROR: #{e.class} #{e.message}".color(:red)
  exit 1
end

#set_autoscalingObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ufo/cli/scale.rb', line 43

def set_autoscaling
  return unless @min || @max
  scalable_target = stack_resources.find do |r|
    r.logical_resource_id == "ScalingTarget"
  end
  register_scalable_target(scalable_target)
  to = []
  to << "min: #{@min}" if @min
  to << "max: #{@max}" if @max
  logger.info "Configured autoscaling to #{to.join(' ')}"
end

#set_desired_countObject



33
34
35
36
37
38
39
40
41
# File 'lib/ufo/cli/scale.rb', line 33

def set_desired_count
  return unless @desired
  ecs.update_service(
    service: service.service_name,
    cluster: @cluster,
    desired_count: @desired
  )
  logger.info "Configured desired count to #{@desired}"
end

#updateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ufo/cli/scale.rb', line 12

def update
  unless service?
    logger.error "ERROR: Unable to find the #{service}.".color(:red)
    logger.error "Are you sure you are trying to scale the right app?"
    exit 1
  end

  unless @desired || @min || @max
    logger.info <<~EOL
      No --desired --min or --max options provided
      Not taking any actions
    EOL
    return
  end

  logger.info "Configuring scaling settings for #{@stack_name}"
  set_desired_count
  set_autoscaling
  warning
end

#warningObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ufo/cli/scale.rb', line 71

def warning
  autoscaling = Ufo.config.autoscaling
  return if autoscaling.manual_changes.warning == false or autoscaling.manual_changes.retain
  logger.info <<~EOL
    Note: The settings are temporary.
    They can be overwritten in the next `ufo ship` deploy.

    You can turn off this warning with

        config.autoscaling.manual_changes.warning = false

    Or you can use the

        config.autoscaling.manual_changes.retain = true

    For considerations, see: https://ufoships.com/docs/features/autoscaling/
  EOL
end