Class: OsVm::Shell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, index, socket_path, log_path, default_timeout:, name: nil) ⇒ Shell

Returns a new instance of Shell.

Parameters:

  • machine (Machine)
  • index (Integer)
  • name (String, nil) (defaults to: nil)
  • socket_path (String)
  • log_path (String)
  • default_timeout (Integer)


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

#indexInteger (readonly)

Returns:

  • (Integer)


11
12
13
# File 'lib/osvm/shell.rb', line 11

def index
  @index
end

#ioObject (readonly, protected)

Returns the value of attribute io.



218
219
220
# File 'lib/osvm/shell.rb', line 218

def io
  @io
end

#logObject (readonly, protected)

Returns the value of attribute log.



218
219
220
# File 'lib/osvm/shell.rb', line 218

def log
  @log
end

#machineMachine (readonly)

Returns:



8
9
10
# File 'lib/osvm/shell.rb', line 8

def machine
  @machine
end

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



218
219
220
# File 'lib/osvm/shell.rb', line 218

def mutex
  @mutex
end

#nameString? (readonly)

Returns:

  • (String, nil)


14
15
16
# File 'lib/osvm/shell.rb', line 14

def name
  @name
end

#serverObject (readonly, protected)

Returns the value of attribute server.



218
219
220
# File 'lib/osvm/shell.rb', line 218

def server
  @server
end

#socket_pathString (readonly)

Returns:

  • (String)


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)



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/osvm/shell.rb', line 220

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
    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

Parameters:

  • cmds (String)

Returns:

  • (Array<Array<[Integer, String]>>)


178
179
180
# File 'lib/osvm/shell.rb', line 178

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

Parameters:

  • cmds (String)

Returns:

  • (Array<Array<[Integer, String]>>)


171
172
173
# File 'lib/osvm/shell.rb', line 171

def all_succeed(*cmds)
  cmds.map { |cmd| succeeds(cmd) }
end

#chardev_idObject



53
54
55
# File 'lib/osvm/shell.rb', line 53

def chardev_id
  index == 0 ? 'shell' : "shell#{index}"
end

#cleanupObject



81
82
83
84
85
# File 'lib/osvm/shell.rb', line 81

def cleanup
  File.unlink(socket_path)
rescue Errno::ENOENT
  # ignore
end

#closeObject



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.

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)

    exit status and output



130
131
132
133
134
135
136
137
138
# File 'lib/osvm/shell.rb', line 130

def execute(cmd, timeout: @default_timeout)
  machine.start unless machine.running?
  wait

  mutex.synchronize do
    wait
    execute_command(cmd, timeout:)
  end
end

#execute_command(cmd, timeout:) ⇒ Object (protected)



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/osvm/shell.rb', line 245

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

#fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>

Execute command and check that it fails

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)


158
159
160
161
162
163
164
165
166
# File 'lib/osvm/shell.rb', line 158

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

#finalizeObject



87
88
89
# File 'lib/osvm/shell.rb', line 87

def finalize
  log.close
end

#prepareObject



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_optionsObject



46
47
48
49
50
51
# File 'lib/osvm/shell.rb', line 46

def qemu_options
  [
    '-chardev', "socket,id=#{chardev_id},path=#{socket_path}",
    '-device', "virtconsole,chardev=#{chardev_id}"
  ]
end

#read_nonblock(io) ⇒ Object (protected)



324
325
326
327
328
# File 'lib/osvm/shell.rb', line 324

def read_nonblock(io)
  io.read_nonblock(4096)
rescue IO::WaitReadable
  ''
end

#read_output(timeout:, command:) ⇒ Object (protected)



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/osvm/shell.rb', line 286

def read_output(timeout:, command:)
  t1 = Time.now
  buffer = ''

  loop do
    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
      raise MachineShellClosed
    end

    break if buffer.end_with?("\n")
  end

  buffer
end

#resetObject (protected)



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/osvm/shell.rb', line 312

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

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)


144
145
146
147
148
149
150
151
152
# File 'lib/osvm/shell.rb', line 144

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

#up?Boolean

Returns:

  • (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.

Parameters:

  • timeout (Integer) (defaults to: @default_timeout)


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
# File 'lib/osvm/shell.rb', line 93

def wait(timeout: @default_timeout)
  raise "machine #{machine.name} is not running" unless machine.running?
  return if up?

  t1 = Time.now
  buffer = ''

  loop do
    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

Returns:

  • (Array<Integer, String>)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/osvm/shell.rb', line 201

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

Returns:

  • (Array<Integer, String>)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/osvm/shell.rb', line 184

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