lib/beaker/dsl/helpers.rb in beaker-1.21.0 vs lib/beaker/dsl/helpers.rb in beaker-2.0.0

- old
+ new

@@ -185,10 +185,12 @@ # Move a local file to a remote host # @note If using {Beaker::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 # # @param [Host, #do_scp_to] host One or more hosts (or some object # that responds like # {Beaker::Host#do_scp_to}. # @param [String] from_path A local path to a file. @@ -196,10 +198,14 @@ # @!macro common_opts # # @return [Result] Returns the result of the SCP operation 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 @@ -254,11 +260,11 @@ # if this puppet command returns a non-zero exit code. # # @return [String] Returns the name of the newly-created file. def create_tmpdir_for_user(host, name='/tmp/beaker', user=nil) if not user - result = on(host, "puppet master --configprint user") + result = on host, puppet("master --configprint user") if not result.exit_code == 0 raise "`puppet master --configprint` failed, check that puppet is installed on #{host} or explicitly pass in a user name." end user = result.stdout.strip end @@ -672,11 +678,11 @@ # @!visibility private def dump_puppet_log(host) syslogfile = case host['platform'] when /fedora|centos|el|redhat|scientific/ then '/var/log/messages' - when /ubuntu|debian/ then '/var/log/syslog' + when /ubuntu|debian|cumulus/ then '/var/log/syslog' else return end logger.notify "\n*************************" logger.notify "* Dumping master log *" @@ -859,22 +865,29 @@ # @option opts [String] :modulepath The search path for modules, as # a list of directories separated by the system # path separator character. (The POSIX path separator # is ‘:’, and the Windows path separator is ‘;’.) # + # @option opts [String] :debug (false) If this option exists, + # the "--debug" command line parameter + # will be passed to the 'puppet apply' command. + # # @param [Block] block This method will yield to a block of code passed # by the caller; this can be used for additional # validation, etc. # def apply_manifest_on(host, manifest, opts = {}, &block) block_on host do | host | - on_options = {} on_options[:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes]) puppet_apply_opts = {} - puppet_apply_opts[:verbose] = nil + if opts[:debug] + puppet_apply_opts[:debug] = nil + else + puppet_apply_opts[:verbose] = nil + end puppet_apply_opts[:parseonly] = nil if opts[:parseonly] puppet_apply_opts[:trace] = nil if opts[:trace] puppet_apply_opts[:parser] = 'future' if opts[:future_parser] puppet_apply_opts[:modulepath] = opts[:modulepath] if opts[:modulepath] puppet_apply_opts[:noop] = nil if opts[:noop] @@ -1113,34 +1126,75 @@ curl_with_retries("start puppetdb", host, "http://localhost:8080", 0, 120) curl_with_retries("start puppetdb (ssl)", host, "https://#{host.node_name}:8081", [35, 60]) end + def sleep_until_puppetserver_started(host) + curl_with_retries("start puppetserver (ssl)", + host, "https://#{host.node_name}:8140", [35, 60]) + end + + def sleep_until_nc_started(host) + curl_with_retries("start nodeclassifier (ssl)", + host, "https://#{host.node_name}:4433", [35, 60]) + end + def curl_with_retries(desc, host, url, desired_exit_codes, max_retries = 60, retry_interval = 1) - retry_command(desc, host, "curl -m 1 #{url}", desired_exit_codes, max_retries, retry_interval) + opts = { + :desired_exit_codes => desired_exit_codes, + :max_retries => max_retries, + :retry_interval => retry_interval + } + retry_on(host, "curl -m 1 #{url}", opts) end - def retry_command(desc, host, command, desired_exit_codes = 0, - max_retries = 60, retry_interval = 1, verbose = false) + # This command will execute repeatedly until success or it runs out with an error + # + # @param [Host, Array<Host>, String, Symbol] host One or more hosts to act upon, + # or a role (String or Symbol) that identifies one or more hosts. + # @param [String, Command] command The command to execute on *host*. + # @param [Hash{Symbol=>String}] opts Options to alter execution. + # @param [Proc] block Additional actions or assertions. + # + # @option opts [Array<Fixnum>, Fixnum] :desired_exit_codes (0) An array + # or integer exit code(s) that should be considered + # acceptable. An error will be thrown if the exit code never + # matches one of the values in this list. + # @option opts [Fixnum] :max_retries (60) number of times the + # command will be tried before failing + # @option opts [Float] :retry_interval (1) number of seconds + # that we'll wait between tries + # @option opts [Boolean] :verbose (false) + 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 - desired_exit_codes = [desired_exit_codes].flatten - result = on host, command, {:acceptable_exit_codes => (0...127), :silent => !verbose} + + result = on host, command, {:acceptable_exit_codes => (0...127), :silent => !verbose}, &block num_retries = 0 until desired_exit_codes.include?(result.exit_code) sleep retry_interval - result = on host, command, {:acceptable_exit_codes => (0...127), :silent => !verbose} + result = on host, command, {:acceptable_exit_codes => (0...127), :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 #Is semver-ish version a less than semver-ish version b #@param [String] a A version of the from '\d.\d.\d.*' #@param [String] b A version of the form '\d.\d.\d.*' @@ -1217,11 +1271,11 @@ #wait for a given host to appear in the dashboard def wait_for_host_in_dashboard(host) hostname = host.node_name - retry_command("Wait for #{hostname} to be in the console", dashboard, "! curl --tlsv1 -k -I https://#{dashboard}/nodes/#{hostname} | grep '404 Not Found'") + retry_on(dashboard, "! curl --tlsv1 -k -I https://#{dashboard}/nodes/#{hostname} | grep '404 Not Found'") end # Ensure the host has requested a cert, then sign it # # @param [Host, Array<Host>, String, Symbol] host One or more hosts to act upon, @@ -1243,10 +1297,10 @@ last_sleep = 0 next_sleep = 1 (0..10).each do |i| fail_test("Failed to sign cert for #{hostname}") if i == 10 - on master, puppet("cert --sign --all"), :acceptable_exit_codes => [0,24] + on master, puppet("cert --sign --all --allow-dns-alt-names"), :acceptable_exit_codes => [0,24] break if on(master, puppet("cert --list --all")).stdout =~ /\+ "?#{hostname}"?/ sleep next_sleep (last_sleep, next_sleep) = next_sleep, last_sleep+next_sleep end