Module: Beaker::Shared::HostManager
- Included in:
- Beaker::Shared
- Defined in:
- lib/beaker/shared/host_manager.rb
Overview
Methods for managing Hosts.
-
selecting hosts by role (Symbol or String)
-
selecting hosts by name (String)
-
adding additional method definitions for selecting by role
-
executing blocks of code against selected sets of hosts
Instance Method Summary collapse
-
#hosts_with_name(hosts, name = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired name.
-
#hosts_with_role(hosts, desired_role = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired role.
-
#only_host_with_role(hosts, role) ⇒ Host
Find a single host with the role provided.
-
#run_block_on(hosts = [], filter = nil, &block) ⇒ Object
Execute a block selecting the hosts that match with the provided criteria.
Instance Method Details
#hosts_with_name(hosts, name = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired name.
24 25 26 27 28 |
# File 'lib/beaker/shared/host_manager.rb', line 24 def hosts_with_name(hosts, name = nil) hosts.select do |host| name.nil? or host.name =~ /\A#{name}/ end end |
#hosts_with_role(hosts, desired_role = nil) ⇒ Array<Host>
Find hosts from a given array of hosts that all have the desired role.
14 15 16 17 18 |
# File 'lib/beaker/shared/host_manager.rb', line 14 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.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/beaker/shared/host_manager.rb', line 37 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 |
#run_block_on(hosts = [], filter = nil, &block) ⇒ Object
Execute a block selecting the hosts that match with the provided criteria
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/beaker/shared/host_manager.rb', line 53 def run_block_on hosts = [], filter = nil, &block result = nil block_hosts = hosts #the hosts to apply the block to after any filtering if filter if not hosts.empty? block_hosts = hosts_with_role(hosts, filter) #check by role if block_hosts.empty? block_hosts = hosts_with_name(hosts, filter) #check by name end if block_hosts.length == 1 #we only found one matching host, don't need it wrapped in an array block_hosts = block_hosts.pop end else raise ArgumentError, "Unable to sort for #{filter} type hosts when provided with [] as Hosts" end end if block_hosts.is_a? Array result = block_hosts.map do |h| run_block_on h, nil, &block end else result = yield block_hosts end result end |