Sha256: 695c640dfa389b11b8929e57cfe6d463b35ffbd9c6b725237d305febbd9f0a19

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

module SimpleDeploy
  class AWS
    class InstanceReader
      def initialize
        @config  = SimpleDeploy.config
      end

      def list_stack_instances(stack_name)
        asg_id = auto_scaling_group_id(stack_name)
        return [] unless asg_id

        asg_instances = list_instances asg_id
        return [] unless asg_instances.any?

        describe_instances asg_instances
      end

      private

      def list_instances(asg_id)
        @asg ||= Fog::AWS::AutoScaling.new :aws_access_key_id => @config.access_key,
                                           :aws_secret_access_key => @config.secret_key,
                                           :region => @config.region

        body = @asg.describe_auto_scaling_groups('AutoScalingGroupNames' => [asg_id]).body
        result = body['DescribeAutoScalingGroupsResult']['AutoScalingGroups'].last
        return [] unless result

        result['Instances'].map { |info| info['InstanceId'] }
      end

      def describe_instances(instances)
        @ec2 ||= Fog::Compute::AWS.new :aws_access_key_id => @config.access_key,
                                       :aws_secret_access_key => @config.secret_key,
                                       :region => @config.region

        @ec2.describe_instances('instance-state-name' => 'running',
                                'instance-id' => instances).body['reservationSet']
      end

      def cloud_formation
        @cloud_formation ||= AWS::CloudFormation.new
      end

      def auto_scaling_group_id(stack_name)
        cf_stack_resources = cloud_formation.stack_resources stack_name
        parse_cf_stack_resources cf_stack_resources
      end

      def parse_cf_stack_resources(cf_stack_resources)
        asgs = cf_stack_resources.select do |r|
          r['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup'
        end
        asgs.any? ? asgs.first['PhysicalResourceId'] : false
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
simple_deploy-0.8.0 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.8 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.7 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.7.beta.1 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.6 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.6.beta.6 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.6.beta.5 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.6.beta.3 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.6.beta.1 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.5 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.4 lib/simple_deploy/aws/instance_reader.rb
simple_deploy-0.7.3 lib/simple_deploy/aws/instance_reader.rb