2015-01-25 11:35:30 +01:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from bootstrapvz.remote import register_deserialization_handlers
|
|
|
|
|
2014-11-30 14:09:21 +01:00
|
|
|
# Register deserialization handlers for objects
|
|
|
|
# that will pass between server and client
|
|
|
|
register_deserialization_handlers()
|
|
|
|
|
2014-08-31 13:45:35 +02:00
|
|
|
|
2015-01-25 11:35:30 +01:00
|
|
|
@contextmanager
|
|
|
|
def boot_manifest(manifest_data):
|
|
|
|
from bootstrapvz.common.tools import load_data
|
|
|
|
build_servers = load_data('build-servers.yml')
|
|
|
|
from bootstrapvz.remote.build_servers import pick_build_server
|
|
|
|
build_server = pick_build_server(build_servers, manifest_data)
|
|
|
|
|
|
|
|
manifest_data = build_server.apply_build_settings(manifest_data)
|
|
|
|
from bootstrapvz.base.manifest import Manifest
|
|
|
|
manifest = Manifest(data=manifest_data)
|
|
|
|
|
2015-01-25 17:38:17 +01:00
|
|
|
bootstrap_info = None
|
|
|
|
with build_server.connect() as connection:
|
|
|
|
bootstrap_info = connection.run(manifest)
|
2015-01-25 11:35:30 +01:00
|
|
|
|
|
|
|
from ..images import initialize_image
|
|
|
|
image = initialize_image(manifest, build_server, bootstrap_info)
|
|
|
|
try:
|
|
|
|
with image.get_instance() as instance:
|
|
|
|
yield instance
|
|
|
|
finally:
|
|
|
|
image.destroy()
|
|
|
|
|
|
|
|
|
2014-12-18 21:47:51 +01:00
|
|
|
def waituntil(predicate, timeout=5, interval=0.05):
|
|
|
|
import time
|
|
|
|
threshhold = time.time() + timeout
|
|
|
|
while time.time() < threshhold:
|
|
|
|
if predicate():
|
|
|
|
return True
|
|
|
|
time.sleep(interval)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-01-16 01:52:42 +01:00
|
|
|
def read_from_socket(socket_path, termination_string, timeout, read_timeout=0.5):
|
2014-12-20 16:07:00 +01:00
|
|
|
import socket
|
|
|
|
import select
|
|
|
|
import errno
|
|
|
|
console = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
console.connect(socket_path)
|
|
|
|
console.setblocking(0)
|
|
|
|
|
2015-01-16 01:52:42 +01:00
|
|
|
from timeit import default_timer
|
|
|
|
start = default_timer()
|
|
|
|
|
2014-12-20 16:07:00 +01:00
|
|
|
output = ''
|
|
|
|
ptr = 0
|
|
|
|
continue_select = True
|
|
|
|
while continue_select:
|
2015-01-16 01:52:42 +01:00
|
|
|
read_ready, _, _ = select.select([console], [], [], read_timeout)
|
2014-12-20 16:07:00 +01:00
|
|
|
if console in read_ready:
|
|
|
|
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
|
2015-01-16 01:52:42 +01:00
|
|
|
if default_timer() - start > timeout:
|
2014-12-20 16:07:00 +01:00
|
|
|
from exceptions import SocketReadTimeout
|
2015-01-16 01:52:42 +01:00
|
|
|
msg = ('Reading from socket `{path}\' timed out after {seconds} seconds.\n'
|
|
|
|
'Here is the output so far:\n{output}'
|
|
|
|
.format(path=socket_path, seconds=timeout, output=output))
|
2014-12-20 16:07:00 +01:00
|
|
|
raise SocketReadTimeout(msg)
|
|
|
|
console.close()
|
|
|
|
return output
|