Sha256: b08f8d6fdfcb42534e6a7048edc09861326c497f67a767d645efd82ab913d47d

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

require 'yaml'
require 'aws-sdk'

module Opsicle
  class Config
    FOG_CONFIG_PATH = '~/.fog'
    OPSICLE_CONFIG_PATH = './.opsicle'


    attr_reader :environment

    def initialize(environment)
      @environment = environment.to_sym
    end

    def aws_config
      return @aws_config if @aws_config
      fog_config = load_config(File.expand_path(FOG_CONFIG_PATH))
      @aws_config = { access_key_id: fog_config[:aws_access_key_id], secret_access_key: fog_config[:aws_secret_access_key] }
    end

    def opsworks_config
      @opsworks_config ||= load_config(OPSICLE_CONFIG_PATH)
    end

    def configure_aws!
      AWS.config(aws_config)
    end

    def load_config(file)
      raise MissingConfig, "Missing configuration file: #{file}  Run 'opsicle help'" unless File.exist?(file)
      env_config = symbolize_keys(YAML.load_file(file))[environment] rescue {}
      raise MissingEnvironment, "Configuration for the \'#{environment}\' environment could not be found in #{file}" unless env_config != nil

      env_config
    end

    # We want all ouf our YAML loaded keys to be symbols
    # taken from http://devblog.avdi.org/2009/07/14/recursively-symbolize-keys/
    def symbolize_keys(hash)
      hash.inject({}){|result, (key, value)|
        new_key = case key
                  when String then key.to_sym
                  else key
                  end
        new_value = case value
                    when Hash then symbolize_keys(value)
                    else value
                    end
        result[new_key] = new_value
        result
      }
    end

    MissingConfig = Class.new(StandardError)
    MissingEnvironment = Class.new(StandardError)

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opsicle-0.10.0 lib/opsicle/config.rb
opsicle-0.9.0 lib/opsicle/config.rb
opsicle-0.8.2 lib/opsicle/config.rb
opsicle-0.8.1 lib/opsicle/config.rb
opsicle-0.8.0 lib/opsicle/config.rb