Sha256: b0a5e2cf9ca586407cf66ff718e67d14aa56455aaaca05dfa8a45a2ef98f42c7

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

require 'securerandom'
require 'digest'
require 'yaml'

module EbDeployer
  class ConfigLoader
    include Utils

    class EvalBinding
      attr_reader :environment, :package_digest
      def initialize(package_digest, env)
        @package_digest = package_digest
        @environment = env
      end

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

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

      env = options[:environment]
      config_settings = load_config_settings(config_file, package_digest, env)

      app_name = config_settings[:application]

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

      envs = config_settings[:environments]
      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 = options.merge(common_settings).merge(env_settings)
      ret[:application] = app_name
      ret[:option_settings] ||= []
      ret[:option_settings] += env_option_settings
      ret
    end

    private

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

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

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

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
man_eb_deployer-0.8.0 lib/eb_deployer/config_loader.rb
eb_deployer_updated-0.8.1 lib/eb_deployer/config_loader.rb
eb_deployer_updated-0.8.0 lib/eb_deployer/config_loader.rb
eb_deployer-0.7.0 lib/eb_deployer/config_loader.rb