Class: BoPeep::Host::CommandOutcome

Inherits:
Object
  • Object
show all
Defined in:
lib/bopeep.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, command, callbacks: nil, log: false, &setup) ⇒ CommandOutcome

Returns a new instance of CommandOutcome.



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/bopeep.rb', line 1022

def initialize(host, command, callbacks: nil, log: false, &setup)
  @host = host
  @command = command

  if log
    @console_status = Console::HostStatus.new(host)
  else
    @console_status = Console::HostStatus::DEV_NULL
  end

  @stdout = ""
  @stderr = ""

  @started_at = nil
  @finished_at = nil

  @callbacks = Callbacks.from(callbacks, &setup)
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



1009
1010
1011
# File 'lib/bopeep.rb', line 1009

def command
  @command
end

#connection_error_codeObject (readonly)

Returns the value of attribute connection_error_code.



1010
1011
1012
# File 'lib/bopeep.rb', line 1010

def connection_error_code
  @connection_error_code
end

#connection_error_reasonObject (readonly)

Returns the value of attribute connection_error_reason.



1011
1012
1013
# File 'lib/bopeep.rb', line 1011

def connection_error_reason
  @connection_error_reason
end

#console_stateObject (readonly)

Returns the value of attribute console_state.



1012
1013
1014
# File 'lib/bopeep.rb', line 1012

def console_state
  @console_state
end

#exit_signalObject

Returns the value of attribute exit_signal.



1013
1014
1015
# File 'lib/bopeep.rb', line 1013

def exit_signal
  @exit_signal
end

#exit_statusObject

Returns the value of attribute exit_status.



1014
1015
1016
# File 'lib/bopeep.rb', line 1014

def exit_status
  @exit_status
end

#failure_reasonObject (readonly)

Returns the value of attribute failure_reason.



1015
1016
1017
# File 'lib/bopeep.rb', line 1015

def failure_reason
  @failure_reason
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



1016
1017
1018
# File 'lib/bopeep.rb', line 1016

def finished_at
  @finished_at
end

#hostObject (readonly)

Returns the value of attribute host.



1017
1018
1019
# File 'lib/bopeep.rb', line 1017

def host
  @host
end

#started_atObject (readonly)

Returns the value of attribute started_at.



1018
1019
1020
# File 'lib/bopeep.rb', line 1018

def started_at
  @started_at
end

#stderrObject (readonly)

Returns the value of attribute stderr.



1019
1020
1021
# File 'lib/bopeep.rb', line 1019

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



1020
1021
1022
# File 'lib/bopeep.rb', line 1020

def stdout
  @stdout
end

Instance Method Details

#collect_stderr(data) ⇒ Object



1050
1051
1052
1053
# File 'lib/bopeep.rb', line 1050

def collect_stderr(data)
  @stderr << data
  @callbacks.on_stderr(data)
end

#collect_stdout(data) ⇒ Object



1045
1046
1047
1048
# File 'lib/bopeep.rb', line 1045

def collect_stdout(data)
  @stdout << data
  @callbacks.on_stdout(data)
end

#command_finished!Object



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'lib/bopeep.rb', line 1118

def command_finished!
  if finished?
    $stderr.puts "[WARN] command already finished"
  else
    @stdout.freeze
    @stderr.freeze

    @finished_at = Time.now
    @finished_at.freeze

    if successful?
      @console_status.success!
      @callbacks.on_success(self)
    else
      @console_status.failed!(failure_summary)
    end
  end
end

#command_started!Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/bopeep.rb', line 1107

def command_started!
  if @started_at
    $stderr.puts "[WARN] command already started"
  else
    @started_at = Time.now
    @started_at.freeze

    @console_status.started!
  end
end

#connection_failed(code, reason) ⇒ Object



1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/bopeep.rb', line 1137

def connection_failed(code, reason)
  @connection_error_code = code.freeze
  @connection_error_reason = reason.freeze

  @failure_reason = :connection_error

  unless started?
    @console_status.failed!(failure_summary)
  end

  @callbacks.on_failure(self)
end

#durationObject



1101
1102
1103
1104
1105
# File 'lib/bopeep.rb', line 1101

def duration
  if @finished_at && @started_at
    @finished_at - @started_at
  end
end

#failed?Boolean

Returns:

  • (Boolean)


1059
1060
1061
# File 'lib/bopeep.rb', line 1059

def failed?
  !successful?
end

#failure_summaryObject



1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
# File 'lib/bopeep.rb', line 1150

def failure_summary
  case failure_reason
  when :exit_signal, :nonzero_exit_status
    adjusted_stdout, adjusted_stderr = [stdout, stderr].map do |output|
      adjusted = output.each_line.map { |line| line.prepend("  ") }.join.chomp

      if adjusted.empty?
        adjusted = "(empty)"
      end

      adjusted
    end

    <<~OUTPUT
      STDOUT: #{adjusted_stdout}
      STDERR: #{adjusted_stderr}
    OUTPUT
  when :connection_error
    <<~OUTPUT
      Connection failed:
        Code: #{connection_error_code}
        Reason: #{connection_error_reason}
    OUTPUT
  end
end

#finished?Boolean

Returns:

  • (Boolean)


1067
1068
1069
# File 'lib/bopeep.rb', line 1067

def finished?
  not @finished_at.nil?
end

#started?Boolean

Returns:

  • (Boolean)


1063
1064
1065
# File 'lib/bopeep.rb', line 1063

def started?
  not @started_at.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


1055
1056
1057
# File 'lib/bopeep.rb', line 1055

def successful?
  exit_status && exit_status.zero?
end

#valueObject



1041
1042
1043
# File 'lib/bopeep.rb', line 1041

def value
  self
end