Sha256: 2e533a97c1999d92afc91a119b131d07c292ccd8b698dd214745453f62b7334b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module BBC
  module Cosmos
    module Config
      class AWS
        def initialize(ec2, s3)
          @ec2 = ec2
          @s3  = s3
        end

        def tags
          instance.tags
        end

        def region
          ::AWS.config.region
        end

        def instance_id
          BBC::Cosmos.production? ? aws_instance_id : "instance_id"
        end

        def config(config, path)
          BBC::Cosmos.production? ? s3_config(config[:component_resources_bucket], path, config[:configuration][:commit]) : config[:configuration]
        end

        private

        def s3_config_location
          tags.to_h['Renderer']
        end

        def s3_config(bucket, path, version)
         JSON.parse(@s3.buckets[bucket].objects["#{path}/config/#{version}/#{s3_config_location}.json"].read, :symbolize_names => true)
        end

        def instance
          BBC::Cosmos.production? ? aws_instance : dummy_instance
        end

        def dummy_instance
          Struct.new(:tags).new({
            "aws:cloudformation:stack-name" => "stack_name",
            "Component" => "component_name"
          })
        end

        def aws_instance
          @instance ||= @ec2.regions[region].instances[instance_id]
        end

        def aws_instance_id
          `curl http://169.254.169.254/latest/meta-data/instance-id`
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bbc-cosmos-config-0.0.6 lib/bbc/cosmos/config/aws.rb