Class: OsVm::Machine

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

Direct Known Subclasses

NixosMachine, VpsadminosMachine

Constant Summary collapse

SHELL_INDEX_KEY =
:osvm_machine_shell_index

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, tmpdir, sockdir, default_timeout: 900, 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: 900)
  • hash_base (String) (defaults to: '')
  • interactive_console (Boolean) (defaults to: false)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osvm/machine.rb', line 30

def initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '', interactive_console: false)
  @name = name
  @config = config
  @tmpdir = tmpdir
  @sockdir = sockdir
  @default_timeout = default_timeout || 900
  @hash_base = hash_base
  @interactive_console = interactive_console
  @start_kernel_params = []
  @running = 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
  @start_mutex = Mutex.new
  @shared_dir_mutex = Mutex.new
  @shared_dir_mounted = false

  FileUtils.mkdir_p(tmpdir)
  FileUtils.mkdir_p(sockdir)
  @log = MachineLog.new(File.join(tmpdir, "#{name}-log.log"))
  named_shells = {}
  worker_shell_count = config.test_shells - config.shell_names.length
  @shell_instances = Array.new(config.test_shells) do |i|
    shell_name = i >= worker_shell_count ? config.shell_names[i - worker_shell_count] : nil
    shell = Shell.new(self, i, shell_socket_path(i), shell_log_path(i), default_timeout:, name: shell_name)
    named_shells[shell_name] = shell if shell_name
    shell
  end
  @shell_collection = ShellCollection.new(self, named_shells)
end

Instance Attribute Details

#configObject (readonly, protected)

Returns the value of attribute config.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def config
  @config
end

#console_threadObject (readonly, protected)

Returns the value of attribute console_thread.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def console_thread
  @console_thread
end

#hash_baseObject (readonly, protected)

Returns the value of attribute hash_base.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def hash_base
  @hash_base
end

#logObject (readonly, protected)

Returns the value of attribute log.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def log
  @log
end

#nameString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/osvm/machine.rb', line 17

def name
  @name
end

#qemu_pidObject (readonly, protected)

Returns the value of attribute qemu_pid.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def qemu_pid
  @qemu_pid
end

#qemu_readObject (readonly, protected)

Returns the value of attribute qemu_read.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def qemu_read
  @qemu_read
end

#qemu_reaperObject (readonly, protected)

Returns the value of attribute qemu_reaper.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def qemu_reaper
  @qemu_reaper
end

#shared_dirObject (readonly, protected)

Returns the value of attribute shared_dir.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def shared_dir
  @shared_dir
end

#shared_filesystemsObject (readonly, protected)

Returns the value of attribute shared_filesystems.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def shared_filesystems
  @shared_filesystems
end

#shell_instancesObject (readonly, protected)

Returns the value of attribute shell_instances.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def shell_instances
  @shell_instances
end

#sockdirObject (readonly, protected)

Returns the value of attribute sockdir.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def sockdir
  @sockdir
end

#start_kernel_paramsArray<String> (readonly)

Kernel parameters passed to #start

Returns:

  • (Array<String>)


21
22
23
# File 'lib/osvm/machine.rb', line 21

def start_kernel_params
  @start_kernel_params
end

#tmpdirObject (readonly, protected)

Returns the value of attribute tmpdir.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def tmpdir
  @tmpdir
end

#virtiofsd_pidsObject (readonly, protected)

Returns the value of attribute virtiofsd_pids.



422
423
424
# File 'lib/osvm/machine.rb', line 422

def virtiofsd_pids
  @virtiofsd_pids
end

Class Method Details

.with_shell(index) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/osvm/machine.rb', line 8

def self.with_shell(index)
  original_index = Thread.current[SHELL_INDEX_KEY]
  Thread.current[SHELL_INDEX_KEY] = index
  yield
ensure
  Thread.current[SHELL_INDEX_KEY] = original_index
end

Instance Method Details

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

Execute all commands and check that they all fail

Parameters:

  • cmds (String)

Returns:

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


288
289
290
# File 'lib/osvm/machine.rb', line 288

def all_fail(*cmds, shell: nil)
  command_shell(shell).all_fail(*cmds)
end

#all_succeed(*cmds, shell: nil) ⇒ Array<Array<[Integer, String]>>

Execute all commands and check that they all succeed

Parameters:

  • cmds (String)

Returns:

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


281
282
283
# File 'lib/osvm/machine.rb', line 281

def all_succeed(*cmds, shell: nil)
  command_shell(shell).all_succeed(*cmds)
end

#base_kernel_params(kernel_params) ⇒ Object (protected)



438
439
440
441
442
443
# File 'lib/osvm/machine.rb', line 438

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

#booted?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/osvm/machine.rb', line 238

def booted?
  current_shell.up?
end

#can_execute?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/osvm/machine.rb', line 243

def can_execute?
  shell_instances.any?(&:up?)
end

#cleanupMachine

Cleanup machine state

Returns:



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/osvm/machine.rb', line 220

def cleanup
  shell_instances.each(&:cleanup)

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

  self
end

#command_shell(name) ⇒ Object (protected)



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

def command_shell(name)
  name.nil? ? current_shell : shells.fetch(name)
end

#console_log_pathObject (protected)



627
628
629
# File 'lib/osvm/machine.rb', line 627

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

#console_outputString

Return text captured from the machine's console so far

Returns:

  • (String)


341
342
343
# File 'lib/osvm/machine.rb', line 341

def console_output
  @mutex.synchronize { (@console_output || '').dup }
end

#current_shellObject (protected)



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

def current_shell
  shell_for(current_shell_index)
end

#current_shell_indexObject (protected)



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

def current_shell_index
  index = Thread.current[SHELL_INDEX_KEY] || 0
  validate_shell_index(index)
  index
end

#custom_qemu_numa_memory_backend?Boolean (protected)

Returns:

  • (Boolean)


503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/osvm/machine.rb', line 503

def custom_qemu_numa_memory_backend?
  opts = config.extra_qemu_options
  has_memory_backend = false
  has_numa_memdev = false

  opts.each_cons(2) do |arg, value|
    if arg == '-object' && value.start_with?('memory-backend-')
      has_memory_backend = true
    elsif arg == '-numa' && value.start_with?('node,') && value.include?('memdev=')
      has_numa_memdev = true
    end
  end

  has_memory_backend && has_numa_memdev
end

#destroyMachine

Destroy the machine

Returns:



195
196
197
198
199
200
# File 'lib/osvm/machine.rb', line 195

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:



208
209
210
211
212
213
214
215
216
# File 'lib/osvm/machine.rb', line 208

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)



631
632
633
634
635
636
637
638
639
# File 'lib/osvm/machine.rb', line 631

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, shell: nil) ⇒ Array<Integer, String>

Execute a command

Parameters:

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

Returns:

  • (Array<Integer, String>)

    exit status and output

Raises:



258
259
260
# File 'lib/osvm/machine.rb', line 258

def execute(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).execute(cmd, timeout:)
end

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

Execute command and check that it fails

Parameters:

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

Returns:

  • (Array<Integer, String>)


274
275
276
# File 'lib/osvm/machine.rb', line 274

def fails(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).fails(cmd, timeout:)
end

#finalizeObject



64
65
66
67
# File 'lib/osvm/machine.rb', line 64

def finalize
  log.close
  shell_instances.each(&:finalize)
end

#inspectObject



416
417
418
# File 'lib/osvm/machine.rb', line 416

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

#join(timeout: @default_timeout) ⇒ Object

Block until the machine stops



134
135
136
137
# File 'lib/osvm/machine.rb', line 134

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:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/osvm/machine.rb', line 160

def kill(signal: 'TERM')
  unless running?
    log.kill('NONE')
    return self
  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:



379
380
381
382
# File 'lib/osvm/machine.rb', line 379

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:



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

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

#mount_shared_dir_onceObject (protected)



679
680
681
682
683
684
685
686
687
688
# File 'lib/osvm/machine.rb', line 679

def mount_shared_dir_once
  return if @shared_dir_mounted

  @shared_dir_mutex.synchronize do
    return if @shared_dir_mounted

    shared_dir.mount
    @shared_dir_mounted = true
  end
end

#poweroff_commandObject (protected)



434
435
436
# File 'lib/osvm/machine.rb', line 434

def poweroff_command
  'poweroff -f'
end

#prepare_disksObject (protected)



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

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



407
408
409
# File 'lib/osvm/machine.rb', line 407

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:



398
399
400
401
402
# File 'lib/osvm/machine.rb', line 398

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

#qemu_boot_media_optionsObject (protected)



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

def qemu_boot_media_options
  return [] if config.iso.nil?

  ['-cdrom', config.iso]
end

#qemu_boot_options(kernel_params) ⇒ Object (protected)



445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/osvm/machine.rb', line 445

def qemu_boot_options(kernel_params)
  if config.boot_mode == 'direct'
    [
      '-kernel', config.kernel,
      '-initrd', config.initrd,
      '-append', base_kernel_params(kernel_params).join(' ')
    ]
  else
    ret = []
    ret += ['-boot', "order=#{config.boot_order}"] if config.boot_order
    ret
  end
end

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

Raises:

  • (NotImplementedError)


426
427
428
# File 'lib/osvm/machine.rb', line 426

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

#qemu_disk_optionsObject (protected)



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

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_shell_optionsObject (protected)



465
466
467
468
469
470
471
472
473
# File 'lib/osvm/machine.rb', line 465

def qemu_shell_options
  ret = ['-device', 'virtio-serial']

  shell_instances.each do |shell|
    ret += shell.qemu_options
  end

  ret
end

#qemu_virtiofs_optionsObject (protected)



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/osvm/machine.rb', line 486

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? && !custom_qemu_numa_memory_backend?
    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)



690
691
692
693
694
# File 'lib/osvm/machine.rb', line 690

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

#run_console_threadObject (protected)



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

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)
        @mutex.synchronize { @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)



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

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_instances.each(&:close)

    stop_virtiofs

    cleanup

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

#running?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/osvm/machine.rb', line 233

def running?
  @running
end

#service_check_command(_name) ⇒ Object (protected)



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

def service_check_command(_name)
  nil
end

#shell_chardev_id(index) ⇒ Object (protected)



613
614
615
# File 'lib/osvm/machine.rb', line 613

def shell_chardev_id(index)
  shell_for(index).chardev_id
end

#shell_for(index) ⇒ Object (protected)



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

def shell_for(index)
  validate_shell_index(index)
  @shell_instances[index]
end

#shell_log_path(index = 0) ⇒ Object (protected)



622
623
624
625
# File 'lib/osvm/machine.rb', line 622

def shell_log_path(index = 0)
  suffix = index == 0 ? 'shell' : "shell#{index}"
  File.join(tmpdir, "#{name}-#{suffix}.log")
end

#shell_socket_path(index = 0) ⇒ Object (protected)



617
618
619
620
# File 'lib/osvm/machine.rb', line 617

def shell_socket_path(index = 0)
  suffix = index == 0 ? 'shell' : "shell#{index}"
  socket_path("#{name}-#{suffix}.sock")
end

#shellsShellCollection

Returns:



412
413
414
# File 'lib/osvm/machine.rb', line 412

def shells
  @shell_collection
end

#socket_path(socket) ⇒ Object (protected)



649
650
651
652
# File 'lib/osvm/machine.rb', line 649

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: [], wait_for_boot: false) ⇒ Machine

Start the machine

Parameters:

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

Returns:



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osvm/machine.rb', line 73

def start(kernel_params: [], wait_for_boot: false)
  @start_mutex.synchronize do
    if running?
      unless start_kernel_params == kernel_params
        raise 'Machine already started with different kernel parameters'
      end

      self.wait_for_boot if wait_for_boot
      return self
    end

    # 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

    shell_instances.each(&:prepare)

    shared_dir.setup
    @shared_dir_mounted = false
    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

    self.wait_for_boot if wait_for_boot
    self
  end
end

#start_virtiofsObject (protected)



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/osvm/machine.rb', line 519

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', 'auto',
      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:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/osvm/machine.rb', line 142

def stop(timeout: @default_timeout)
  log.stop
  begin
    execute(poweroff_command)
  rescue MachineShellClosed
    # The shell logs the failed command.
  end

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

  self
end

#stop_virtiofsObject (protected)



537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/osvm/machine.rb', line 537

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, shell: nil) ⇒ Array<Integer, String>

Execute command and check that it succeeds

Parameters:

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

Returns:

  • (Array<Integer, String>)


266
267
268
# File 'lib/osvm/machine.rb', line 266

def succeeds(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).succeeds(cmd, timeout:)
end

#validate_shell_index(index) ⇒ Object (protected)

Raises:

  • (ArgumentError)


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

def validate_shell_index(index)
  return if index.is_a?(Integer) && index >= 0 && index < config.test_shells

  raise ArgumentError, "invalid shell index #{index.inspect}"
end

#virtiofs_log_path(mount_name) ⇒ Object (protected)



645
646
647
# File 'lib/osvm/machine.rb', line 645

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

#virtiofs_socket_path(mount_name) ⇒ Object (protected)



641
642
643
# File 'lib/osvm/machine.rb', line 641

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)


249
250
251
# File 'lib/osvm/machine.rb', line 249

def wait_for_boot(timeout: @default_timeout)
  current_shell.wait(timeout:)
end

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

Wait for text to appear in console output

Parameters:

  • regex (Regexp)

Returns:



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

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

  log_started_at = log.console_wait_begin(regex)

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

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      log.console_wait_end(false, 'timeout', log_started_at)
      raise TimeoutError, "Timeout occurred while waiting for #{regex.inspect} on the console"
    elsif !running?
      log.console_wait_end(false, 'machine not running', log_started_at)
      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)


331
332
333
334
335
336
337
# File 'lib/osvm/machine.rb', line 331

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

Wait until the machine shuts down

Parameters:

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

Returns:



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/osvm/machine.rb', line 314

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, shell: nil) ⇒ Array<Integer, String>

Wait until command fails

Returns:

  • (Array<Integer, String>)


300
301
302
# File 'lib/osvm/machine.rb', line 300

def wait_until_fails(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).wait_until_fails(cmd, timeout:)
end

#wait_until_online(timeout: @default_timeout) ⇒ Machine

Wait until network is operational, including DNS

Returns:



306
307
308
309
# File 'lib/osvm/machine.rb', line 306

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, shell: nil) ⇒ Array<Integer, String>

Wait until command succeeds

Returns:

  • (Array<Integer, String>)


294
295
296
# File 'lib/osvm/machine.rb', line 294

def wait_until_succeeds(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).wait_until_succeeds(cmd, timeout:)
end