ia64/xen-unstable

changeset 5153:be7e052ce7e8

bitkeeper revision 1.1550.1.2 (4294a1f1_I9jQ97QY8OTqDCx1kDIUA)

SrvDaemon.py, XendRoot.py, relocate.py:
Add relocation server.
XendCheckpoint.py:
Import XendError.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed May 25 16:04:01 2005 +0000 (2005-05-25)
parents fed73636e06b
children 6e81b0f58356
files .rootkeys tools/python/xen/xend/XendCheckpoint.py tools/python/xen/xend/XendRoot.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xend/server/relocate.py
line diff
     1.1 --- a/.rootkeys	Wed May 25 13:24:35 2005 +0000
     1.2 +++ b/.rootkeys	Wed May 25 16:04:01 2005 +0000
     1.3 @@ -866,6 +866,7 @@ 40c9c469yrm31i60pGKslTi2Zgpotg tools/pyt
     1.4  40c9c46925x-Rjb0Cv2f1-l2jZrPYg tools/python/xen/xend/server/netif.py
     1.5  40c9c469ZqILEQ8x6yWy0_51jopiCg tools/python/xen/xend/server/params.py
     1.6  4266169eI_oX3YBjwaeC0V-THBRnjg tools/python/xen/xend/server/pciif.py
     1.7 +4294a1bf8rMUcddot-B2-pOxORimOg tools/python/xen/xend/server/relocate.py
     1.8  41ee5e8dq9NtihbL4nWKjuSLOhXPUg tools/python/xen/xend/server/usbif.py
     1.9  40c9c469LNxLVizOUpOjEaTKKCm8Aw tools/python/xen/xend/sxp.py
    1.10  40d05079aFRp6NQdo5wIh5Ly31c0cg tools/python/xen/xm/__init__.py
     2.1 --- a/tools/python/xen/xend/XendCheckpoint.py	Wed May 25 13:24:35 2005 +0000
     2.2 +++ b/tools/python/xen/xend/XendCheckpoint.py	Wed May 25 16:04:01 2005 +0000
     2.3 @@ -13,6 +13,7 @@ from struct import pack, unpack, calcsiz
     2.4  from xen.util.xpopen import xPopen3
     2.5  import xen.lowlevel.xc; xc = xen.lowlevel.xc.new()
     2.6  
     2.7 +from XendError import XendError
     2.8  from XendLogging import log
     2.9  
    2.10  SIGNATURE = "LinuxGuestRecord"
     3.1 --- a/tools/python/xen/xend/XendRoot.py	Wed May 25 13:24:35 2005 +0000
     3.2 +++ b/tools/python/xen/xend/XendRoot.py	Wed May 25 16:04:01 2005 +0000
     3.3 @@ -51,12 +51,21 @@ class XendRoot:
     3.4      """Default interface address xend listens at. """
     3.5      xend_address_default      = ''
     3.6  
     3.7 +    """Default for the flag indicating whether xend should run a relocation server."""
     3.8 +    xend_relocation_server_default = 'yes'
     3.9 +
    3.10 +    """Default interface address the xend relocation server listens at. """
    3.11 +    xend_relocation_address_default = ''
    3.12 +
    3.13      """Default port xend serves HTTP at. """
    3.14      xend_port_default         = '8000'
    3.15  
    3.16      """Default port xend serves events at. """
    3.17      xend_event_port_default   = '8001'
    3.18  
    3.19 +    """Default port xend serves relocation at. """
    3.20 +    xend_relocation_port_default = '8002'
    3.21 +
    3.22      """Default for the flag indicating whether xend should run a unix-domain server."""
    3.23      xend_unix_server_default = 'yes'
    3.24  
    3.25 @@ -249,6 +258,11 @@ class XendRoot:
    3.26          """
    3.27          return self.get_config_bool("xend-http-server", self.xend_http_server_default)
    3.28  
    3.29 +    def get_xend_relocation_server(self):
    3.30 +        """Get the flag indicating whether xend should run a relocation server.
    3.31 +        """
    3.32 +        return self.get_config_bool("xend-relocation-server", self.xend_relocation_server_default)
    3.33 +
    3.34      def get_xend_port(self):
    3.35          """Get the port xend listens at for its HTTP interface.
    3.36          """
    3.37 @@ -259,6 +273,11 @@ class XendRoot:
    3.38          """
    3.39          return self.get_config_int('xend-event-port', self.xend_event_port_default)
    3.40  
    3.41 +    def get_xend_relocation_port(self):
    3.42 +        """Get the port xend listens at for connection to its relocation server.
    3.43 +        """
    3.44 +        return self.get_config_int('xend-relocation-port', self.xend_relocation_port_default)
    3.45 +
    3.46      def get_xend_address(self):
    3.47          """Get the address xend listens at for its HTTP and event ports.
    3.48          This defaults to the empty string which allows all hosts to connect.
    3.49 @@ -267,6 +286,14 @@ class XendRoot:
    3.50          """
    3.51          return self.get_config_value('xend-address', self.xend_address_default)
    3.52  
    3.53 +    def get_xend_relocation_address(self):
    3.54 +        """Get the address xend listens at for its HTTP and event ports.
    3.55 +        This defaults to the empty string which allows all hosts to connect.
    3.56 +        If this is set to 'localhost' only the localhost will be able to connect
    3.57 +        to the HTTP and event ports.
    3.58 +        """
    3.59 +        return self.get_config_value('xend-relocation-address', self.xend_relocation_address_default)
    3.60 +
    3.61      def get_xend_unix_server(self):
    3.62          """Get the flag indicating whether xend should run a unix-domain server.
    3.63          """
     4.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Wed May 25 13:24:35 2005 +0000
     4.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Wed May 25 16:04:01 2005 +0000
     4.3 @@ -30,6 +30,7 @@ from xen.xend.XendLogging import log
     4.4  import channel
     4.5  import controller
     4.6  import event
     4.7 +import relocate
     4.8  from params import *
     4.9  
    4.10  DAEMONIZE = 0
    4.11 @@ -302,6 +303,7 @@ class Daemon:
    4.12              log.info("Xend Daemon started")
    4.13              self.createFactories()
    4.14              event.listenEvent(self)
    4.15 +            relocate.listenRelocation()
    4.16              self.listenChannels()
    4.17              servers = SrvServer.create()
    4.18              self.daemonize()
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tools/python/xen/xend/server/relocate.py	Wed May 25 16:04:01 2005 +0000
     5.3 @@ -0,0 +1,125 @@
     5.4 +import sys
     5.5 +import StringIO
     5.6 +
     5.7 +from xen.web import reactor, protocol
     5.8 +
     5.9 +from xen.xend import scheduler
    5.10 +from xen.xend import sxp
    5.11 +from xen.xend import EventServer; eserver = EventServer.instance()
    5.12 +from xen.xend.XendError import XendError
    5.13 +from xen.xend import XendRoot; xroot = XendRoot.instance()
    5.14 +from xen.xend.XendLogging import log
    5.15 +from xen.xend import XendCheckpoint
    5.16 +
    5.17 +DEBUG = 0
    5.18 +
    5.19 +class RelocationProtocol(protocol.Protocol):
    5.20 +    """Asynchronous handler for a connected relocation socket.
    5.21 +    """
    5.22 +
    5.23 +    def __init__(self):
    5.24 +        #protocol.Protocol.__init__(self)
    5.25 +        self.parser = sxp.Parser()
    5.26 +
    5.27 +    def dataReceived(self, data):
    5.28 +        try:
    5.29 +            self.parser.input(data)
    5.30 +            if self.parser.ready():
    5.31 +                val = self.parser.get_val()
    5.32 +                res = self.dispatch(val)
    5.33 +                self.send_result(res)
    5.34 +            if self.parser.at_eof():
    5.35 +                self.loseConnection()
    5.36 +        except SystemExit:
    5.37 +            raise
    5.38 +        except:
    5.39 +            self.send_error()
    5.40 +
    5.41 +    def loseConnection(self):
    5.42 +        if self.transport:
    5.43 +            self.transport.loseConnection()
    5.44 +        if self.connected:
    5.45 +            scheduler.now(self.connectionLost)
    5.46 +
    5.47 +    def connectionLost(self, reason=None):
    5.48 +        pass
    5.49 +
    5.50 +    def send_reply(self, sxpr):
    5.51 +        io = StringIO.StringIO()
    5.52 +        sxp.show(sxpr, out=io)
    5.53 +        print >> io
    5.54 +        io.seek(0)
    5.55 +        if self.transport:
    5.56 +            return self.transport.write(io.getvalue())
    5.57 +        else:
    5.58 +            return 0
    5.59 +
    5.60 +    def send_result(self, res):
    5.61 +        if res is None:
    5.62 +            resp = ['ok']
    5.63 +        else:
    5.64 +            resp = ['ok', res]
    5.65 +        return self.send_reply(resp)
    5.66 +
    5.67 +    def send_error(self):
    5.68 +        (extype, exval) = sys.exc_info()[:2]
    5.69 +        return self.send_reply(['err',
    5.70 +                                ['type', str(extype)],
    5.71 +                                ['value', str(exval)]])
    5.72 +
    5.73 +    def opname(self, name):
    5.74 +         return 'op_' + name.replace('.', '_')
    5.75 +
    5.76 +    def operror(self, name, req):
    5.77 +        raise XendError('Invalid operation: ' +name)
    5.78 +
    5.79 +    def dispatch(self, req):
    5.80 +        op_name = sxp.name(req)
    5.81 +        op_method_name = self.opname(op_name)
    5.82 +        op_method = getattr(self, op_method_name, self.operror)
    5.83 +        return op_method(op_name, req)
    5.84 +
    5.85 +    def op_help(self, name, req):
    5.86 +        def nameop(x):
    5.87 +            if x.startswith('op_'):
    5.88 +                return x[3:].replace('_', '.')
    5.89 +            else:
    5.90 +                return x
    5.91 +        
    5.92 +        l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
    5.93 +        return l
    5.94 +
    5.95 +    def op_quit(self, name, req):
    5.96 +        self.loseConnection()
    5.97 +
    5.98 +    def op_receive(self, name, req):
    5.99 +        if self.transport:
   5.100 +            self.send_reply(["ready", name])
   5.101 +            self.transport.sock.setblocking(1)
   5.102 +            xd = xroot.get_component("xen.xend.XendDomain")
   5.103 +            XendCheckpoint.restore(xd, self.transport.sock.fileno())
   5.104 +            self.transport.sock.setblocking(0)
   5.105 +        else:
   5.106 +            log.error(name + ": no transport")
   5.107 +            raise XendError(name + ": no transport")
   5.108 +
   5.109 +class RelocationFactory(protocol.ServerFactory):
   5.110 +    """Asynchronous handler for the relocation server socket.
   5.111 +    """
   5.112 +
   5.113 +    def __init__(self):
   5.114 +        #protocol.ServerFactory.__init__(self)
   5.115 +        pass
   5.116 +
   5.117 +    def buildProtocol(self, addr):
   5.118 +        return RelocationProtocol()
   5.119 +
   5.120 +def listenRelocation():
   5.121 +    factory = RelocationFactory()
   5.122 +    if xroot.get_xend_unix_server():
   5.123 +        path = '/var/lib/xend/relocation-socket'
   5.124 +        reactor.listenUNIX(path, factory)
   5.125 +    if xroot.get_xend_relocation_server():
   5.126 +        port = xroot.get_xend_relocation_port()
   5.127 +        interface = xroot.get_xend_relocation_address()
   5.128 +        reactor.listenTCP(port, factory, interface=interface)