Method: Beaker::Host#exec

Defined in:
lib/beaker/host.rb

#exec(command, options = {}) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/beaker/host.rb', line 324

def exec command, options={}
  result = nil
  # I've always found this confusing
  cmdline = command.cmd_line(self)

  # use the value of :dry_run passed to the method unless
  # undefined, then use parsed @options hash.
  options[:dry_run] ||= @options[:dry_run]

  if options[:dry_run]
    @logger.debug "\n Running in :dry_run mode. Command #{cmdline} not executed."
    result = Beaker::NullResult.new(self, command)
    return result
  end

  if options[:silent]
    output_callback = nil
  else
    @logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{cmdline}"
    if @options[:color_host_output]
      output_callback = logger.method(:color_host_output)
    else
      output_callback = logger.method(:host_output)
    end
  end

  unless options[:dry_run]
    # is this returning a result object?
    # the options should come at the end of the method signature (rubyism)
    # and they shouldn't be ssh specific

    seconds = Benchmark.realtime {
      @logger.with_indent do
        result = connection.execute(cmdline, options, output_callback)
      end
    }

    if not options[:silent]
      @logger.debug "\n#{log_prefix} executed in %0.2f seconds" % seconds
    end

    if options[:reset_connection]
      # Expect the connection to fail hard and possibly take a long time timeout.
      # Pre-emptively reset it so we don't wait forever.
      close
      return result
    end

    unless options[:silent]
      # What?
      result.log(@logger)
      if !options[:expect_connection_failure] && !result.exit_code
        # no exit code was collected, so the stream failed
        raise CommandFailure, "Host '#{self}' connection failure running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"

      end
      if options[:expect_connection_failure] && result.exit_code
        # should have had a connection failure, but didn't
        # wait to see if the connection failure will be generation, otherwise raise error
        if not connection.wait_for_connection_failure(options, output_callback)
          raise CommandFailure,  "Host '#{self}' should have resulted in a connection failure running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"
        end
      end
      # No, TestCase has the knowledge about whether its failed, checking acceptable
      # exit codes at the host level and then raising...
      # is it necessary to break execution??
      if options[:accept_all_exit_codes] && options[:acceptable_exit_codes]
        @logger.warn ":accept_all_exit_codes & :acceptable_exit_codes set. :acceptable_exit_codes overrides, but they shouldn't both be set at once"
        options[:accept_all_exit_codes] = false
      end
      if !options[:accept_all_exit_codes] && !result.exit_code_in?(Array(options[:acceptable_exit_codes] || [0, nil]))
        raise CommandFailure, "Host '#{self}' exited with #{result.exit_code} running:\n #{cmdline}\nLast #{@options[:trace_limit]} lines of output were:\n#{result.formatted_output(@options[:trace_limit])}"
      end
    end
  end
  result
end