Class: OsVm::Machine
- Inherits:
-
Object
- Object
- OsVm::Machine
- Defined in:
- lib/osvm/machine.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
protected
Returns the value of attribute config.
-
#console_thread ⇒ Object
readonly
protected
Returns the value of attribute console_thread.
-
#hash_base ⇒ Object
readonly
protected
Returns the value of attribute hash_base.
-
#log ⇒ Object
readonly
protected
Returns the value of attribute log.
- #name ⇒ String readonly
-
#qemu_pid ⇒ Object
readonly
protected
Returns the value of attribute qemu_pid.
-
#qemu_read ⇒ Object
readonly
protected
Returns the value of attribute qemu_read.
-
#qemu_reaper ⇒ Object
readonly
protected
Returns the value of attribute qemu_reaper.
-
#shared_dir ⇒ Object
readonly
protected
Returns the value of attribute shared_dir.
-
#shared_filesystems ⇒ Object
readonly
protected
Returns the value of attribute shared_filesystems.
-
#shell ⇒ Object
readonly
protected
Returns the value of attribute shell.
-
#shell_server ⇒ Object
readonly
protected
Returns the value of attribute shell_server.
-
#sockdir ⇒ Object
readonly
protected
Returns the value of attribute sockdir.
-
#tmpdir ⇒ Object
readonly
protected
Returns the value of attribute tmpdir.
-
#virtiofsd_pids ⇒ Object
readonly
protected
Returns the value of attribute virtiofsd_pids.
Instance Method Summary collapse
-
#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.
- #booted? ⇒ Boolean
-
#cleanup ⇒ Machine
Cleanup machine state.
- #console_log_path ⇒ Object protected
-
#destroy ⇒ Machine
Destroy the machine.
-
#destroy_disks ⇒ Machine
Destroy file-backed disks.
- #disk_path(path) ⇒ Object protected
-
#execute(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute a command.
-
#fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it fails.
- #finalize ⇒ Object
-
#initialize(name, config, tmpdir, sockdir, default_timeout: 600, hash_base: '', interactive_console: false) ⇒ Machine
constructor
A new instance of Machine.
- #inspect ⇒ Object
-
#join(timeout: @default_timeout) ⇒ Object
Block until the machine stops.
-
#kill ⇒ Machine
Kill the machine.
-
#mkdir(path) ⇒ Machine
Create a directory inside the machine.
-
#mkdir_p(path) ⇒ Machine
Create a directory inside the machine.
-
#osctl_json(cmd) ⇒ Hash
osctl command without ‘osctl`, output is returned as JSON.
- #prepare_disks ⇒ Object protected
-
#pull_file(src, preserve: false) ⇒ String
Pull file from the machine to the host.
-
#push_file(src, dst, preserve: false, mkpath: false) ⇒ Machine
Push file from the host to the machine.
- #qemu_command(kernel_params: []) ⇒ Object protected
- #qemu_disk_options ⇒ Object protected
- #qemu_virtiofs_options ⇒ Object protected
- #read_nonblock(io) ⇒ Object protected
- #read_shell_output(timeout:, command:) ⇒ Object protected
- #run_console_thread ⇒ Object protected
- #run_qemu_reaper(pid) ⇒ Object protected
- #running? ⇒ Boolean
- #shell_socket_path ⇒ Object protected
- #shell_up? ⇒ Boolean protected
- #socket_path(socket) ⇒ Object protected
-
#start(kernel_params: []) ⇒ Machine
Start the machine.
- #start_virtiofs ⇒ Object protected
-
#stop(timeout: @default_timeout) ⇒ Machine
Stop the machine.
- #stop_virtiofs ⇒ Object protected
-
#succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Execute command and check that it succeeds.
- #virtiofs_log_path(mount_name) ⇒ Object protected
- #virtiofs_socket_path(mount_name) ⇒ Object protected
-
#wait_for_boot(timeout: @default_timeout) ⇒ Object
Wait until the system has booted.
-
#wait_for_osctl_pool(name, timeout: @default_timeout) ⇒ Machine
Wait for pool to be imported into osctld.
-
#wait_for_service(name) ⇒ Machine
Wait for runit system service to start.
- #wait_for_shell(timeout: @default_timeout) ⇒ Object protected
-
#wait_for_shutdown(timeout: @default_timeout) ⇒ Machine
Wait until the machine shuts down.
-
#wait_for_zpool(name, timeout: @default_timeout) ⇒ Machine
Wait for zpool.
-
#wait_until_fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command fails.
-
#wait_until_online(timeout: @default_timeout) ⇒ Machine
Wait until network is operational, including DNS.
-
#wait_until_succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command succeeds.
Constructor Details
#initialize(name, config, tmpdir, sockdir, default_timeout: 600, hash_base: '', interactive_console: false) ⇒ Machine
Returns a new instance of Machine.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/osvm/machine.rb', line 19 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 @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
#config ⇒ Object (readonly, protected)
Returns the value of attribute config.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def config @config end |
#console_thread ⇒ Object (readonly, protected)
Returns the value of attribute console_thread.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def console_thread @console_thread end |
#hash_base ⇒ Object (readonly, protected)
Returns the value of attribute hash_base.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def hash_base @hash_base end |
#log ⇒ Object (readonly, protected)
Returns the value of attribute log.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def log @log end |
#name ⇒ String (readonly)
10 11 12 |
# File 'lib/osvm/machine.rb', line 10 def name @name end |
#qemu_pid ⇒ Object (readonly, protected)
Returns the value of attribute qemu_pid.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def qemu_pid @qemu_pid end |
#qemu_read ⇒ Object (readonly, protected)
Returns the value of attribute qemu_read.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def qemu_read @qemu_read end |
#qemu_reaper ⇒ Object (readonly, protected)
Returns the value of attribute qemu_reaper.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def qemu_reaper @qemu_reaper end |
#shared_dir ⇒ Object (readonly, protected)
Returns the value of attribute shared_dir.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def shared_dir @shared_dir end |
#shared_filesystems ⇒ Object (readonly, protected)
Returns the value of attribute shared_filesystems.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def shared_filesystems @shared_filesystems end |
#shell ⇒ Object (readonly, protected)
Returns the value of attribute shell.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def shell @shell end |
#shell_server ⇒ Object (readonly, protected)
Returns the value of attribute shell_server.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def shell_server @shell_server end |
#sockdir ⇒ Object (readonly, protected)
Returns the value of attribute sockdir.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def sockdir @sockdir end |
#tmpdir ⇒ Object (readonly, protected)
Returns the value of attribute tmpdir.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 def tmpdir @tmpdir end |
#virtiofsd_pids ⇒ Object (readonly, protected)
Returns the value of attribute virtiofsd_pids.
433 434 435 |
# File 'lib/osvm/machine.rb', line 433 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
279 280 281 |
# File 'lib/osvm/machine.rb', line 279 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
272 273 274 |
# File 'lib/osvm/machine.rb', line 272 def all_succeed(*cmds) cmds.map { |cmd| succeeds(cmd) } end |
#booted? ⇒ Boolean
196 197 198 |
# File 'lib/osvm/machine.rb', line 196 def booted? shell_up? end |
#cleanup ⇒ Machine
Cleanup machine state
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/osvm/machine.rb', line 174 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_path ⇒ Object (protected)
616 617 618 |
# File 'lib/osvm/machine.rb', line 616 def console_log_path File.join(tmpdir, "#{name}-console.log") end |
#destroy ⇒ Machine
Destroy the machine
148 149 150 151 152 153 |
# File 'lib/osvm/machine.rb', line 148 def destroy log.destroy shared_dir.destroy destroy_disks self end |
#destroy_disks ⇒ Machine
Destroy file-backed disks
Disks are destroyed automatically or when #destroy is called. #destroy_disks can be used to reset storage between machine runs.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/osvm/machine.rb', line 161 def destroy_disks config.disks.each do |disk| next if disk.type != 'file' path = disk_path(disk.device) FileUtils.rm_f(path) end self end |
#disk_path(path) ⇒ Object (protected)
620 621 622 623 624 625 626 627 628 |
# File 'lib/osvm/machine.rb', line 620 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/osvm/machine.rb', line 210 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) output = Base64.decode64(read_shell_output(timeout: real_timeout + 5, command: vm_command)) shell.write("#{workaround}echo ${PIPESTATUS[0]}\n") status = read_shell_output(timeout: 60, command: 'echo ${PIPESTATUS[0]}').strip.to_i if timeout && status == 124 log.execute_end(-1, output) raise TimeoutError, "Timeout occured 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
259 260 261 262 263 264 265 266 267 |
# File 'lib/osvm/machine.rb', line 259 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 |
#finalize ⇒ Object
41 42 43 |
# File 'lib/osvm/machine.rb', line 41 def finalize log.close end |
#inspect ⇒ Object
427 428 429 |
# File 'lib/osvm/machine.rb', line 427 def inspect "#<#{self.class.name}:#{object_id} name=#{name}>" end |
#join(timeout: @default_timeout) ⇒ Object
Block until the machine stops
97 98 99 100 |
# File 'lib/osvm/machine.rb', line 97 def join(timeout: @default_timeout) qemu_reaper.join(timeout) nil end |
#kill ⇒ Machine
Kill the machine
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/osvm/machine.rb', line 118 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
395 396 397 398 |
# File 'lib/osvm/machine.rb', line 395 def mkdir(path) succeeds("mkdir \"#{path}\"") self end |
#mkdir_p(path) ⇒ Machine
Create a directory inside the machine
403 404 405 406 |
# File 'lib/osvm/machine.rb', line 403 def mkdir_p(path) succeeds("mkdir -p \"#{path}\"") self end |
#osctl_json(cmd) ⇒ Hash
osctl command without ‘osctl`, output is returned as JSON
352 353 354 355 |
# File 'lib/osvm/machine.rb', line 352 def osctl_json(cmd) status, output = succeeds("osctl -j #{cmd}") JSON.parse(output, symbolize_names: true) end |
#prepare_disks ⇒ Object (protected)
578 579 580 581 582 583 584 585 586 |
# File 'lib/osvm/machine.rb', line 578 def prepare_disks config.disks.each do |disk| if disk.type != 'file' || !disk.create || File.exist?(disk_path(disk.device)) next end `truncate -s#{disk.size} #{disk_path(disk.device)}` end end |
#pull_file(src, preserve: false) ⇒ String
Pull file from the machine to the host
423 424 425 |
# File 'lib/osvm/machine.rb', line 423 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
414 415 416 417 418 |
# File 'lib/osvm/machine.rb', line 414 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)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/osvm/machine.rb', line 437 def qemu_command(kernel_params: []) all_kernel_params = [ 'console=ttyS0', "init=#{config.toplevel}/init" ] + config.kernel_params + kernel_params [ "#{config.qemu}/bin/qemu-kvm", '-name', "os-vm-#{name}", '-m', config.memory.to_s, '-cpu', 'host', '-smp', "cpus=#{config.cpus},cores=#{config.cpu.cores},threads=#{config.cpu.threads},sockets=#{config.cpu.sockets}", '--no-reboot', '-device', 'ahci,id=ahci' ] + config.network. + [ '-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' ] + + + config. end |
#qemu_disk_options ⇒ Object (protected)
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/osvm/machine.rb', line 463 def 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_options ⇒ Object (protected)
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/osvm/machine.rb', line 474 def 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)
667 668 669 670 671 |
# File 'lib/osvm/machine.rb', line 667 def read_nonblock(io) io.read_nonblock(4096) rescue IO::WaitReadable '' end |
#read_shell_output(timeout:, command:) ⇒ Object (protected)
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/osvm/machine.rb', line 647 def read_shell_output(timeout:, command:) t1 = Time.now buffer = '' loop do if t1 + timeout < Time.now raise UnrecoverableTimeoutError, "Timeout occured while running command '#{command}', " \ "buffer contents: #{buffer.inspect}" end rs = shell.wait_readable(1) next unless rs buffer << read_nonblock(shell) break if buffer.end_with?("\n") end buffer end |
#run_console_thread ⇒ Object (protected)
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/osvm/machine.rb', line 558 def run_console_thread @console_thread = Thread.new do console_log = File.open(console_log_path, 'w') begin loop do rs = qemu_read.wait_readable next unless rs console_log.write(read_nonblock(qemu_read)) console_log.flush end rescue EOFError console_log.close rescue IOError # pass end end end |
#run_qemu_reaper(pid) ⇒ Object (protected)
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/osvm/machine.rb', line 523 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 end end |
#running? ⇒ Boolean
191 192 193 |
# File 'lib/osvm/machine.rb', line 191 def running? @running end |
#shell_socket_path ⇒ Object (protected)
612 613 614 |
# File 'lib/osvm/machine.rb', line 612 def shell_socket_path socket_path("#{name}-shell.sock") end |
#shell_up? ⇒ Boolean (protected)
643 644 645 |
# File 'lib/osvm/machine.rb', line 643 def shell_up? @shell_up end |
#socket_path(socket) ⇒ Object (protected)
638 639 640 641 |
# File 'lib/osvm/machine.rb', line 638 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
48 49 50 51 52 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 |
# File 'lib/osvm/machine.rb', line 48 def start(kernel_params: []) if running? raise 'Machine already started' 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 @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_virtiofs ⇒ Object (protected)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/osvm/machine.rb', line 491 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
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/osvm/machine.rb', line 105 def stop(timeout: @default_timeout) log.stop execute('poweroff -f') if qemu_reaper.join(timeout).nil? raise UnrecoverableTimeoutError, "Timeout while stopping machine #{name}" end self end |
#stop_virtiofs ⇒ Object (protected)
509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/osvm/machine.rb', line 509 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
245 246 247 248 249 250 251 252 253 |
# File 'lib/osvm/machine.rb', line 245 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)
634 635 636 |
# File 'lib/osvm/machine.rb', line 634 def virtiofs_log_path(mount_name) File.join(tmpdir, "#{name}-fs-#{mount_name}.log") end |
#virtiofs_socket_path(mount_name) ⇒ Object (protected)
630 631 632 |
# File 'lib/osvm/machine.rb', line 630 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
202 203 204 |
# File 'lib/osvm/machine.rb', line 202 def wait_for_boot(timeout: @default_timeout) wait_for_shell(timeout:) end |
#wait_for_osctl_pool(name, timeout: @default_timeout) ⇒ Machine
Wait for pool to be imported into osctld
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/osvm/machine.rb', line 370 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.strip == 'active' cur_timeout = timeout - (Time.now - t1) if cur_timeout <= 0 raise TimeoutError, "Timeout occured while waiting for pool #{name.inspect} to become active" end sleep(1) end end |
#wait_for_service(name) ⇒ Machine
Wait for runit system service to start
344 345 346 347 |
# File 'lib/osvm/machine.rb', line 344 def wait_for_service(name) wait_until_succeeds("sv check #{name}") self end |
#wait_for_shell(timeout: @default_timeout) ⇒ Object (protected)
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/osvm/machine.rb', line 588 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 occured while waiting for shell' end rs = shell.wait_readable(1) next unless rs buffer << read_nonblock(shell) 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
327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/osvm/machine.rb', line 327 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
361 362 363 364 |
# File 'lib/osvm/machine.rb', line 361 def wait_for_zpool(name, timeout: @default_timeout) wait_until_succeeds("zpool list #{name}", timeout:) self end |
#wait_until_fails(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command fails
302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/osvm/machine.rb', line 302 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 occured 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
319 320 321 322 |
# File 'lib/osvm/machine.rb', line 319 def wait_until_online(timeout: @default_timeout) wait_until_succeeds('curl --head https://vpsadminos.org', timeout:) self end |
#wait_until_succeeds(cmd, timeout: @default_timeout) ⇒ Array<Integer, String>
Wait until command succeeds
285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/osvm/machine.rb', line 285 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 occured while running command '#{cmd}'" if cur_timeout <= 0 sleep(1) end end |