class CfnTools

Public Class Methods

new( stack) click to toggle source
# File lib/CfnTools.rb, line 7
def initialize( stack)

  @stack_name = stack
  @cfn = AWS::CloudFormation.new()
  unless @cfn.stacks[@stack_name].exists?
    puts "Stack does not exist: #{@stack_name}"
    exit 1
  end

  @substacks = nil
  @stack_as = nil
  @stack_events = Set.new()
end

Public Instance Methods

display_stack_events() click to toggle source
# File lib/CfnTools.rb, line 118
def display_stack_events() 
  max_size = { 'time' => 0, 'type' => 0, 'resource_id' => 0, 'resource_status' => 0, 'status' => 0 }
  while true
    events = self.get_stack_events
    events.each do |event|
      hash = { 'time' => event.timestamp, 'type' => event.resource_type, 'resource_id' => event.logical_resource_id, 'resource_status' => event.resource_status, 'status' => event.resource_status_reason }
      hash.each do |tuple|
        if tuple[1].to_s.length > max_size[tuple[0]] then
          max_size[tuple[0]] = tuple[1].to_s.length
        end
      end
    end
    events.each do |event|
      if event.resource_status_reason.to_s.length > 0 then
        printf("%-#{max_size['time']}s %-#{max_size['type']}s %-#{max_size['resource_id']}s %-#{max_size['resource_status']}s %-#{max_size['status']}s\n", event.timestamp, event.resource_type, event.logical_resource_id, event.resource_status, event.resource_status_reason)
      else
        printf("%-#{max_size['time']}s %-#{max_size['type']}s %-#{max_size['resource_id']}s %-#{max_size['resource_status']}s\n", event.timestamp, event.resource_type, event.logical_resource_id, event.resource_status)
      end
    end
  end
end
get_as_instances( as_name) click to toggle source
# File lib/CfnTools.rb, line 84
def get_as_instances( as_name)
  instances = Array.new()
  as = AWS::AutoScaling.new()
  myAS = as.groups[ as_name ]
  myAS.auto_scaling_instances.each do |instance|
    old_lc = 0
    if instance.launch_configuration_name != myAS.launch_configuration.name
      #old_lc = "** has old LaunchConfiguration ** "
      old_lc = 1
    end
    myInstance = instance.ec2_instance
    instances.push( { 'ip_address' => myInstance.private_ip_address, 'instance_id' => instance.ec2_instance.instance_id, 'old_lc' => old_lc } )
  end 
  return instances
end
get_stack_as() click to toggle source
# File lib/CfnTools.rb, line 61
def get_stack_as()
  AWS.memoize do
    if @stack_as.nil?
      @stack_as = []
      stack = @cfn.stacks[@stack_name]
      stack.resource_summaries.each do |resource|
        if resource[:resource_type] == 'AWS::AutoScaling::AutoScalingGroup'
          @stack_as.push( resource[:physical_resource_id])
        end
      end
      get_sub_stacks().each do |substack|
        stack = @cfn.stacks[substack]
        stack.resource_summaries.each do |resource|
          if resource[:resource_type] == 'AWS::AutoScaling::AutoScalingGroup'
            @stack_as.push( resource[:physical_resource_id])
          end
        end
      end
    end
    return @stack_as
  end
end
get_stack_events() click to toggle source
# File lib/CfnTools.rb, line 100
def get_stack_events()
  events = Array.new()
  stacks = get_sub_stacks()
  stacks.push( @stack_name)
  AWS.memoize do
    stacks.each do |stack|
      stack = @cfn.stacks[stack]
      stack.events.each do |event|
        if @stack_events.add?(event.event_id)
          events.push(event)
        end
      end
    end
  end
  events.sort! { |a,b| a.timestamp <=> b.timestamp }
  return events
end
get_sub_stacks() click to toggle source
# File lib/CfnTools.rb, line 44
def get_sub_stacks()
  AWS.memoize do
    if @substacks.nil?
      @substacks = []
      stack = @cfn.stacks[@stack_name]
      stack.resource_summaries.each do |resource|
        if resource[:resource_type] == 'AWS::CloudFormation::Stack'
          if resource[:physical_resource_id].is_a? String
            @substacks.push( resource[:physical_resource_id] )
          end
        end
      end
    end
    return @substacks
  end
end
get_template( stack) click to toggle source
# File lib/CfnTools.rb, line 37
def get_template( stack)
  cfn_template = @cfn.stacks[stack].template
  parsed_cfn_template = JSON.load( cfn_template)
  template = YAML::dump( parsed_cfn_template)
  return template
end
ret_stack_as() click to toggle source
# File lib/CfnTools.rb, line 29
def ret_stack_as()
  return get_stack_as()
end
ret_stack_events() click to toggle source
# File lib/CfnTools.rb, line 33
def ret_stack_events()
  return @stack_events
end
ret_stack_name() click to toggle source
# File lib/CfnTools.rb, line 21
def ret_stack_name()
  return @stack_name
end
ret_substacks() click to toggle source
# File lib/CfnTools.rb, line 25
def ret_substacks()
  return get_sub_stacks()
end