Class: OsVm::Shell
- Inherits:
-
Object
- Object
- OsVm::Shell
- Defined in:
- lib/osvm/shell.rb
Instance Attribute Summary collapse
- #index ⇒ Integer readonly
-
#io ⇒ Object
readonly
protected
Returns the value of attribute io.
-
#log ⇒ Object
readonly
protected
Returns the value of attribute log.
- #machine ⇒ Machine readonly
-
#mutex ⇒ Object
readonly
protected
Returns the value of attribute mutex.
- #name ⇒ String? readonly
-
#server ⇒ Object
readonly
protected
Returns the value of attribute server.
- #socket_path ⇒ String readonly
Instance Method Summary collapse
- #accept(timeout: @default_timeout) ⇒ Object protected
-
#all_fail(*cmds) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all fail.
-
#all_succeed(*cmds) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all succeed.
- #chardev_id ⇒ Object
- #cleanup ⇒ Object
- #close ⇒ Object
-
#execute(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Exit status and output.
- #execute_command(cmd, timeout:) ⇒ Object protected
- #expect_with_retries(cmd, attempts:, retry_delay:, timeout:, success:) ⇒ Object protected
-
#fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it fails.
-
#fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute a command repeatedly until it fails or all attempts are used.
- #finalize ⇒ Object
-
#initialize(machine, index, socket_path, log_path, default_timeout:, name: nil) ⇒ Shell
constructor
A new instance of Shell.
- #prepare ⇒ Object
- #qemu_options ⇒ Object
- #read_nonblock(io) ⇒ Object protected
- #read_output(timeout:, command:) ⇒ Object protected
- #reset ⇒ Object protected
-
#succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it succeeds.
-
#succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute a command repeatedly until it succeeds or all attempts are used.
- #up? ⇒ Boolean
- #wait(timeout: @default_timeout) ⇒ void
-
#wait_until_fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command fails.
-
#wait_until_succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command succeeds.
Constructor Details
#initialize(machine, index, socket_path, log_path, default_timeout:, name: nil) ⇒ Shell
Returns a new instance of Shell.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/osvm/shell.rb', line 25 def initialize(machine, index, socket_path, log_path, default_timeout:, name: nil) @machine = machine @index = index @name = name @socket_path = socket_path @default_timeout = default_timeout @log = ShellLog.new(log_path, shell_index: index, shell_name: name) @mutex = Mutex.new @up = false @server = nil @io = nil end |
Instance Attribute Details
#index ⇒ Integer (readonly)
11 12 13 |
# File 'lib/osvm/shell.rb', line 11 def index @index end |
#io ⇒ Object (readonly, protected)
Returns the value of attribute io.
243 244 245 |
# File 'lib/osvm/shell.rb', line 243 def io @io end |
#log ⇒ Object (readonly, protected)
Returns the value of attribute log.
243 244 245 |
# File 'lib/osvm/shell.rb', line 243 def log @log end |
#mutex ⇒ Object (readonly, protected)
Returns the value of attribute mutex.
243 244 245 |
# File 'lib/osvm/shell.rb', line 243 def mutex @mutex end |
#name ⇒ String? (readonly)
14 15 16 |
# File 'lib/osvm/shell.rb', line 14 def name @name end |
#server ⇒ Object (readonly, protected)
Returns the value of attribute server.
243 244 245 |
# File 'lib/osvm/shell.rb', line 243 def server @server end |
#socket_path ⇒ String (readonly)
17 18 19 |
# File 'lib/osvm/shell.rb', line 17 def socket_path @socket_path end |
Instance Method Details
#accept(timeout: @default_timeout) ⇒ Object (protected)
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/osvm/shell.rb', line 267 def accept(timeout: @default_timeout) raise "machine #{machine.name} is not running" unless machine.running? return io unless io.nil? t1 = Time.now loop do machine.raise_if_kernel_failed! if t1 + timeout < Time.now raise TimeoutError, 'Timeout occurred while waiting for shell connection' elsif !machine.running? raise Error, 'Machine is not running' end rs = server.wait_readable(1) next unless rs begin @io = server.accept_nonblock return io rescue IO::WaitReadable, Errno::EINTR next end end end |
#all_fail(*cmds) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all fail
203 204 205 |
# File 'lib/osvm/shell.rb', line 203 def all_fail(*cmds) cmds.map { |cmd| fails(cmd) } end |
#all_succeed(*cmds) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all succeed
196 197 198 |
# File 'lib/osvm/shell.rb', line 196 def all_succeed(*cmds) cmds.map { |cmd| succeeds(cmd) } end |
#chardev_id ⇒ Object
53 54 55 |
# File 'lib/osvm/shell.rb', line 53 def chardev_id index == 0 ? 'shell' : "shell#{index}" end |
#cleanup ⇒ Object
81 82 83 84 85 |
# File 'lib/osvm/shell.rb', line 81 def cleanup File.unlink(socket_path) rescue Errno::ENOENT # ignore end |
#close ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/osvm/shell.rb', line 61 def close begin server&.close rescue IOError # ignore ensure @server = nil end begin io&.close rescue IOError # ignore ensure @io = nil end @up = false end |
#execute(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Returns exit status and output.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/osvm/shell.rb', line 133 def execute(cmd, timeout: @default_timeout) machine.raise_if_kernel_failed! machine.start unless machine.running? machine.raise_if_kernel_failed! wait mutex.synchronize do wait execute_command(cmd, timeout:) end end |
#execute_command(cmd, timeout:) ⇒ Object (protected)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/osvm/shell.rb', line 294 def execute_command(cmd, timeout:) real_timeout = [timeout, 5].max vm_command = "set -euo pipefail; #{cmd}" timeout_command = "timeout #{real_timeout}" # For unknown reason, the first character written to the shell is cut. Sometimes # more characters are lost. We therefore prefix the executed command with whitespace # which can be lost. workaround = ' ' * 10 io.write("#{workaround}#{timeout_command} bash -c #{Shellwords.escape(vm_command)} 2>&1 | (base64 -w 0; echo)\n") log_started_at = log.execute_begin(cmd) begin raw_output = read_output(timeout: real_timeout + 5, command: vm_command) rescue MachineShellClosed log.execute_end(-1, '[machine shell closed]', log_started_at) raise end output = Base64.decode64(raw_output) io.write("#{workaround}echo ${PIPESTATUS[0]}\n") begin status = read_output(timeout: 60, command: 'echo ${PIPESTATUS[0]}').strip.to_i rescue MachineShellClosed log.execute_end(-1, output, log_started_at) raise end if timeout && status == 124 log.execute_end(-1, output, log_started_at) raise TimeoutError, "Timeout occurred while running command '#{cmd}', " \ "output: #{output.inspect}" end log.execute_end(status, output, log_started_at) [status, output] end |
#expect_with_retries(cmd, attempts:, retry_delay:, timeout:, success:) ⇒ Object (protected)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/osvm/shell.rb', line 245 def expect_with_retries(cmd, attempts:, retry_delay:, timeout:, success:) unless attempts.is_a?(Integer) && attempts > 0 raise ArgumentError, 'attempts must be a positive integer' end status = output = nil attempts.times do |attempt| status, output = execute(cmd, timeout:) matches = success ? (status == 0) : (status != 0) return [status, output] if matches sleep(retry_delay) unless attempt + 1 == attempts end if success raise CommandFailed, "Command '#{cmd}' failed with status #{status}. Output:\n #{output}" end raise CommandSucceeded, "Command '#{cmd}' succeeds with status #{status}. Output:\n #{output}" end |
#fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it fails
173 174 175 176 177 178 179 180 181 |
# File 'lib/osvm/shell.rb', line 173 def fails(cmd, timeout: @default_timeout) status, output = execute(cmd, timeout:) if status == 0 raise CommandSucceeded, "Command '#{cmd}' succeeds with status #{status}. Output:\n #{output}" end [status, output] end |
#fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute a command repeatedly until it fails or all attempts are used
189 190 191 |
# File 'lib/osvm/shell.rb', line 189 def fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) expect_with_retries(cmd, attempts:, retry_delay:, timeout:, success: false) end |
#finalize ⇒ Object
87 88 89 |
# File 'lib/osvm/shell.rb', line 87 def finalize log.close end |
#prepare ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/osvm/shell.rb', line 38 def prepare File.unlink(socket_path) rescue Errno::ENOENT # ignore ensure @server = UNIXServer.new(socket_path) end |
#qemu_options ⇒ Object
46 47 48 49 50 51 |
# File 'lib/osvm/shell.rb', line 46 def [ '-chardev', "socket,id=#{chardev_id},path=#{socket_path}", '-device', "virtconsole,chardev=#{chardev_id}" ] end |
#read_nonblock(io) ⇒ Object (protected)
376 377 378 379 380 |
# File 'lib/osvm/shell.rb', line 376 def read_nonblock(io) io.read_nonblock(4096) rescue IO::WaitReadable '' end |
#read_output(timeout:, command:) ⇒ Object (protected)
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 |
# File 'lib/osvm/shell.rb', line 335 def read_output(timeout:, command:) t1 = Time.now buffer = '' loop do machine.raise_if_kernel_failed! if t1 + timeout < Time.now raise UnrecoverableTimeoutError, "Timeout occurred while running command '#{command}', " \ "buffer contents: #{buffer.inspect}" end rs = io.wait_readable(1) next unless rs begin buffer << read_nonblock(io) rescue EOFError reset machine.raise_if_kernel_failed! raise MachineShellClosed end break if buffer.end_with?("\n") end buffer end |
#reset ⇒ Object (protected)
364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/osvm/shell.rb', line 364 def reset @up = false return if io.nil? io.close unless io.closed? rescue IOError # ignore ensure @io = nil end |
#succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it succeeds
149 150 151 152 153 154 155 156 157 |
# File 'lib/osvm/shell.rb', line 149 def succeeds(cmd, timeout: @default_timeout) status, output = execute(cmd, timeout:) if status != 0 raise CommandFailed, "Command '#{cmd}' failed with status #{status}. Output:\n #{output}" end [status, output] end |
#succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute a command repeatedly until it succeeds or all attempts are used
165 166 167 |
# File 'lib/osvm/shell.rb', line 165 def succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout) expect_with_retries(cmd, attempts:, retry_delay:, timeout:, success: true) end |
#up? ⇒ Boolean
57 58 59 |
# File 'lib/osvm/shell.rb', line 57 def up? @up end |
#wait(timeout: @default_timeout) ⇒ void
This method returns an undefined value.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/osvm/shell.rb', line 93 def wait(timeout: @default_timeout) machine.raise_if_kernel_failed! raise "machine #{machine.name} is not running" unless machine.running? return if up? t1 = Time.now buffer = '' loop do machine.raise_if_kernel_failed! if t1 + timeout < Time.now raise TimeoutError, 'Timeout occurred while waiting for shell' end reset if io&.closed? accept(timeout: t1 + timeout - Time.now) if io.nil? rs = io.wait_readable(1) next unless rs begin buffer << read_nonblock(io) rescue EOFError reset buffer = '' next end next unless buffer.include?("test-shell-ready\n") @up = true machine.__send__(:mount_shared_dir_once) return end end |
#wait_until_fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command fails
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/osvm/shell.rb', line 226 def wait_until_fails(cmd, timeout: @default_timeout) t1 = Time.now cur_timeout = timeout loop do status, output = execute(cmd, timeout: cur_timeout) return [status, output] if status != 0 cur_timeout = timeout - (Time.now - t1) raise TimeoutError, "Timeout occurred while running command '#{cmd}'" if cur_timeout <= 0 sleep(1) end end |
#wait_until_succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command succeeds
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/osvm/shell.rb', line 209 def wait_until_succeeds(cmd, timeout: @default_timeout) t1 = Time.now cur_timeout = timeout loop do status, output = execute(cmd, timeout: cur_timeout) return [status, output] if status == 0 cur_timeout = timeout - (Time.now - t1) raise TimeoutError, "Timeout occurred while running command '#{cmd}'" if cur_timeout <= 0 sleep(1) end end |