Class: ConfigBuilder::Filter::Roles

Inherits:
Object
  • Object
show all
Defined in:
lib/config_builder/filter/roles.rb

Overview

The 'roles' filter adds a mechanism for defining generic VM roles and applying them to VMs.

Defining roles

This filter adds support for a top level roles key. It contains a hash of role names that define a hash containing the role behavior.

@note: The 'vms' field is of type Array, while the 'roles' field is of type Hash. This is because order of declaration matters for the actual VMs, while the order of declaration of roles does not matter.

Examples:

>> run()
=>  {
      'roles' => {
        'webserver' => {
          'synced_folders' => [
            {'host_path' => './www',                'guest_path' => '/var/www'},
            {'host_path' => './webserver-binaries', 'guest_path' => '/opt/webserver-binaries'},
          ]
        },
        'database' => {
          'provisioners' => [
            {'type' => 'puppet', 'manifest' => 'dbserver.pp'},
            {'type' => 'shell',  'path'     => 'scripts/initialize-db.sh'},
          ],
        }
      },
      'vms' => [
        {'name' => 'web',        'roles' => 'webserver'},
        {'name' => 'db',         'roles' => 'database'},
        {'name' => 'standalone', 'roles' => ['webserver', 'database']},
      ],
    }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rolesObject (readonly)

Returns the value of attribute roles.



42
43
44
# File 'lib/config_builder/filter/roles.rb', line 42

def roles
  @roles
end

Instance Method Details

#filter_vm(vm) ⇒ Hash

Returns The filtered VM.

Parameters:

  • old_vm (Hash)

Returns:

  • (Hash)

    The filtered VM



60
61
62
63
64
65
66
# File 'lib/config_builder/filter/roles.rb', line 60

def filter_vm(vm)
  node_stack = roles_by_name(vm.delete('roles'))

  node_stack.inject(vm) do |accumulator, role|
    merge_nodes!(accumulator, role)
  end
end

#role(name) ⇒ Hash<String, Object>

Fetch the role associated with the given name

Parameters:

  • name (String)

Returns:

  • (Hash<String, Object>)


73
74
75
76
77
78
79
# File 'lib/config_builder/filter/roles.rb', line 73

def role(name)
  if (retval = @roles[name])
    retval
  else
    raise ArgumentError, "Requested role #{name.inspect} is not defined, available roles: #{@roles.keys}."
  end
end

#roles_by_name(name) ⇒ Array<Hash> #roles_by_name(names) ⇒ Array<Hash> #roles_by_name(nothing) ⇒ Array<>

Overloads:

  • #roles_by_name(name) ⇒ Array<Hash>

    Returns An array containing the requested role.

    Parameters:

    • name (String)

      A single role name

    Returns:

    • (Array<Hash>)

      An array containing the requested role

  • #roles_by_name(names) ⇒ Array<Hash>

    Returns An array containing all of the requested roles in the order requested.

    Parameters:

    • names (Array<String>)

      A list of role names

    Returns:

    • (Array<Hash>)

      An array containing all of the requested roles in the order requested.

  • #roles_by_name(nothing) ⇒ Array<>

    Returns An empty array.

    Parameters:

    • nothing (NilClass)

      nil

    Returns:

    • (Array<>)

      An empty array

Returns:

  • (Array)


95
96
97
98
99
100
101
102
103
104
# File 'lib/config_builder/filter/roles.rb', line 95

def roles_by_name(field)

  case field
  when Array    then names = field
  when String   then names = [field]
  when NilClass then names = []
  end

  names.map { |name| role(name) }
end

#runObject



50
51
52
53
54
55
# File 'lib/config_builder/filter/roles.rb', line 50

def run
  return @root_config if @vms.nil?

  @root_config['vms'] = @vms.map { |vm_hash| filter_vm(vm_hash) }
  @root_config
end

#set_config(root_config) ⇒ Object



44
45
46
47
48
# File 'lib/config_builder/filter/roles.rb', line 44

def set_config(root_config)
  @root_config = root_config
  @roles       = @root_config.delete('roles')
  @vms         = @root_config.delete('vms')
end