Module: Beaker::HostPrebuiltSteps
- Included in:
- Hypervisor
- Defined in:
- lib/beaker/host_prebuilt_steps.rb
Overview
Provides convienience methods for commonly run actions on hosts
Constant Summary collapse
- NTPSERVER =
'pool.ntp.org'- SLEEPWAIT =
5- TRIES =
5- UNIX_PACKAGES =
['curl', 'ntpdate']
- WINDOWS_PACKAGES =
['curl']
- SLES_PACKAGES =
['curl', 'ntp']
- ETC_HOSTS_PATH =
"/etc/hosts"- ETC_HOSTS_PATH_SOLARIS =
"/etc/inet/hosts"- ROOT_KEYS_SCRIPT =
"https://raw.githubusercontent.com/puppetlabs/puppetlabs-sshkeys/master/templates/scripts/manage_root_authorized_keys"- ROOT_KEYS_SYNC_CMD =
"curl -k -o - -L #{ROOT_KEYS_SCRIPT} | %s"- APT_CFG =
%q{ Acquire::http::Proxy "http://proxy.puppetlabs.net:3128/"; }- IPS_PKG_REPO =
"http://solaris-11-internal-repo.delivery.puppetlabs.net"
Instance Method Summary collapse
-
#add_el_extras(host, opts) ⇒ Object
Install EPEL on host or hosts with platform = /el-(5|6)/.
-
#apt_get_update(host) ⇒ Object
Run ‘apt-get update’ on the provided host or hosts.
-
#copy_file_to_remote(host, file_path, file_content) ⇒ Object
Create a file on host or hosts at the provided file path with the provided file contents.
-
#copy_ssh_to_root(host, opts) ⇒ Object
Make it possible to log in as root by copying the current users ssh keys to the root account.
-
#disable_iptables(host, opts) ⇒ Object
Disable iptables on centos, does nothing on other platforms.
-
#disable_se_linux(host, opts) ⇒ Object
Disable SELinux on centos, does nothing on other platforms.
-
#enable_root_login(host, opts) ⇒ Object
Update sshd_config on debian, ubuntu, centos, el, redhat and fedora boxes to allow for root login, does nothing on other platfoms.
-
#epel_info_for!(host) ⇒ String
Determine the Extra Packages for Enterprise Linux URL for the provided Enterprise Linux host.
-
#get_domain_name(host) ⇒ Object
Determine the domain name of the provided host from its /etc/resolv.conf.
- #get_ip(host) ⇒ Object deprecated Deprecated.
-
#hack_etc_hosts(hosts, opts) ⇒ Object
Update /etc/hosts to make it possible for each provided host to reach each other host by name.
-
#package_proxy(host, opts) ⇒ Object
Setup files for enabling requests to pass to a proxy server This works for the APT package manager on debian and ubuntu and YUM package manager on el, centos, fedora and redhat.
-
#proxy_config(host, opts) ⇒ Object
Alter apt configuration on ubuntu and debian host or hosts to internal Puppet Labs proxy APT_CFG proxy, alter pkg on solaris-11 host or hosts to point to interal Puppetlabs proxy IPS_PKG_REPO.
-
#set_etc_hosts(host, etc_hosts) ⇒ Object
Append the provided string to the /etc/hosts file of the provided host.
-
#sync_root_keys(host, opts) ⇒ Object
Install a set of authorized keys using ROOT_KEYS_SCRIPT.
-
#timesync(host, opts) ⇒ Object
Run timesync on the provided hosts.
-
#validate_host(host, opts) ⇒ Object
Validate that hosts are prepared to be used as SUTs, if packages are missing attempt to install them.
Instance Method Details
#add_el_extras(host, opts) ⇒ Object
Install EPEL on host or hosts with platform = /el-(5|6)/. Do nothing on host or hosts of other platforms.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 223 def add_el_extras( host, opts ) #add_el_extras #only supports el-* platforms logger = opts[:logger] debug_opt = opts[:debug] ? 'vh' : '' if host.is_a? Array host.map { |h| add_el_extras(h, opts) } else case when host['platform'] =~ /el-(5|6)/ result = host.exec(Command.new('rpm -qa | grep epel-release'), :acceptable_exit_codes => [0,1]) if result.exit_code == 1 url = epel_info_for! host host.exec(Command.new("rpm -i#{debug_opt} #{url}")) host.exec(Command.new('yum clean all && yum makecache')) end else logger.debug "#{host}: package repo configuration not modified" end end rescue => e report_and_raise(logger, e, "add_repos") end |
#apt_get_update(host) ⇒ Object
Run ‘apt-get update’ on the provided host or hosts. If the platform of the provided host is not ubuntu or debian do nothing.
158 159 160 161 162 163 164 165 166 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 158 def apt_get_update host if host.is_a? Array host.map { |h| apt_get_update(h) } else if host[:platform] =~ /(ubuntu)|(debian)/ host.exec(Command.new("apt-get update")) end end end |
#copy_file_to_remote(host, file_path, file_content) ⇒ Object
Create a file on host or hosts at the provided file path with the provided file contents.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 172 def copy_file_to_remote(host, file_path, file_content) if host.is_a? Array host.map { |h| copy_file_to_remote(h, file_path, file_contents) } else Tempfile.open 'beaker' do |tempfile| File.open(tempfile.path, 'w') {|file| file.puts file_content } host.do_scp_to(tempfile.path, file_path, @options) end end end |
#copy_ssh_to_root(host, opts) ⇒ Object
Make it possible to log in as root by copying the current users ssh keys to the root account
282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 282 def copy_ssh_to_root host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| copy_ssh_to_root(h, opts) } else logger.debug "Give root a copy of current user's keys, on #{host.name}" if host['platform'] =~ /windows/ host.exec(Command.new('cp -r .ssh /cygdrive/c/Users/Administrator/.')) host.exec(Command.new('chown -R Administrator /cygdrive/c/Users/Administrator/.ssh')) else host.exec(Command.new('sudo su -c "cp -r .ssh /root/."'), {:pty => true}) end end end |
#disable_iptables(host, opts) ⇒ Object
Disable iptables on centos, does nothing on other platforms
357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 357 def disable_iptables host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| copy_ssh_to_root(h, opts) } else if host['platform'] =~ /centos|el-|redhat|fedora/ logger.debug("Disabling iptables on #{host.name}") host.exec(Command.new("sudo su -c \"/etc/init.d/iptables stop\""), {:pty => true}) else logger.warn("Attempting to disable iptables on non-supported platform: #{host.name}: #{host['platform']}") end end end |
#disable_se_linux(host, opts) ⇒ Object
Disable SELinux on centos, does nothing on other platforms
339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 339 def disable_se_linux host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| copy_ssh_to_root(h, opts) } else if host['platform'] =~ /centos|el-|redhat|fedora/ @logger.debug("Disabling se_linux on #{host.name}") host.exec(Command.new("sudo su -c \"setenforce 0\""), {:pty => true}) else @logger.warn("Attempting to disable SELinux on non-supported platform: #{host.name}: #{host['platform']}") end end end |
#enable_root_login(host, opts) ⇒ Object
Update sshd_config on debian, ubuntu, centos, el, redhat and fedora boxes to allow for root login, does nothing on other platfoms
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 316 def enable_root_login host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| copy_ssh_to_root(h, opts) } else logger.debug "Update /etc/ssh/sshd_config to allow root login" host.exec(Command.new("sudo su -c \"sed -i 's/PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config\""), {:pty => true} ) #restart sshd if host['platform'] =~ /debian|ubuntu/ host.exec(Command.new("sudo su -c \"service ssh restart\""), {:pty => true}) elsif host['platform'] =~ /centos|el-|redhat|fedora/ host.exec(Command.new("sudo su -c \"service sshd restart\""), {:pty => true}) else @logger.warn("Attempting to update ssh on non-supported platform: #{host.name}: #{host['platform']}") end end end |
#epel_info_for!(host) ⇒ String
Determine the Extra Packages for Enterprise Linux URL for the provided Enterprise Linux host.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 137 def epel_info_for! host version = host['platform'].match(/el-(\d+)/) if not version raise "epel_info_for! not available for #{host.name} on platform #{host['platform']}" end version = version[1] if version == '6' pkg = 'epel-release-6-8.noarch.rpm' url = "http://mirror.itc.virginia.edu/fedora-epel/6/i386/#{pkg}" elsif version == '5' pkg = 'epel-release-5-4.noarch.rpm' url = "http://archive.linux.duke.edu/pub/epel/5/i386/#{pkg}" else raise "epel_info_for! does not support el version #{version}, on #{host.name}" end return url end |
#get_domain_name(host) ⇒ Object
Determine the domain name of the provided host from its /etc/resolv.conf
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 249 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
Determine the ip address of the provided host
267 268 269 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 267 def get_ip(host) host.get_ip end |
#hack_etc_hosts(hosts, opts) ⇒ Object
Update /etc/hosts to make it possible for each provided host to reach each other host by name. Assumes that each provided host has host set.
302 303 304 305 306 307 308 309 310 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 302 def hack_etc_hosts hosts, opts etc_hosts = "127.0.0.1\tlocalhost localhost.localdomain\n" hosts.each do |host| etc_hosts += "#{host['ip'].to_s}\t#{host[:vmhostname] || host.name}\n" end hosts.each do |host| set_etc_hosts(host, etc_hosts) end end |
#package_proxy(host, opts) ⇒ Object
Setup files for enabling requests to pass to a proxy server This works for the APT package manager on debian and ubuntu and YUM package manager on el, centos, fedora and redhat.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 377 def package_proxy host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| package_proxy(h, opts) } else logger.debug("enabling proxy support on #{host.name}") case host['platform'] when /ubuntu/, /debian/ host.exec(Command.new("echo 'Acquire::http::Proxy \"#{opts[:package_proxy]}/\";' >> /etc/apt/apt.conf.d/10proxy")) when /^el-/, /centos/, /fedora/, /redhat/ host.exec(Command.new("echo 'proxy=#{opts[:package_proxy]}/' >> /etc/yum.conf")) else logger.debug("Attempting to enable package manager proxy support on non-supported platform: #{host.name}: #{host['platform']}") end end end |
#proxy_config(host, opts) ⇒ Object
Alter apt configuration on ubuntu and debian host or hosts to internal Puppet Labs proxy APT_CFG proxy, alter pkg on solaris-11 host or hosts to point to interal Puppetlabs proxy IPS_PKG_REPO. Do nothing on non-ubuntu, debian or solaris-11 platform host or hosts.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 191 def proxy_config( host, opts ) # repo_proxy # supports ubuntu, debian and solaris platforms logger = opts[:logger] if host.is_a? Array host.map { |h| proxy_config(h, opts) } else case when host['platform'] =~ /ubuntu/ host.exec(Command.new("if test -f /etc/apt/apt.conf; then mv /etc/apt/apt.conf /etc/apt/apt.conf.bk; fi")) copy_file_to_remote(host, '/etc/apt/apt.conf', APT_CFG) apt_get_update(host) when host['platform'] =~ /debian/ host.exec(Command.new("if test -f /etc/apt/apt.conf; then mv /etc/apt/apt.conf /etc/apt/apt.conf.bk; fi")) copy_file_to_remote(host, '/etc/apt/apt.conf', APT_CFG) apt_get_update(host) when host['platform'] =~ /solaris-11/ host.exec(Command.new("/usr/bin/pkg unset-publisher solaris || :")) host.exec(Command.new("/usr/bin/pkg set-publisher -g %s solaris" % IPS_PKG_REPO)) else logger.debug "#{host}: repo proxy configuration not modified" end end rescue => e report_and_raise(logger, e, "proxy_config") end |
#set_etc_hosts(host, etc_hosts) ⇒ Object
Append the provided string to the /etc/hosts file of the provided host
274 275 276 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 274 def set_etc_hosts(host, etc_hosts) host.exec(Command.new("echo '#{etc_hosts}' > /etc/hosts")) end |
#sync_root_keys(host, opts) ⇒ Object
Install a set of authorized keys using ROOT_KEYS_SCRIPT. This is a convenience method to allow for easy login to hosts after they have been provisioned with Beaker.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 112 def sync_root_keys host, opts # JJM This step runs on every system under test right now. We're anticipating # issues on Windows and maybe Solaris. We will likely need to filter this step # but we're deliberately taking the approach of "assume it will work, fix it # when reality dictates otherwise" logger = opts[:logger] if host.is_a? Array host.map { |h| sync_root_keys(h, opts) } else logger.notify "Sync root authorized_keys from github on #{host.name}" # Allow all exit code, as this operation is unlikely to cause problems if it fails. if host['platform'].include? 'solaris' host.exec(Command.new(ROOT_KEYS_SYNC_CMD % "bash"), :acceptable_exit_codes => (0..255)) else host.exec(Command.new(ROOT_KEYS_SYNC_CMD % "env PATH=/usr/gnu/bin:$PATH bash"), :acceptable_exit_codes => (0..255)) end end rescue => e report_and_raise(logger, e, "sync_root_keys") end |
#timesync(host, opts) ⇒ Object
Run timesync on the provided hosts
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 25 def timesync host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| timesync(h, opts) } else logger.notify "Update system time sync for '#{host.name}'" if host['platform'].include? 'windows' # The exit code of 5 is for Windows 2008 systems where the w32tm /register command # is not actually necessary. host.exec(Command.new("w32tm /register"), :acceptable_exit_codes => [0,5]) host.exec(Command.new("net start w32time"), :acceptable_exit_codes => [0,2]) host.exec(Command.new("w32tm /config /manualpeerlist:#{NTPSERVER} /syncfromflags:manual /update")) host.exec(Command.new("w32tm /resync")) logger.notify "NTP date succeeded on #{host}" else case when host['platform'] =~ /solaris-10/ ntp_command = "sleep 10 && ntpdate -w #{NTPSERVER}" when host['platform'] =~ /sles-/ ntp_command = "sntp #{NTPSERVER}" else ntp_command = "ntpdate -t 20 #{NTPSERVER}" end success=false try = 0 until try >= TRIES do try += 1 if host.exec(Command.new(ntp_command), :acceptable_exit_codes => (0..255)).exit_code == 0 success=true break end sleep SLEEPWAIT end if success logger.notify "NTP date succeeded on #{host} after #{try} tries" else raise "NTP date was not successful after #{try} tries" end end end rescue => e report_and_raise(logger, e, "timesync (--ntp)") end |
#validate_host(host, opts) ⇒ Object
Validate that hosts are prepared to be used as SUTs, if packages are missing attempt to install them. Verifies the presence of #UNIX_PACKAGES on unix platform hosts, SLES_PACKAGES on SUSE platform hosts and WINDOWS_PACKAGES on windows platforms.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/beaker/host_prebuilt_steps.rb', line 76 def validate_host host, opts logger = opts[:logger] if host.is_a? Array host.map { |h| validate_host(h, opts) } else case when host['platform'] =~ /sles-/ SLES_PACKAGES.each do |pkg| if not host.check_for_package pkg host.install_package pkg end end when host['platform'] =~ /windows/ WINDOWS_PACKAGES.each do |pkg| if not host.check_for_package pkg host.install_package pkg end end when host['platform'] !~ /aix|solaris|windows|sles-|osx-/ UNIX_PACKAGES.each do |pkg| if not host.check_for_package pkg host.install_package pkg end end end end rescue => e report_and_raise(logger, e, "validate") end |