Sha256: 2e2331eb977c0f15a5819cdf4cbcaa2c78e43bbea0639f39c6c338ce9e49485c

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'securerandom'
require 'digest'

module EbDeployer
  class ConfigLoader

    class EvalBinding
      def initialize(package_digest)
        @package_digest = package_digest
      end

      def random_hash
        SecureRandom.hex[0..9]
      end

      def package_digest
        @package_digest
      end
    end

    def load(options)
      options = options.dup
      package_digest = package_digest(options[:package])
      config_file = options.delete(:config_file)
      config_settings = load_config_settings(config_file, package_digest)

      common_settings = symbolize_keys(config_settings.delete(:common))
      common_settings[:version_label] ||= package_digest

      envs = config_settings.delete(:environments)
      ret = options.merge(config_settings).merge(common_settings)
      unless ret.delete(:all_envs)
        env = ret[:environment]
        raise "Environment #{env} is not defined in #{config_file}" unless envs.has_key?(env)
        env_settings = symbolize_keys(envs[env] || {})
        env_option_settings = env_settings.delete(:option_settings) || []

        ret.merge!(env_settings)

        ret[:option_settings] ||= []
        ret[:option_settings] += env_option_settings
      else
        ret.delete(:environment)
      end
      ret
    end

    private

    def symbolize_keys(hash)
      hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
    end

    def load_config_settings(config_file, package_digest)
      yaml = ERB.new(File.read(config_file)).result(eval_binding(package_digest))
      symbolize_keys(YAML.load(yaml))
    end

    def eval_binding(package_digest)
      EvalBinding.new(package_digest).instance_eval { binding }
    end

    def package_digest(package)
      return nil unless package
      return package unless File.exists?(package)
      Digest::MD5.file(package).hexdigest
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eb_deployer-0.4.0 lib/eb_deployer/config_loader.rb
eb_deployer-0.3.9 lib/eb_deployer/config_loader.rb
eb_deployer-0.3.6 lib/eb_deployer/config_loader.rb