Class: Rivet::Autoscale

Inherits:
Object
  • Object
show all
Defined in:
lib/rivet/as/autoscale.rb

Constant Summary collapse

OPTIONS =
[
  :availability_zones,
  :default_cooldown,
  :desired_capacity,
  :health_check_grace_period,
  :health_check_type,
  :launch_configuration,
  :load_balancers,
  :max_size,
  :min_size,
  :placement_group,
  :subnets,
  :tags,
  :termination_policies
].each { |a| attr_reader a }
REQUIRED_OPTIONS =
[
  :availability_zones,
  :launch_configuration,
  :max_size,
  :min_size
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Autoscale

Returns a new instance of Autoscale.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rivet/as/autoscale.rb', line 33

def initialize(config)
  @name          = config.name
  @remote_group  = AwsAutoscaleWrapper.new(@name)
  @launch_config = LaunchConfig.new(config)
  @post = config.post

  OPTIONS.each do |o|
    if config.respond_to?(o)
      instance_variable_set("@#{o}", config.send(o))
    end
  end

  # The launch_configuration attr exists because that is what
  # the aws SDK refers to the launch configuration name as.
  @launch_configuration = @launch_config.identity
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/rivet/as/autoscale.rb', line 30

def name
  @name
end

#postObject (readonly)

Returns the value of attribute post.



31
32
33
# File 'lib/rivet/as/autoscale.rb', line 31

def post
  @post
end

Instance Method Details

#differencesObject



54
55
56
# File 'lib/rivet/as/autoscale.rb', line 54

def differences
  @differences ||= get_differences
end

#differences?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rivet/as/autoscale.rb', line 58

def differences?
  !differences.empty?
end

#display(level = 'info') ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rivet/as/autoscale.rb', line 62

def display(level = 'info')
  Rivet::Log.write(level, 'Remote and local match') unless differences?
  differences.each_pair do |attr, values|
    Rivet::Log.write(level, "#{attr}:")
    Rivet::Log.write(level, "  remote: #{values[:remote]}")
    Rivet::Log.write(level, "  local:  #{values[:local]}")
  end
 Rivet::Log.write('debug', @launch_config.user_data)
 display_lc_diff
end

#display_lc_diffObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rivet/as/autoscale.rb', line 73

def display_lc_diff
  @lc_diff ||= get_lc_diff

  Rivet::Log.info"Displaying diff for launch configuration:"
  @lc_diff.each do |d|
    d.each do |current|
      diff_text = if current.element.respond_to? :join
        current.element.join
      else
        current.element
      end
      Rivet::Log.info "   #{current.action} #{diff_text}"
    end
  end
end

#optionsObject



50
51
52
# File 'lib/rivet/as/autoscale.rb', line 50

def options
  @options ||= get_update_options
end

#syncObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rivet/as/autoscale.rb', line 89

def sync
  if differences?
    Rivet::Log.info "Syncing autoscale group changes to AWS for #{@name}"
    autoscale = AWS::AutoScaling.new
    group     = autoscale.groups[@name]

    @launch_config.save
    create(options) unless group.exists?

    Rivet::Log.debug 'Updating autoscaling group with the follow options'
    Rivet::Log.debug options.inspect

    # It's easier to just delete all the tags if there are changes and apply
    # new ones, than ferret out exactly which ones should be removed.
    if differences.has_key? :tags
      group.delete_all_tags
    end
    group.update(options)

  else
    Rivet::Log.info "No autoscale differences to sync to AWS for #{@name}."
  end
# Post processing hooks
  unless post.nil?
    post_processing = OpenStruct.new
    post_processing.instance_eval(&post)
  end
end