Module: Beaker::DSL::Helpers::HostHelpers
- Included in:
- Beaker::DSL::Helpers
- Defined in:
- lib/beaker/dsl/helpers/host_helpers.rb
Overview
Methods that help you interact and manage the state of your Beaker SUTs, these methods do not require puppet to be installed to execute correctly
Instance Method Summary collapse
-
#add_system32_hosts_entry(host, opts = {}) ⇒ Object
Configure a host entry on the give host @example: will add a host entry for forge.puppetlabs.com add_system32_hosts_entry(host, { :ip => ‘23.251.154.122’, :name => ‘forge.puppetlabs.com’ }).
-
#backup_the_file(host, current_dir, new_dir, filename = 'puppet.conf') ⇒ String?
Back up the given file in the current_dir to the new_dir.
-
#check_for_package(host, package_name) ⇒ Boolean
Check to see if a package is installed on a remote host.
-
#create_remote_file(hosts, file_path, file_content, opts = {}) ⇒ Result
Create a remote file out of a string.
-
#create_tmpdir_on(host, path_prefix = '', user = nil) ⇒ String+
Create a temp directory on remote host owned by specified user.
-
#curl_on(host, cmd, opts = {}, &block) ⇒ Object
Run a curl command on the provided host(s).
- #curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1) ⇒ Object
-
#deploy_package_repo(host, path, name, version) ⇒ Object
Deploy packaging configurations generated by github.com/puppetlabs/packaging to a host.
-
#echo_on(hosts, val) ⇒ String, Array<String> The echo'ed value(s) returned by the host(s)
‘echo’ the provided value on the given host(s).
-
#execute_powershell_script_on(hosts, powershell_script, opts = {}) ⇒ Result
Execute a powershell script from file, remote file created from provided string.
- #exit_code ⇒ Object deprecated Deprecated.
-
#install_package(host, package_name, package_version = nil) ⇒ Result
Install a package on a host.
-
#on(host, command, opts = {}, &block) ⇒ Result
The primary method for executing commands on some set of hosts.
-
#retry_on(host, command, opts = {}, &block) ⇒ Result
This command will execute repeatedly until success or it runs out with an error.
-
#rsync_to(host, from_path, to_path, opts = {}) ⇒ Result
Move a local file or directory to a remote host using rsync.
-
#run_script(script, opts = {}, &block) ⇒ Object
Move a local script to default host and execute it.
-
#run_script_on(host, script, opts = {}, &block) ⇒ Result
Move a local script to a remote host and execute it.
-
#scp_from(host, from_path, to_path, opts = {}) ⇒ Result
Move a file from a remote to a local path.
-
#scp_to(host, from_path, to_path, opts = {}) ⇒ Result
Move a local file to a remote host using scp.
-
#shell(command, opts = {}, &block) ⇒ Result
The method for executing commands on the default host.
- #stderr ⇒ Object deprecated Deprecated.
- #stdout ⇒ Object deprecated Deprecated.
-
#upgrade_package(host, package_name) ⇒ Result
Upgrade a package on a host.
Instance Method Details
#add_system32_hosts_entry(host, opts = {}) ⇒ Object
Configure a host entry on the give host @example: will add a host entry for forge.puppetlabs.com
add_system32_hosts_entry(host, { :ip => '23.251.154.122', :name => 'forge.puppetlabs.com' })
357 358 359 360 361 362 363 364 365 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 357 def add_system32_hosts_entry(host, opts = {}) if host.is_powershell? hosts_file = "C:\\Windows\\System32\\Drivers\\etc\\hosts" host_entry = "#{opts['ip']}`t`t#{opts['name']}" on host, powershell("\$text = \\\"#{host_entry}\\\"; Add-Content -path '#{hosts_file}' -value \$text") else raise "nothing to do for #{host.name} on #{host['platform']}" end end |
#backup_the_file(host, current_dir, new_dir, filename = 'puppet.conf') ⇒ String?
Back up the given file in the current_dir to the new_dir
376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 376 def backup_the_file host, current_dir, new_dir, filename = 'puppet.conf' old_location = current_dir + '/' + filename new_location = new_dir + '/' + filename + '.bak' if host.file_exist? old_location host.exec( Command.new( "cp #{old_location} #{new_location}" ) ) return new_location else logger.warn "Could not backup file '#{old_location}': no such file" nil end end |
#check_for_package(host, package_name) ⇒ Boolean
Check to see if a package is installed on a remote host
338 339 340 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 338 def check_for_package host, package_name host.check_for_package package_name end |
#create_remote_file(hosts, file_path, file_content, opts = {}) ⇒ Result
This method uses Tempfile in Ruby’s STDLIB as well as #scp_to.
Create a remote file out of a string
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 256 def create_remote_file(hosts, file_path, file_content, opts = {}) Tempfile.open 'beaker' do |tempfile| File.open(tempfile.path, 'w') {|file| file.puts file_content } opts[:protocol] ||= 'scp' case opts[:protocol] when 'scp' scp_to hosts, tempfile.path, file_path, opts when 'rsync' rsync_to hosts, tempfile.path, file_path, opts else logger.debug "Unsupported transfer protocol, returning nil" nil end end end |
#create_tmpdir_on(host, path_prefix = '', user = nil) ⇒ String+
Create a temp directory on remote host owned by specified user.
directory. directory. If no username is specified defaults to the currently logged in user per host
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 508 def create_tmpdir_on(host, path_prefix = '', user=nil) block_on host do | host | # use default user logged into this host if not user user = host['user'] end if not on(host, "getent passwd #{user}").exit_code == 0 raise "User #{user} does not exist on #{host}." end if defined? host.tmpdir dir = host.tmpdir(path_prefix) on host, "chown #{user}:#{user} #{dir}" dir else raise "Host platform not supported by `create_tmpdir_on`." end end end |
#curl_on(host, cmd, opts = {}, &block) ⇒ Object
Run a curl command on the provided host(s)
398 399 400 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 398 def curl_on(host, cmd, opts = {}, &block) on host, "curl --tlsv1 %s" % cmd, opts, &block end |
#curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1) ⇒ Object
402 403 404 405 406 407 408 409 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 402 def curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1) opts = { :desired_exit_codes => desired_exit_codes, :max_retries => max_retries, :retry_interval => retry_interval } retry_on(host, "curl -m 1 #{url}", opts) end |
#deploy_package_repo(host, path, name, version) ⇒ Object
To ensure the repo configs are available for deployment, you should run ‘rake pl:jenkins:deb_repo_configs` and `rake pl:jenkins:rpm_repo_configs` on your project checkout
Deploy packaging configurations generated by github.com/puppetlabs/packaging to a host.
239 240 241 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 239 def deploy_package_repo host, path, name, version host.deploy_package_repo path, name, version end |
#echo_on(hosts, val) ⇒ String, Array<String> The echo'ed value(s) returned by the host(s)
‘echo’ the provided value on the given host(s)
535 536 537 538 539 540 541 542 543 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 535 def echo_on hosts, val block_on hosts do |host| if host.is_powershell? host.exec(Command.new("echo #{val}")).stdout.chomp else host.exec(Command.new("echo \"#{val}\"")).stdout.chomp end end end |
#execute_powershell_script_on(hosts, powershell_script, opts = {}) ⇒ Result
This method uses Tempfile in Ruby’s STDLIB as well as #create_remote_file.
Execute a powershell script from file, remote file created from provided string
282 283 284 285 286 287 288 289 290 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 282 def execute_powershell_script_on(hosts, powershell_script, opts = {}) block_on hosts do |host| script_path = "beaker_powershell_script_#{Time.now.to_i}.ps1" create_remote_file(host, script_path, powershell_script, opts) native_path = script_path.gsub(/\//, "\\") on host, powershell("", {"File" => native_path }), opts end end |
#exit_code ⇒ Object
An proxy for the last Result#exit_code returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
150 151 152 153 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 150 def exit_code return nil if @result.nil? @result.exit_code end |
#install_package(host, package_name, package_version = nil) ⇒ Result
Install a package on a host
328 329 330 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 328 def install_package host, package_name, package_version = nil host.install_package package_name, '', package_version end |
#on(host, command, opts = {}, &block) ⇒ Result
The primary method for executing commands on some set of hosts.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 62 def on(host, command, opts = {}, &block) block_on host do | host | if command.is_a? String cmd_opts = {} #add any additional environment variables to the command if opts[:environment] cmd_opts['ENV'] = opts[:environment] end command_object = Command.new(command.to_s, [], cmd_opts) elsif command.is_a? Command command_object = command else msg = "DSL method `on` can only be called with a String or Beaker::Command" msg << " object as the command parameter, not #{command.class}." raise ArgumentError, msg end @result = host.exec(command_object, opts) # Also, let additional checking be performed by the caller. if block_given? case block.arity #block with arity of 0, just hand back yourself when 0 yield self #block with arity of 1 or greater, hand back the result object else yield @result end end @result end end |
#retry_on(host, command, opts = {}, &block) ⇒ Result
This command will execute repeatedly until success or it runs out with an error
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 430 def retry_on(host, command, opts = {}, &block) option_exit_codes = opts[:desired_exit_codes] option_max_retries = opts[:max_retries].to_i option_retry_interval = opts[:retry_interval].to_f desired_exit_codes = option_exit_codes ? [option_exit_codes].flatten : [0] desired_exit_codes = [0] if desired_exit_codes.empty? max_retries = option_max_retries == 0 ? 60 : option_max_retries # nil & "" both return 0 retry_interval = option_retry_interval == 0 ? 1 : option_retry_interval verbose = true.to_s == opts[:verbose] log_prefix = host.log_prefix logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command}" logger.debug " Trying command #{max_retries} times." logger.debug ".", add_newline=false result = on host, command, {:accept_all_exit_codes => true, :silent => !verbose}, &block num_retries = 0 until desired_exit_codes.include?(result.exit_code) sleep retry_interval result = on host, command, {:accept_all_exit_codes => true, :silent => !verbose}, &block num_retries += 1 logger.debug ".", add_newline=false if (num_retries > max_retries) logger.debug " Command \`#{command}\` failed." fail("Command \`#{command}\` failed.") end end logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command} ostensibly successful." result end |
#rsync_to(host, from_path, to_path, opts = {}) ⇒ Result
rsync is required on the local host.
Move a local file or directory to a remote host using rsync
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 213 def rsync_to host, from_path, to_path, opts = {} block_on host do | host | if host['platform'] =~ /windows/ && to_path.match('`cygpath') result = host.echo "#{to_path}" to_path = result.raw_output.chomp end @result = host.do_rsync_to(from_path, to_path, opts) @result end end |
#run_script(script, opts = {}, &block) ⇒ Object
Move a local script to default host and execute it
318 319 320 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 318 def run_script(script, opts = {}, &block) run_script_on(default, script, opts, &block) end |
#run_script_on(host, script, opts = {}, &block) ⇒ Result
303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 303 def run_script_on(host, script, opts = {}, &block) # this is unsafe as it uses the File::SEPARATOR will be set to that # of the coordinator node. This works for us because we use cygwin # which will properly convert the paths. Otherwise this would not # work for running tests on a windows machine when the coordinator # that the harness is running on is *nix. We should use # {Beaker::Host#temp_path} instead. TODO remote_path = File.join("", "tmp", File.basename(script)) scp_to host, script, remote_path on host, remote_path, opts, &block end |
#scp_from(host, from_path, to_path, opts = {}) ⇒ Result
If using Host for the hosts scp is not required on the system as it uses Ruby’s net/scp library. The net-scp gem however is required (and specified in the gemspec).
Move a file from a remote to a local path
168 169 170 171 172 173 174 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 168 def scp_from host, from_path, to_path, opts = {} block_on host do | host | @result = host.do_scp_from(from_path, to_path, opts) @result.log logger @result end end |
#scp_to(host, from_path, to_path, opts = {}) ⇒ Result
If using Host for the hosts scp is not required on the system as it uses Ruby’s net/scp library. The net-scp gem however is required (and specified in the gemspec. When using SCP with Windows it will now auto expand path when using ‘cygpath instead of failing or requiring full path
Move a local file to a remote host using scp
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 191 def scp_to host, from_path, to_path, opts = {} block_on host do | host | if host['platform'] =~ /windows/ && to_path.match('`cygpath') result = on host, "echo #{to_path}" to_path = result.raw_output.chomp end @result = host.do_scp_to(from_path, to_path, opts) @result.log logger @result end end |
#shell(command, opts = {}, &block) ⇒ Result
The method for executing commands on the default host
121 122 123 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 121 def shell(command, opts = {}, &block) on(default, command, opts, &block) end |
#stderr ⇒ Object
An proxy for the last Result#stderr returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
140 141 142 143 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 140 def stderr return nil if @result.nil? @result.stderr end |
#stdout ⇒ Object
An proxy for the last Result#stdout returned by a method that makes remote calls. Use the Result object returned by the method directly instead. For Usage see Result.
130 131 132 133 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 130 def stdout return nil if @result.nil? @result.stdout end |
#upgrade_package(host, package_name) ⇒ Result
Upgrade a package on a host. The package must already be installed
348 349 350 |
# File 'lib/beaker/dsl/helpers/host_helpers.rb', line 348 def upgrade_package host, package_name host.upgrade_package package_name end |