Module: Beaker::Shared::HostHandler

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

Instance Method Summary collapse

Instance Method Details

#get_domain_name(host) ⇒ Object

NOTE: this code is shamelessly stolen from facter’s ‘domain’ fact, but we don’t have access to facter at this point in the run. Also, this utility method should perhaps be moved to a more central location in the framework.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/beaker/shared/host_handler.rb', line 9

def get_domain_name(host)
  domain = nil
  search = nil
  resolv_conf = host.exec(Command.new("cat /etc/resolv.conf")).stdout
  resolv_conf.each_line { |line|
    if line =~ /^\s*domain\s+(\S+)/
      domain = $1
    elsif line =~ /^\s*search\s+(\S+)/
      search = $1
    end
  }
  return domain if domain
  return search if search
end

#get_ip(host) ⇒ Object



24
25
26
# File 'lib/beaker/shared/host_handler.rb', line 24

def get_ip(host)
  host.exec(Command.new("ip a|awk '/g/{print$2}' | cut -d/ -f1 | head -1")).stdout.chomp
end

#hosts_with_role(hosts, desired_role = nil) ⇒ Object



32
33
34
35
36
# File 'lib/beaker/shared/host_handler.rb', line 32

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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beaker/shared/host_handler.rb', line 38

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

#set_etc_hosts(host, etc_hosts) ⇒ Object



28
29
30
# File 'lib/beaker/shared/host_handler.rb', line 28

def set_etc_hosts(host, etc_hosts)
  host.exec(Command.new("echo '#{etc_hosts}' > /etc/hosts"))
end