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.



18
19
20
21
22
23
24
25
26
# File 'lib/aerosol/auto_scaling.rb', line 18

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



141
142
143
144
# File 'lib/aerosol/auto_scaling.rb', line 141

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

.request_allObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/aerosol/auto_scaling.rb', line 128

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



123
124
125
126
# File 'lib/aerosol/auto_scaling.rb', line 123

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



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

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.to_hash) }
end

#auto_scaling_group_name(arg = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/aerosol/auto_scaling.rb', line 28

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aerosol/auto_scaling.rb', line 46

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' unless launch_details

  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)


85
86
87
88
89
90
91
# File 'lib/aerosol/auto_scaling.rb', line 85

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aerosol/auto_scaling.rb', line 73

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_details.destroy
  rescue => ex
    info "Launch Details: #{launch_details} for #{auto_scaling_group_name} were not deleted."
    info ex.message
  end
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/aerosol/auto_scaling.rb', line 37

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

#launch_detailsObject



99
100
101
# File 'lib/aerosol/auto_scaling.rb', line 99

def launch_details
  launch_configuration || launch_template
end

#tag(val) ⇒ Object



103
104
105
# File 'lib/aerosol/auto_scaling.rb', line 103

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

#tags(ary = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aerosol/auto_scaling.rb', line 107

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/aerosol/auto_scaling.rb', line 146

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