Class: OsVm::Machine

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

Direct Known Subclasses

NixosMachine, VpsadminosMachine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, tmpdir, sockdir, default_timeout: 600, hash_base: '', interactive_console: false) ⇒ Machine

Returns a new instance of Machine.

Parameters:

  • name (String)
  • config (MachineConfig)
  • tmpdir (String)
  • sockdir (String)
  • default_timeout (Integer) (defaults to: 600)
  • hash_base (String) (defaults to: '')
  • interactive_console (Boolean) (defaults to: false)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/osvm/machine.rb', line 23

def initialize(name, config, tmpdir, sockdir, default_timeout: 600, hash_base: '', interactive_console: false)
  @name = name
  @config = config
  @tmpdir = tmpdir
  @sockdir = sockdir
  @default_timeout = default_timeout
  @hash_base = hash_base
  @interactive_console = interactive_console
  @start_kernel_params = []
  @running = false
  @shell_up = false
  @shared_dir = SharedDir.new(self)
  @shared_filesystems = {
    shared_dir.fs_name => shared_dir.host_path
  }.merge(config.shared_filesystems)
  @virtiofsd_pids = []
  @mutex = Mutex.new

  FileUtils.mkdir_p(tmpdir)
  FileUtils.mkdir_p(sockdir)
  @log = MachineLog.new(File.join(tmpdir, "#{name}-log.log"))
end

Instance Attribute Details

#configObject (readonly, protected)

Returns the value of attribute config.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def config
  @config
end

#console_threadObject (readonly, protected)

Returns the value of attribute console_thread.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def console_thread
  @console_thread
end

#hash_baseObject (readonly, protected)

Returns the value of attribute hash_base.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def hash_base
  @hash_base
end

#logObject (readonly, protected)

Returns the value of attribute log.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def log
  @log
end

#nameString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/osvm/machine.rb', line 10

def name
  @name
end

#qemu_pidObject (readonly, protected)

Returns the value of attribute qemu_pid.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def qemu_pid
  @qemu_pid
end

#qemu_readObject (readonly, protected)

Returns the value of attribute qemu_read.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def qemu_read
  @qemu_read
end

#qemu_reaperObject (readonly, protected)

Returns the value of attribute qemu_reaper.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def qemu_reaper
  @qemu_reaper
end

#shared_dirObject (readonly, protected)

Returns the value of attribute shared_dir.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def shared_dir
  @shared_dir
end

#shared_filesystemsObject (readonly, protected)

Returns the value of attribute shared_filesystems.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def shared_filesystems
  @shared_filesystems
end

#shellObject (readonly, protected)

Returns the value of attribute shell.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def shell
  @shell
end

#shell_serverObject (readonly, protected)

Returns the value of attribute shell_server.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def shell_server
  @shell_server
end

#sockdirObject (readonly, protected)

Returns the value of attribute sockdir.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def sockdir
  @sockdir
end

#start_kernel_paramsArray<String> (readonly)

Kernel parameters passed to #start

Returns:

  • (Array<String>)


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

def start_kernel_params
  @start_kernel_params
end

#tmpdirObject (readonly, protected)

Returns the value of attribute tmpdir.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def tmpdir
  @tmpdir
end

#virtiofsd_pidsObject (readonly, protected)

Returns the value of attribute virtiofsd_pids.



466
467
468
# File 'lib/osvm/machine.rb', line 466

def virtiofsd_pids
  @virtiofsd_pids
end

Instance Method Details

#all_fail(*cmds) ⇒ Array<Array<[Integer, String]>>

Execute all commands and check that they all fail

Parameters:

  • cmds (String)

Returns:

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


321
322
323
# File 'lib/osvm/machine.rb', line 321

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


314
315
316
# File 'lib/osvm/machine.rb', line 314

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

#base_kernel_params(kernel_params) ⇒ Object (protected)



478
479
480
481
482
483
# File 'lib/osvm/machine.rb', line 478

def base_kernel_params(kernel_params)
  [
    'console=ttyS0',
    "init=#{config.toplevel}/init"
  ] + config.kernel_params + kernel_params
end

#booted?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/osvm/machine.rb', line 219

def booted?
  shell_up?
end

#can_execute?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/osvm/machine.rb', line 224

def can_execute?
  shell_up?
end

#cleanupMachine

Cleanup machine state

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/osvm/machine.rb', line 197

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

  shared_filesystems.each_key do |fs_name|
    File.unlink(virtiofs_socket_path(fs_name))
  rescue Errno::ENOENT
    # ignore
  end

  self
end

#console_log_pathObject (protected)



647
648
649
# File 'lib/osvm/machine.rb', line 647

def console_log_path
  File.join(tmpdir, "#{name}-console.log")
end

#destroyMachine

Destroy the machine

Returns:



172
173
174
175
176
177
# File 'lib/osvm/machine.rb', line 172

def destroy
  log.destroy
  shared_dir.destroy
  destroy_disks
  self
end

#destroy_disksMachine

Destroy file-backed disks

Disks are destroyed automatically or when #destroy is called. #destroy_disks can be used to reset storage between machine runs.

Returns:



185
186
187
188
189
190
191
192
193
# File 'lib/osvm/machine.rb', line 185

def destroy_disks
  config.disks.each do |disk|
    next if disk.type != 'file' || !disk.create

    FileUtils.rm_f(disk_path(disk.device))
  end

  self
end

#disk_path(path) ⇒ Object (protected)



651
652
653
654
655
656
657
658
659
# File 'lib/osvm/machine.rb', line 651

def disk_path(path)
  resolved = path.gsub('{machine}', name)

  if resolved.start_with?('/')
    resolved
  else
    File.join(tmpdir, resolved)
  end
end

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

Execute a command

Parameters:

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

Returns:

  • (Array<Integer, String>)

    exit status and output

Raises:



239
240
241
242
243
244
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
# File 'lib/osvm/machine.rb', line 239

def execute(cmd, timeout: @default_timeout)
  start unless running?
  wait_for_shell

  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

  shell.write("#{workaround}#{timeout_command} bash -c #{Shellwords.escape(vm_command)} 2>&1 | (base64 -w 0; echo)\n")
  log.execute_begin(cmd)

  begin
    raw_output = read_shell_output(timeout: real_timeout + 5, command: vm_command)
  rescue MachineShellClosed
    log.execute_end(-1, '[machine shell closed]')
    raise
  end

  output = Base64.decode64(raw_output)

  shell.write("#{workaround}echo ${PIPESTATUS[0]}\n")

  begin
    status = read_shell_output(timeout: 60, command: 'echo ${PIPESTATUS[0]}').strip.to_i
  rescue MachineShellClosed
    log.execute_end(-1, output)
    raise
  end

  if timeout && status == 124
    log.execute_end(-1, output)
    raise TimeoutError, "Timeout occurred while running command '#{cmd}', " \
                        "output: #{output.inspect}"
  end

  log.execute_end(status, output)
  [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>)


301
302
303
304
305
306
307
308
309
# File 'lib/osvm/machine.rb', line 301

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



46
47
48
# File 'lib/osvm/machine.rb', line 46

def finalize
  log.close
end

#inspectObject



460
461
462
# File 'lib/osvm/machine.rb', line 460

def inspect
  "#<#{self.class.name}:#{object_id} name=#{name}>"
end

#join(timeout: @default_timeout) ⇒ Object

Block until the machine stops



111
112
113
114
# File 'lib/osvm/machine.rb', line 111

def join(timeout: @default_timeout)
  qemu_reaper.join(timeout)
  nil
end

#kill(signal: 'TERM') ⇒ Machine

Kill the machine

Parameters:

  • signal ('TERM', 'KILL') (defaults to: 'TERM')

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/osvm/machine.rb', line 137

def kill(signal: 'TERM')
  unless running?
    log.kill('NONE')
    return
  end

  log.kill(signal)

  begin
    Process.kill(signal, qemu_pid)
  rescue Errno::ESRCH
    warn "Unable to kill machine #{name} using SIG#{signal}"
  end

  if signal == 'KILL'
    qemu_reaper.join
    return self
  elsif qemu_reaper.join(60)
    return self
  end

  log.kill('KILL')

  begin
    Process.kill('KILL', qemu_pid)
  rescue Errno::ESRCH
    warn "Unable to kill machine #{name} using SIGKILL"
  end

  qemu_reaper.join
  self
end

#mkdir(path) ⇒ Machine

Create a directory inside the machine

Parameters:

  • path (String)

    path within the machine

Returns:



428
429
430
431
# File 'lib/osvm/machine.rb', line 428

def mkdir(path)
  succeeds("mkdir \"#{path}\"")
  self
end

#mkdir_p(path) ⇒ Machine

Create a directory inside the machine

Parameters:

  • path (String)

    path within the machine

Returns:



436
437
438
439
# File 'lib/osvm/machine.rb', line 436

def mkdir_p(path)
  succeeds("mkdir -p \"#{path}\"")
  self
end

#prepare_disksObject (protected)



606
607
608
609
610
611
612
# File 'lib/osvm/machine.rb', line 606

def prepare_disks
  config.disks.each do |disk|
    next if disk.type != 'file' || !disk.create || File.exist?(disk_path(disk.device))

    `truncate -s#{disk.size} #{disk_path(disk.device)}`
  end
end

#pull_file(src, preserve: false) ⇒ String

Pull file from the machine to the host

Parameters:

  • src (String)

    file within the machine

Returns:

  • (String)

    path to the file on the host



456
457
458
# File 'lib/osvm/machine.rb', line 456

def pull_file(src, preserve: false)
  shared_dir.pull_file(src, preserve:)
end

#push_file(src, dst, preserve: false, mkpath: false) ⇒ Machine

Push file from the host to the machine

Parameters:

  • src (String)

    file on the host

  • dst (String)

    file within the machine

  • preserve (Boolean) (defaults to: false)
  • mkpath (Boolean) (defaults to: false)

Returns:



447
448
449
450
451
# File 'lib/osvm/machine.rb', line 447

def push_file(src, dst, preserve: false, mkpath: false)
  mkdir_p(File.dirname(dst)) if mkpath
  shared_dir.push_file(src, dst)
  self
end

#qemu_command(kernel_params: []) ⇒ Object (protected)

Raises:

  • (NotImplementedError)


470
471
472
# File 'lib/osvm/machine.rb', line 470

def qemu_command(kernel_params: [])
  raise NotImplementedError, "#{self.class} must implement #qemu_command"
end

#qemu_disk_optionsObject (protected)



485
486
487
488
489
490
491
492
493
494
# File 'lib/osvm/machine.rb', line 485

def qemu_disk_options
  ret = []

  config.disks.each_with_index do |disk, i|
    ret << '-drive' << "id=disk#{i},file=#{disk_path(disk.device)},if=none,format=raw"
    ret << '-device' << "ide-hd,drive=disk#{i},bus=ahci.#{i}"
  end

  ret
end

#qemu_virtiofs_optionsObject (protected)



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/osvm/machine.rb', line 496

def qemu_virtiofs_options
  ret = []

  shared_filesystems.each_with_index do |fs, i|
    name, = fs
    ret << '-chardev' << "socket,id=char#{i},path=#{virtiofs_socket_path(name)}"
    ret << '-device' << "vhost-user-fs-pci,queue-size=1024,chardev=char#{i},tag=#{name}"
  end

  if ret.any?
    ret << '-object' << "memory-backend-file,id=m0,size=#{config.memory}M,mem-path=/dev/shm,share=on"
    ret << '-numa' << 'node,memdev=m0'
  end

  ret
end

#read_nonblock(io) ⇒ Object (protected)



704
705
706
707
708
# File 'lib/osvm/machine.rb', line 704

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

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



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/osvm/machine.rb', line 678

def read_shell_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 = shell.wait_readable(1)
    next unless rs

    begin
      buffer << read_nonblock(shell)
    rescue EOFError
      @shell_up = false
      raise MachineShellClosed
    end

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

  buffer
end

#run_console_threadObject (protected)



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/osvm/machine.rb', line 581

def run_console_thread
  @console_output = ''

  @console_thread = Thread.new do
    console_log = File.open(console_log_path, 'w')

    begin
      loop do
        rs = qemu_read.wait_readable
        next unless rs

        data = read_nonblock(qemu_read)
        @console_output << data

        console_log.write(data)
        console_log.flush
      end
    rescue EOFError
      console_log.close
    rescue IOError
      # pass
    end
  end
end

#run_qemu_reaper(pid) ⇒ Object (protected)



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/osvm/machine.rb', line 545

def run_qemu_reaper(pid)
  @qemu_reaper = Thread.new do
    Process.wait(pid)
    log.exit($?.exitstatus)

    @qemu_pid = nil

    if @qemu_read
      @qemu_read.close
      @qemu_read = nil
    end

    if @console_thread
      console_thread.join
      @console_thread = nil
    end

    shell_server.close
    @shell_server = nil

    if shell
      shell.close
      @shell = nil
    end

    stop_virtiofs

    cleanup

    @qemu_reaper = nil
    @shell_up = false
    @running = false
    @stopped_at = Time.now
  end
end

#running?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/osvm/machine.rb', line 214

def running?
  @running
end

#service_check_command(_name) ⇒ Object (protected)



474
475
476
# File 'lib/osvm/machine.rb', line 474

def service_check_command(_name)
  nil
end

#shell_socket_pathObject (protected)



643
644
645
# File 'lib/osvm/machine.rb', line 643

def shell_socket_path
  socket_path("#{name}-shell.sock")
end

#shell_up?Boolean (protected)

Returns:

  • (Boolean)


674
675
676
# File 'lib/osvm/machine.rb', line 674

def shell_up?
  @shell_up
end

#socket_path(socket) ⇒ Object (protected)



669
670
671
672
# File 'lib/osvm/machine.rb', line 669

def socket_path(socket)
  @socket_hash ||= Digest::SHA256.hexdigest([hash_base, name].join)[0..7]
  File.join(sockdir, "#{@socket_hash}-#{socket}")
end

#start(kernel_params: []) ⇒ Machine

Start the machine

Parameters:

  • kernel_params (Array<String>) (defaults to: [])

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osvm/machine.rb', line 53

def start(kernel_params: [])
  raise 'Machine already started' if running?

  # virtiofsd cannot be relaunched right away, it needs some time settle
  # for unknown reasons, so we ensure there's a 5 second gap between stop
  # and start of this machine
  if @stopped_at
    diff = Time.now - @stopped_at
    delay = 5
    sleep([delay - diff, delay].min) if diff <= delay
  end

  log.start
  prepare_disks

  # Clear-out left-over socket
  begin
    File.unlink(shell_socket_path)
  rescue Errno::ENOENT
    # ignore
  end

  @shell_server = UNIXServer.new(shell_socket_path)

  shared_dir.setup
  start_virtiofs
  sleep(1)

  qemu_kwargs = {}

  unless @interactive_console
    @qemu_read, w = IO.pipe

    qemu_kwargs = {
      in: :close,
      out: w,
      err: w
    }
  end

  @start_kernel_params = kernel_params

  @qemu_pid = Process.spawn(
    *qemu_command(kernel_params:),
    **qemu_kwargs
  )
  w.close unless @interactive_console
  run_qemu_reaper(qemu_pid)

  @running = true

  run_console_thread unless @interactive_console

  @shell = @shell_server.accept
  self
end

#start_virtiofsObject (protected)



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/osvm/machine.rb', line 513

def start_virtiofs
  shared_filesystems.each do |name, path|
    f = File.open(virtiofs_log_path(name), 'w')

    virtiofsd_pids << Process.spawn(
      File.join(config.virtiofsd, 'bin/virtiofsd'),
      '--socket-path', virtiofs_socket_path(name),
      '--shared-dir', path,
      '--cache', 'never',
      in: :close,
      out: f,
      err: f
    )

    f.close
  end
end

#stop(timeout: @default_timeout) ⇒ Machine

Stop the machine

Parameters:

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

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/osvm/machine.rb', line 119

def stop(timeout: @default_timeout)
  log.stop
  begin
    execute('poweroff -f')
  rescue MachineShellClosed
    log.execute_end(-1, '[machine shell closed]')
  end

  if qemu_reaper && qemu_reaper.join(timeout).nil?
    raise UnrecoverableTimeoutError, "Timeout while stopping machine #{name}"
  end

  self
end

#stop_virtiofsObject (protected)



531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/osvm/machine.rb', line 531

def stop_virtiofs
  virtiofsd_pids.delete_if do |pid|
    Process.kill('TERM', pid)
    false
  rescue Errno::ESRCH
    true
  end

  virtiofsd_pids.delete_if do |pid|
    Process.wait(pid)
    true
  end
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>)


287
288
289
290
291
292
293
294
295
# File 'lib/osvm/machine.rb', line 287

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

#virtiofs_log_path(mount_name) ⇒ Object (protected)



665
666
667
# File 'lib/osvm/machine.rb', line 665

def virtiofs_log_path(mount_name)
  File.join(tmpdir, "#{name}-fs-#{mount_name}.log")
end

#virtiofs_socket_path(mount_name) ⇒ Object (protected)



661
662
663
# File 'lib/osvm/machine.rb', line 661

def virtiofs_socket_path(mount_name)
  socket_path("#{name}-fs-#{mount_name}.sock")
end

#wait_for_boot(timeout: @default_timeout) ⇒ Object

Wait until the system has booted

Parameters:

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


230
231
232
# File 'lib/osvm/machine.rb', line 230

def wait_for_boot(timeout: @default_timeout)
  wait_for_shell(timeout:)
end

#wait_for_console_text(regex, timeout: @default_timeout) ⇒ Machine

Wait for text to appear in console output

Parameters:

  • regex (Regexp)

Returns:



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/osvm/machine.rb', line 397

def wait_for_console_text(regex, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  log.console_wait_begin(regex)

  loop do
    if regex =~ @console_output
      log.console_wait_end(true)
      return self
    end

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      log.console_wait_end(false, 'timeout')
      raise TimeoutError, "Timeout occurred while waiting for #{regex.inspect} on the console"
    elsif !running?
      log.console_wait_end(false, 'machine not running')
      raise Error, 'Machine is not running'
    end

    sleep(1)
  end

  self
end

#wait_for_service(name) ⇒ Machine

Wait for a system service to start

Parameters:

  • name (String)

Returns:

Raises:

  • (NotImplementedError)


386
387
388
389
390
391
392
# File 'lib/osvm/machine.rb', line 386

def wait_for_service(name)
  cmd = service_check_command(name)
  raise NotImplementedError, "#{self.class} does not implement service checks" if cmd.nil?

  wait_until_succeeds(cmd)
  self
end

#wait_for_shell(timeout: @default_timeout) ⇒ Object (protected)



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/osvm/machine.rb', line 614

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

  t1 = Time.now
  buffer = ''

  loop do
    if t1 + timeout < Time.now
      raise TimeoutError, 'Timeout occurred while waiting for shell'
    end

    rs = shell.wait_readable(1)
    next unless rs

    begin
      buffer << read_nonblock(shell)
    rescue EOFError
      raise MachineShellClosed
    end

    next unless buffer.include?("test-shell-ready\n")

    @shell_up = true
    shared_dir.mount
    return
  end
end

#wait_for_shutdown(timeout: @default_timeout) ⇒ Machine

Wait until the machine shuts down

Parameters:

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

Returns:



369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/osvm/machine.rb', line 369

def wait_for_shutdown(timeout: @default_timeout)
  t1 = Time.now

  loop do
    return self unless running?

    if t1 + timeout < Time.now
      raise TimeoutError, 'Timeout occurred while waiting for shutdown'
    end

    sleep(1)
  end
end

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

Wait until command fails

Returns:

  • (Array<Integer, String>)


344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/osvm/machine.rb', line 344

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_online(timeout: @default_timeout) ⇒ Machine

Wait until network is operational, including DNS

Returns:



361
362
363
364
# File 'lib/osvm/machine.rb', line 361

def wait_until_online(timeout: @default_timeout)
  wait_until_succeeds('curl --head https://check-online.vpsadminos.org', timeout:)
  self
end

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

Wait until command succeeds

Returns:

  • (Array<Integer, String>)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/osvm/machine.rb', line 327

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