Class: OpenPortChecker::SecurityGroupsRetrievedState

Inherits:
OpenPortCheckerState show all
Defined in:
lib/scripts/ec2/open_port_checker.rb

Overview

Got all instances. If there are some, check security groups

Instance Attribute Summary

Attributes inherited from ScriptExecutionState

#context, #logger

Instance Method Summary collapse

Methods inherited from OpenPortCheckerState

load_state

Methods inherited from ScriptExecutionState

#done?, #end_state, #failed?, #initialize, #register_state_change_listener, #start_state_machine, #to_s

Methods included from StateTransitionHelper

#attach_volume, #connect, #copy_distribution, #create_fs, #create_snapshot, #create_volume, #create_volume_from_snapshot, #delete_snapshot, #delete_volume, #detach_volume, #determine_file, #disconnect, #ec2_handler, #ec2_handler=, #launch_instance, #mount_fs, #register_snapshot, #remote_copy, #remote_handler, #remote_handler=, #retrieve_instances, #retrieve_security_groups, #shut_down_instance, #start_instance, #unmount_fs, #upload_file, #zip_volume

Constructor Details

This class inherits a constructor from ScriptExecutionState

Instance Method Details

#enterObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/scripts/ec2/open_port_checker.rb', line 63

def enter
  @context[:result][:port_checks] = []
  ec2_helper = Ec2Helper.new(@context[:ec2_api_handler])
  @context[:ec2_instances]['reservationSet']['item'].each() do |instance_info|
    instance_id = ec2_helper.get_instance_id(instance_info)
    @logger.debug("instance_info = #{instance_info.inspect}")
    instance_ip = ec2_helper.get_instance_prop(instance_info, 'dnsName')
    instance_state = ec2_helper.get_instance_prop(instance_info, 'instanceState')['name']
    if instance_state != "running"
      post_message("ignore instance #{instance_id} since not running")
      next
    end
    sec_groups = ec2_helper.lookup_security_group_names(instance_info)
    @logger.debug("group lookup for #{instance_id} => #{sec_groups.inspect}")
    sec_groups.each() do |group_name|
      port_infos = ec2_helper.lookup_open_ports(group_name, @context[:security_groups])
      @logger.debug("port_infos for group #{group_name} #{port_infos.inspect}")
      port_infos.each() do |port_info|
        result = false
        begin
          result = @context[:remote_command_handler].is_port_open?(instance_ip, port_info[:port])
          post_message("check port #{port_info[:port]} for instance #{instance_id} (on #{instance_ip}) #{result ? "successful" : "failed"}")
        rescue Exception => e
          @logger.warn("exception during executing port check: #{e}")
        end
        @context[:result][:port_checks] << {:instance => instance_id, :protocol => port_info[:protocol],
          :port => port_info[:port], :success => result, :group_name => group_name
        }
      end
    end
  end
  AnalysisDone.new(@context)
end