Class: OsVm::Machine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '') ⇒ Machine

Returns a new instance of Machine.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/osvm/machine.rb', line 10

def initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '')
  @name = name
  @config = config
  @tmpdir = tmpdir
  @sockdir = sockdir
  @default_timeout = default_timeout || 900
  @hash_base = hash_base
  @running = false
  @shell_up = false
  @shared_dir = SharedDir.new(self)
  @shared_filesystems = {
    shared_dir.fs_name => shared_dir.host_path,
  }.merge(config.fetch(:shared_filesystems, {}))
  @can_use_virtiofs = Process.uid == 0
  @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.



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

def config
  @config
end

#console_threadObject (readonly, protected)

Returns the value of attribute console_thread.



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

def console_thread
  @console_thread
end

#hash_baseObject (readonly, protected)

Returns the value of attribute hash_base.



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

def hash_base
  @hash_base
end

#logObject (readonly, protected)

Returns the value of attribute log.



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

def log
  @log
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#qemu_pidObject (readonly, protected)

Returns the value of attribute qemu_pid.



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

def qemu_pid
  @qemu_pid
end

#qemu_readObject (readonly, protected)

Returns the value of attribute qemu_read.



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

def qemu_read
  @qemu_read
end

#qemu_reaperObject (readonly, protected)

Returns the value of attribute qemu_reaper.



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

def qemu_reaper
  @qemu_reaper
end

#shared_dirObject (readonly, protected)

Returns the value of attribute shared_dir.



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

def shared_dir
  @shared_dir
end

#shared_filesystemsObject (readonly, protected)

Returns the value of attribute shared_filesystems.



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

def shared_filesystems
  @shared_filesystems
end

#shellObject (readonly, protected)

Returns the value of attribute shell.



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

def shell
  @shell
end

#shell_serverObject (readonly, protected)

Returns the value of attribute shell_server.



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

def shell_server
  @shell_server
end

#sockdirObject (readonly, protected)

Returns the value of attribute sockdir.



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

def sockdir
  @sockdir
end

#tmpdirObject (readonly, protected)

Returns the value of attribute tmpdir.



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

def tmpdir
  @tmpdir
end

#virtiofsd_pidsObject (readonly, protected)

Returns the value of attribute virtiofsd_pids.



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

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


250
251
252
253
254
255
256
257
258
# File 'lib/osvm/machine.rb', line 250

def all_fail(*cmds)
  ret = []

  cmds.each do |cmd|
    ret << fails(cmd)
  end

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


237
238
239
240
241
242
243
244
245
# File 'lib/osvm/machine.rb', line 237

def all_succeed(*cmds)
  ret = []

  cmds.each do |cmd|
    ret << succeeds(cmd)
  end

  ret
end

#booted?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/osvm/machine.rb', line 150

def booted?
  shell_up?
end

#can_use_virtiofs?Boolean (protected)

Returns:

  • (Boolean)


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

def can_use_virtiofs?
  @can_use_virtiofs
end

#cleanupMachine

Cleanup machine state

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/osvm/machine.rb', line 128

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

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

  self
end

#console_log_pathObject (protected)



605
606
607
# File 'lib/osvm/machine.rb', line 605

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

#destroyMachine

Destroy the machine

Returns:



119
120
121
122
123
124
# File 'lib/osvm/machine.rb', line 119

def destroy
  log.destroy
  shared_dir.destroy
  destroy_disks
  self
end

#destroy_disksObject (protected)



561
562
563
564
565
566
567
568
# File 'lib/osvm/machine.rb', line 561

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

    path = disk_path(disk[:device])
    File.unlink(path) if File.exist?(path)
  end
end

#disk_path(path) ⇒ Object (protected)



609
610
611
612
613
614
615
# File 'lib/osvm/machine.rb', line 609

def disk_path(path)
  if path.start_with?('/')
    path
  else
    File.join(tmpdir, path)
  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



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
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/osvm/machine.rb', line 164

def execute(cmd, timeout: @default_timeout)
  start unless running?
  wait_for_shell
  t1 = Time.now

  # It is a bit of a mystery why this write is needed. The shell just
  # sometimes swallows the first character, which would be a '(', and then
  # it complains about a syntax error. So we first write a character that
  # it can harmlessly swallow.
  shell.write("\n")

  shell.write("( #{cmd} ); echo '|!=EOF' $?\n")
  log.execute_begin(cmd)
  rx = /(.*)\|\!=EOF\s+(\d+)/m
  buffer = ''

  loop do
    if t1 + timeout < Time.now
      log.execute_end(-1, buffer)
      raise TimeoutError, "Timeout occured while running command '#{cmd}'"
    end

    rs, _ = IO.select([shell], [], [], 1)
    next if rs.nil?

    rs.each do |io|
      case io
      when shell
        buffer << read_nonblock(shell)
      end
    end

    if rx =~ buffer
      status = $2.to_i
      output = $1.strip

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


224
225
226
227
228
229
230
231
232
# File 'lib/osvm/machine.rb', line 224

def fails(cmd, timeout: @default_timeout)
  status, output = execute(cmd, timeout: timeout)

  if status == 0
    raise CommandSucceeded, "Command '#{cmd}' succeeds with status #{status}. Output:\n #{output}"
  end

  return [status, output]
end

#finalizeObject



32
33
34
# File 'lib/osvm/machine.rb', line 32

def finalize
  log.close
end

#inspectObject



402
403
404
# File 'lib/osvm/machine.rb', line 402

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

#killMachine

Kill the machine

Returns:



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

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

  log.kill('TERM')

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

  return if qemu_reaper.join(60)

  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:



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

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:



370
371
372
373
# File 'lib/osvm/machine.rb', line 370

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

#osctl_json(cmd) ⇒ Hash

osctl command without `osctl`, output is returned as JSON

Parameters:

  • cmd (String)

Returns:

  • (Hash)


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

def osctl_json(cmd)
  status, output = succeeds("osctl -j #{cmd}")
  JSON.parse(output, symbolize_names: true)
end

#prepare_disksObject (protected)



553
554
555
556
557
558
559
# File 'lib/osvm/machine.rb', line 553

def prepare_disks
  config[:disks].each do |disk|
    next if disk[:type] != 'file' || 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



394
395
396
397
398
399
400
# File 'lib/osvm/machine.rb', line 394

def pull_file(src, preserve: false)
  unless can_use_virtiofs?
    fail "#{$0} must be run as root for pull_file() to work"
  end

  shared_dir.pull_file(src, preserve: 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:



381
382
383
384
385
386
387
388
389
# File 'lib/osvm/machine.rb', line 381

def push_file(src, dst, preserve: false, mkpath: false)
  unless can_use_virtiofs?
    fail "#{$0} must be run as root for push_file() to work"
  end

  mkdir_p(File.dirname(dst)) if mkpath
  shared_dir.push_file(src, dst)
  self
end

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



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/osvm/machine.rb', line 411

def qemu_command(kernel_params: [])
  all_kernel_params = [
    "console=ttyS0",
    "init=#{config[:toplevel]}/init",
  ] + config[:kernelParams] + kernel_params

  [
    "#{config[:qemu]}/bin/qemu-kvm",
    "-name", "os-vm-#{name}",
    "-m", "#{config[:memory]}",
    "-cpu", "host",
    "-smp", "cpus=#{config[:cpus]},cores=#{config[:cpu][:cores]},threads=#{config[:cpu][:threads]},sockets=#{config[:cpu][:sockets]}",
    "--no-reboot",
    "-device", "ahci,id=ahci",
    "-device", "virtio-net,netdev=net0",
    "-netdev", "user,id=net0,net=10.0.2.0/24,host=10.0.2.2,dns=10.0.2.3",
    "-drive", "index=0,id=drive1,file=#{config[:squashfs]},readonly=on,media=cdrom,format=raw,if=virtio",
    "-chardev", "socket,id=shell,path=#{shell_socket_path}",
    "-device", "virtio-serial",
    "-device", "virtconsole,chardev=shell",
    "-kernel", config[:kernel],
    "-initrd", config[:initrd],
    "-append", "#{all_kernel_params.join(' ')}",
    "-nographic",
  ] + qemu_disk_options + qemu_virtiofs_options
end

#qemu_disk_optionsObject (protected)



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

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)



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/osvm/machine.rb', line 449

def qemu_virtiofs_options
  ret = []
  return ret unless can_use_virtiofs?

  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)



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

def read_nonblock(io)
  io.read_nonblock(4096)

rescue IO::WaitReadable
  ''
end

#run_console_threadObject (protected)



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/osvm/machine.rb', line 531

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

    begin
      loop do
        rs, _ = IO.select([qemu_read])

        rs.each do |io|
          case io
          when qemu_read
            console_log.write(read_nonblock(qemu_read))
            console_log.flush
          end
        end
      end
    rescue EOFError
      console_log.close
    end
  end
end

#run_qemu_reaper(pid) ⇒ Object (protected)



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/osvm/machine.rb', line 501

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

    @qemu_pid = nil
    @qemu_read.close
    @qemu_read = nil

    console_thread.join
    @console_thread = nil

    shell_server.close
    @shell_server = nil

    if shell
      shell.close
      @shell = nil
    end

    stop_virtiofs

    cleanup

    @qemu_reaper = nil
    @shell_up = false
    @running = false
  end
end

#running?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/osvm/machine.rb', line 145

def running?
  @running
end

#shell_socket_pathObject (protected)



601
602
603
# File 'lib/osvm/machine.rb', line 601

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

#shell_up?Boolean (protected)

Returns:

  • (Boolean)


634
635
636
# File 'lib/osvm/machine.rb', line 634

def shell_up?
  @shell_up
end

#socket_path(socket) ⇒ Object (protected)



629
630
631
632
# File 'lib/osvm/machine.rb', line 629

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:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osvm/machine.rb', line 39

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

  log.start
  prepare_disks

  @shell_server = UNIXServer.new(shell_socket_path)

  if can_use_virtiofs?
    shared_dir.setup
    start_virtiofs
    sleep(1)
  end

  @qemu_read, w = IO.pipe
  @qemu_pid = Process.spawn(
    *qemu_command(kernel_params: kernel_params),
    in: :close,
    out: w,
    err: w,
  )
  w.close
  run_qemu_reaper(qemu_pid)

  @running = true

  run_console_thread

  @shell = @shell_server.accept
  self
end

#start_virtiofsObject (protected)



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/osvm/machine.rb', line 467

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

    virtiofsd_pids << Process.spawn(
      File.join(config[:qemu], 'libexec/virtiofsd'),
      "--socket-path=#{virtiofs_socket_path(name)}",
      "-o", "source=#{path}",
      "-o", "cache=none",
      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:



76
77
78
79
80
81
82
83
84
85
# File 'lib/osvm/machine.rb', line 76

def stop(timeout: @default_timeout)
  log.stop
  execute('poweroff -f')

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

  self
end

#stop_virtiofsObject (protected)



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

def stop_virtiofs
  virtiofsd_pids.delete_if do |pid|
    begin
      Process.kill('TERM', pid)
      false
    rescue Errno::ESRCH
      true
    end
  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>)


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

def succeeds(cmd, timeout: @default_timeout)
  status, output = execute(cmd, timeout: timeout)

  if status != 0
    raise CommandFailed, "Command '#{cmd}' failed with status #{status}. Output:\n #{output}"
  end

  return [status, output]
end

#virtiofs_log_path(mount_name) ⇒ Object (protected)



625
626
627
# File 'lib/osvm/machine.rb', line 625

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

#virtiofs_socket_path(mount_name) ⇒ Object (protected)



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

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)


156
157
158
# File 'lib/osvm/machine.rb', line 156

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

#wait_for_osctl_pool(name, timeout: @default_timeout) ⇒ Machine

Wait for pool to be imported into osctld

Parameters:

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

Returns:



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

def wait_for_osctl_pool(name, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    status, output = wait_until_succeeds(
      "osctl pool show -H -o state #{name}",
      timeout: cur_timeout,
    )

    return self if output == 'active'

    cur_timeout = timeout - (Time.now - t1)
  end
end

#wait_for_service(name) ⇒ Machine

Wait for runit system service to start

Parameters:

  • name (String)

Returns:



317
318
319
320
# File 'lib/osvm/machine.rb', line 317

def wait_for_service(name)
  wait_until_succeeds("sv check #{name}")
  self
end

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



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/osvm/machine.rb', line 570

def wait_for_shell(timeout: @default_timeout)
  fail "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 occured while waiting for shell"
    end

    rs, _ = IO.select([shell], [], [], 1)
    next if rs.nil?

    rs.each do |io|
      case io
      when shell
        buffer << read_nonblock(shell)
      end
    end

    if buffer.include?("test-shell-ready\r\n")
      @shell_up = true
      succeeds("stty -F /dev/hvc0 -echo")
      shared_dir.mount if can_use_virtiofs?
      return
    end
  end
end

#wait_for_shutdown(timeout: @default_timeout) ⇒ Machine

Wait until the machine shuts down

Parameters:

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

Returns:



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

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

  loop do
    return self unless running?

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

    sleep(1)
  end
end

#wait_for_zpool(name, timeout: @default_timeout) ⇒ Machine

Wait for zpool

Parameters:

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

Returns:



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

def wait_for_zpool(name, timeout: @default_timeout)
  wait_until_succeeds("zpool list #{name}", timeout: timeout)
  self
end

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

Wait until command fails

Returns:

  • (Array<Integer, String>)


277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/osvm/machine.rb', line 277

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)
    sleep(1)
  end
end

#wait_until_online(timeout: @default_timeout) ⇒ Machine

Wait until network is operational, including DNS

Returns:



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

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

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

Wait until command succeeds

Returns:

  • (Array<Integer, String>)


262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/osvm/machine.rb', line 262

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)
    sleep(1)
  end
end