mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 07:26:29 +00:00
Generalize reading from socket
This commit is contained in:
parent
c71a8230fe
commit
a11e466611
3 changed files with 46 additions and 34 deletions
|
@ -50,40 +50,8 @@ class VirtualBoxInstance(Instance):
|
|||
|
||||
def boot(self):
|
||||
self.machine.launch_vm_process(self.session, 'headless').wait_for_completion(-1)
|
||||
self.console_output = self._read_console_output()
|
||||
|
||||
def _read_console_output(self):
|
||||
import socket
|
||||
import select
|
||||
import errno
|
||||
import sys
|
||||
console = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
console.connect(self.serial_port_path)
|
||||
console.setblocking(0)
|
||||
|
||||
runlvl_check = 'INIT: Entering runlevel: 2'
|
||||
output = ''
|
||||
ptr = 0
|
||||
continue_select = True
|
||||
while continue_select:
|
||||
read_ready, _, _ = select.select([console], [], [])
|
||||
if console in read_ready:
|
||||
while True:
|
||||
try:
|
||||
read = console.recv(1024)
|
||||
output += read
|
||||
sys.stdout.write(read)
|
||||
if runlvl_check in output[ptr:]:
|
||||
continue_select = False
|
||||
else:
|
||||
ptr = len(output) - len(runlvl_check)
|
||||
break
|
||||
except socket.error, e:
|
||||
if e.errno != errno.EWOULDBLOCK:
|
||||
raise Exception(e)
|
||||
continue_select = False
|
||||
console.close()
|
||||
return output
|
||||
from ..tools import read_from_socket
|
||||
self.console_output = read_from_socket(self.serial_port_path, 'INIT: Entering runlevel: 2', 20)
|
||||
|
||||
def shutdown(self):
|
||||
self.session.console.power_down().wait_for_completion(-1)
|
||||
|
|
|
@ -33,6 +33,46 @@ def waituntil(predicate, timeout=5, interval=0.05):
|
|||
return False
|
||||
|
||||
|
||||
def read_from_socket(socket_path, termination_string, timeout):
|
||||
import socket
|
||||
import select
|
||||
import errno
|
||||
console = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
console.connect(socket_path)
|
||||
console.setblocking(0)
|
||||
|
||||
output = ''
|
||||
ptr = 0
|
||||
continue_select = True
|
||||
nooutput_for = 0
|
||||
select_timeout = .5
|
||||
while continue_select:
|
||||
read_ready, _, _ = select.select([console], [], [], select_timeout)
|
||||
if console in read_ready:
|
||||
nooutput_for = 0
|
||||
while True:
|
||||
try:
|
||||
output += console.recv(1024)
|
||||
if termination_string in output[ptr:]:
|
||||
continue_select = False
|
||||
else:
|
||||
ptr = len(output) - len(termination_string)
|
||||
break
|
||||
except socket.error, e:
|
||||
if e.errno != errno.EWOULDBLOCK:
|
||||
raise Exception(e)
|
||||
continue_select = False
|
||||
else:
|
||||
nooutput_for += select_timeout
|
||||
if nooutput_for > timeout:
|
||||
from exceptions import SocketReadTimeout
|
||||
msg = ('Reading from socket `{path}\' timed out after {seconds} seconds.'
|
||||
.format(path=socket_path, timeout=nooutput_for))
|
||||
raise SocketReadTimeout(msg)
|
||||
console.close()
|
||||
return output
|
||||
|
||||
|
||||
def bootstrap(manifest, build_server):
|
||||
from bootstrapvz.remote.build_servers import LocalBuildServer
|
||||
if isinstance(build_server, LocalBuildServer):
|
||||
|
|
4
tests/integration/tools/exceptions.py
Normal file
4
tests/integration/tools/exceptions.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
|
||||
|
||||
class SocketReadTimeout(Exception):
|
||||
pass
|
Loading…
Add table
Reference in a new issue