Sha256: a256ea18d96729b0ed69a07030fcc5360e10ee009fdfdee0f1410ab1e87be096

Contents?: true

Size: 1.53 KB

Versions: 72

Compression:

Stored size: 1.53 KB

Contents

require "ostruct"
class Subspace::Configuration
  attr_accessor :project_name
  attr_reader :vars, :host_config, :group_config

  def initialize
    @host_config = {}
    @group_config = {}
    @vars = OpenStruct.new
  end

  def host(name, options)
    @host_config[name] = options
  end

  def hosts
    @host_config.keys
  end

  def groups
    @group_config.keys
  end

  def group(name, hosts: [], vars: {})
    group_config(name).hosts += hosts
    group_config(name).vars.merge!(vars)
  end

  def role(name, groups: [], vars: {})
    groups.each do |group|
      group_config(group).roles.push(name.to_sym)
      vars.each do |k,v|
        if group_config(group).vars[k]
          put "Warning, variable '#{k}' already set for group '#{group}'"
        end
        group_config(group).vars[k] = v
      end
    end
  end

  # I think a better way to do this is possibly to just write out all the host vars directly to a yaml file
  # That provides more flexibility, although this method provides a whitelist.
  def binding_for(host: nil, group: nil)
    config = @vars.dup
    if host
      @host_config[host].each do |k,v|
        config[k] = v
      end
    end
    b = binding
    b.local_variable_set(:config, config)
    b
  end

  private

  def group_config(group)
    @group_config[group.to_sym] ||= GroupConfig.new(group)
  end

  class GroupConfig
    attr_reader :name, :vars
    attr_accessor :hosts, :roles
    def initialize(name)
      @name = name.to_sym
      @hosts = []
      @roles = []
      @vars = OpenStruct.new
    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
subspace-3.0.13 lib/subspace/configuration.rb
subspace-3.0.12 lib/subspace/configuration.rb
subspace-3.0.11 lib/subspace/configuration.rb
subspace-3.0.10 lib/subspace/configuration.rb
subspace-3.0.9 lib/subspace/configuration.rb
subspace-3.0.6 lib/subspace/configuration.rb
subspace-3.0.5 lib/subspace/configuration.rb
subspace-3.0.4 lib/subspace/configuration.rb
subspace-3.0.3 lib/subspace/configuration.rb
subspace-3.0.2 lib/subspace/configuration.rb
subspace-3.0.0 lib/subspace/configuration.rb
subspace-2.5.10 lib/subspace/configuration.rb
subspace-3.0.0.rc1 lib/subspace/configuration.rb
subspace-2.5.9 lib/subspace/configuration.rb
subspace-2.5.8 lib/subspace/configuration.rb
subspace-2.5.7 lib/subspace/configuration.rb
subspace-2.5.6 lib/subspace/configuration.rb
subspace-2.5.5 lib/subspace/configuration.rb
subspace-2.5.4 lib/subspace/configuration.rb
subspace-2.5.3 lib/subspace/configuration.rb