Sha256: 80afcd480a9e3414581f271605c338d5184504369666bd69f9d79ca57e3a7715

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

require 'logger'
require 'rubber/environment'
require 'rubber/instance'
require 'rubber/generator'

module Rubber
  module Configuration
    extend MonitorMixin

    @@configurations = {}

    def self.get_configuration(env=nil, root=nil)
      key = "#{env}-#{root}"

      synchronize do
        unless @@configurations[key]
          @@configurations[key] = ConfigHolder.new(env, root)
          @@configurations[key].load()
        end
      end

      return @@configurations[key]
    end

    def self.rubber_env
      raise "This convenience method needs Rubber.env to be set" unless Rubber.env
      cfg = Rubber::Configuration.get_configuration(Rubber.env)
      host = cfg.environment.current_host
      roles = cfg.instance[host] ? cfg.instance[host].role_names : nil
      cfg.environment.bind(roles, host)
    end

    def self.rubber_instances
      raise "This convenience method needs Rubber.env to be set" unless Rubber.env
      Rubber::Configuration.get_configuration(Rubber.env).instance
    end

    def self.reset
      synchronize do
        @@configurations.clear
      end
    end

    class ConfigHolder
      def initialize(env=nil, root=nil)
        @env = env
        @root = root || "#{Rubber.root}/config/rubber"
        @environment = Environment.new("#{@root}", @env)
      end

      def load
        config = @environment.bind()
        instance_storage = config['instance_storage']
        instance_storage_backup = config['instance_storage_backup']
        instance_storage ||= "file:#{@root}/instance-#{@env}.yml"
        @instance = Instance.new(instance_storage, :backup => instance_storage_backup)
      end
      
      def environment
        @environment
      end

      def instance
        @instance
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubber-3.2.2 lib/rubber/configuration.rb
rubber-3.2.1 lib/rubber/configuration.rb
rubber-3.2.0 lib/rubber/configuration.rb
rubber-3.1.0 lib/rubber/configuration.rb
rubber-3.0.1 lib/rubber/configuration.rb