class Rivet::Autoscale

Constants

OPTIONS
REQUIRED_OPTIONS

Attributes

name[R]
post[R]

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

differences() click to toggle source
# File lib/rivet/as/autoscale.rb, line 54
def differences
  @differences ||= get_differences
end
differences?() click to toggle source
# File lib/rivet/as/autoscale.rb, line 58
def differences?
  !differences.empty?
end
display(level = 'info') click to toggle source
# 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_diff() click to toggle source
# 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
options() click to toggle source
# File lib/rivet/as/autoscale.rb, line 50
def options
  @options ||= get_update_options
end
sync() click to toggle source
# 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

Protected Instance Methods

create(options) click to toggle source
# File lib/rivet/as/autoscale.rb, line 167
def create(options)
  # When creating an autoscaling group passing empty arrays for subnets
  # or some other fields can cause it to barf.  Remove them first.
  options.delete_if { |k, v| v.respond_to?(:'empty?') && v.empty? }

  Rivet::Log.debug "Creating Autoscaling group #{@name} with the following options"
  Rivet::Log.debug options

  autoscale = AWS::AutoScaling.new
  if autoscale.groups[@name].exists?
    fail "Cannot create AutoScaling #{@name} group it already exists!"
  else
    autoscale.groups.create(@name, options)
  end
end
get_differences() click to toggle source
# File lib/rivet/as/autoscale.rb, line 120
def get_differences
  differences = {}

  OPTIONS.each do |o|
    remote_value = @remote_group.send(o)
    local_value  = send(o)

    if (remote_value != local_value)
      differences[o] = { :local => send(o), :remote => @remote_group.send(o) }
    end
  end
  differences
end
get_lc_diff() click to toggle source
# File lib/rivet/as/autoscale.rb, line 134
def get_lc_diff
  autoscale = AWS::AutoScaling.new
  remote_lc_identity = @remote_group.launch_configuration

  if remote_lc_identity.nil?
    remote_user_data = ''
  else
    remote_lc = autoscale.launch_configurations[remote_lc_identity]
    remote_user_data = remote_lc.user_data.split("\n")
  end

  # We split on new lines so the diff doesn't compare by character
  local_user_data  = @launch_config.user_data.split("\n")

  Diff::LCS.diff(remote_user_data,local_user_data)
end
get_update_options() click to toggle source
# File lib/rivet/as/autoscale.rb, line 151
def get_update_options
  options = {}

  OPTIONS.each do |field|
    local_value = self.send(field)
    options[field] = local_value unless local_value.nil?
  end

  REQUIRED_OPTIONS.each do |field|
    unless options.has_key? field
      options[field] = self.send(field)
    end
  end
  options
end