Class: Aerosol::AutoScaling

Inherits:
Object
  • Object
show all
Includes:
AWSModel, Dockly::Util::Logger::Mixin
Defined in:
lib/aerosol/auto_scaling.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AWSModel

#create, #default_identifier, #destroy, included, #namespaced_name

Constructor Details

#initialize(options = {}, &block) ⇒ AutoScaling

Returns a new instance of AutoScaling.



13
14
15
16
17
18
19
20
21
# File 'lib/aerosol/auto_scaling.rb', line 13

def initialize(options={}, &block)
  tag = options.delete(:tag)
  super(options, &block)

  tags.merge!(tag) unless tag.nil?

  tags["GitSha"] ||= Aerosol::Util.git_sha
  tags["Deploy"] ||= namespaced_name
end

Class Method Details

.latest_for_tag(key, value) ⇒ Object



132
133
134
135
# File 'lib/aerosol/auto_scaling.rb', line 132

def self.latest_for_tag(key, value)
  all.select  { |group| group.tags[key] == value }
     .sort_by { |group| group.created_time }.last
end

.request_allObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/aerosol/auto_scaling.rb', line 119

def self.request_all
  next_token = nil
  asgs = []

  begin
    new_asgs = request_all_for_token(next_token)
    asgs.concat(new_asgs.auto_scaling_groups)
    next_token = new_asgs.next_token
  end until next_token.nil?

  asgs
end

.request_all_for_token(next_token) ⇒ Object



114
115
116
117
# File 'lib/aerosol/auto_scaling.rb', line 114

def self.request_all_for_token(next_token)
  options = next_token.nil? ? {} : { next_token: next_token }
  Aerosol::AWS.auto_scaling.describe_auto_scaling_groups(options)
end

Instance Method Details

#all_instancesObject



88
89
90
91
92
# File 'lib/aerosol/auto_scaling.rb', line 88

def all_instances
  conn.describe_auto_scaling_groups(auto_scaling_group_names: [*auto_scaling_group_name])
      .auto_scaling_groups.first
      .instances.map { |instance| Aerosol::Instance.from_hash(instance) }
end

#auto_scaling_group_name(arg = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/aerosol/auto_scaling.rb', line 23

def auto_scaling_group_name(arg = nil)
  if arg
    raise "You cannot set the auto_scaling_group_name directly" unless from_aws
    @auto_scaling_group_name = arg
  else
    @auto_scaling_group_name || default_identifier
  end
end

#create!Object



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
# File 'lib/aerosol/auto_scaling.rb', line 41

def create!
  ensure_present! :max_size, :min_size
  raise 'availability_zones or vpc_zone_identifier must be set' if [availability_zones, vpc_zone_identifier].none?
  raise 'launch_configuration or launch_template must be set' if [launch_configuration, launch_template].none?

  info "creating auto scaling group"
  launch_details = create_launch_details

  info self.inspect

  conn.create_auto_scaling_group({
    auto_scaling_group_name: auto_scaling_group_name,
    availability_zones: [*availability_zones],
    max_size: max_size,
    min_size: min_size
  }
    .merge(create_options)
    .merge(launch_details)
  )

  conn.wait_until(:group_in_service, auto_scaling_group_names: [auto_scaling_group_name]) do |waiter|
    waiter.before_wait do |attempt_count, _response|
      info "Wait count #{attempt_count} of #{waiter.max_attempts} for #{auto_scaling_group_name} to be in service"
    end
  end
end

#deleting?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/aerosol/auto_scaling.rb', line 80

def deleting?
  asgs = conn.describe_auto_scaling_groups(auto_scaling_group_names: [auto_scaling_group_name]).auto_scaling_groups

  return true if asgs.empty?

  asgs.first.status.to_s.include?('Delete')
end

#destroy!Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aerosol/auto_scaling.rb', line 68

def destroy!
  info self.inspect
  conn.delete_auto_scaling_group(auto_scaling_group_name: auto_scaling_group_name, force_delete: true)
  begin
    (0..2).each { break if deleting?; sleep 1 }
    launch_configuration.destroy
  rescue => ex
    info "Launch Config: #{launch_configuration} for #{auto_scaling_group_name} was not deleted."
    info ex.message
  end
end

#exists?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/aerosol/auto_scaling.rb', line 32

def exists?
  info "auto_scaling: needed?: #{namespaced_name}: " +
       "checking for auto scaling group: #{auto_scaling_group_name}"
  exists = super
  info "auto scaling: needed?: #{namespaced_name}: " +
       "#{exists ? 'found' : 'did not find'} auto scaling group: #{auto_scaling_group_name}"
  exists
end

#tag(val) ⇒ Object



94
95
96
# File 'lib/aerosol/auto_scaling.rb', line 94

def tag(val)
  tags.merge!(val)
end

#tags(ary = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aerosol/auto_scaling.rb', line 98

def tags(ary=nil)
  if !ary.nil?
    if ary.is_a? Hash
      ary.each do |key, value|
        tag key => value
      end
    else
      ary.each do |struct|
        tag struct[:key] => struct[:value]
      end
    end
  else
    @tags ||= {}
  end
end

#to_sObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aerosol/auto_scaling.rb', line 137

def to_s
  %{Aerosol::AutoScaling { \
"auto_scaling_group_name" => "#{auto_scaling_group_name}", \
"availability_zones" => "#{availability_zones}", \
"min_size" => "#{min_size}", \
"max_size" => "#{max_size}", \
"default_cooldown" => "#{default_cooldown}", \
"desired_capacity" => "#{desired_capacity}", \
"health_check_grace_period" => "#{health_check_grace_period}", \
"health_check_type" => "#{health_check_type}", \
"load_balancer_names" => "#{load_balancer_names}", \
"placement_group" => "#{placement_group}", \
"tags" => #{tags.to_s}, \
"created_time" => "#{created_time}" \
"target_group_arns" => "#{target_group_arns}" \
}}
end