Module: Beaker::Shared::HostRoleParser

Included in:
Beaker::Shared
Defined in:
lib/beaker/shared/host_role_parser.rb

Overview

Methods for selecting host or hosts that match roles.

Instance Method Summary collapse

Instance Method Details

#hosts_with_role(hosts, desired_role = nil) ⇒ Array<Host>

Find hosts from a given array of hosts that all have the desired role.

Parameters:

  • hosts (Array<Host>)

    The hosts to examine

  • desired_role (String) (defaults to: nil)

    The hosts returned will have this role in their roles list

Returns:

  • (Array<Host>)

    The hosts that have the desired role in their roles list



10
11
12
13
14
# File 'lib/beaker/shared/host_role_parser.rb', line 10

def hosts_with_role(hosts, desired_role = nil)
  hosts.select do |host|
    desired_role.nil? or host['roles'].include?(desired_role.to_s)
  end
end

#only_host_with_role(hosts, role) ⇒ Host

Find a single host with the role provided. Raise an error if more than one host is found to have the provided role.

Parameters:

  • hosts (Array<Host>)

    The hosts to examine

  • role (String)

    The host returned will have this role in its role list

Returns:

  • (Host)

    The single host with the desired role in its roles list

Raises:

  • (ArgumentError)

    Raised if more than one host has the given role defined, or if no host has the role defined.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/beaker/shared/host_role_parser.rb', line 23

def only_host_with_role(hosts, role)
  a_host = hosts_with_role(hosts, role)
  case
    when a_host.length == 0
      raise ArgumentError, "There should be one host with #{role} defined!"
    when a_host.length > 1
      host_string = ( a_host.map { |host| host.name } ).join( ', ')
      raise ArgumentError, "There should be only one host with #{role} defined, but I found #{a_host.length} (#{host_string})"
  end
  a_host.first
end