[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 2/3] Add SSH over XML-RPC support to xmlrpclib2.ServerProxy



This is the client-side support for SSH over XML-RPC.

Regards,

Anthony Liguori
# HG changeset patch
# User anthony@xxxxxxxxxxxxxxxxxxxxx
# Node ID 03bbc156d3e8fb01eee2af990176bb64e012c4e1
# Parent  cf8e253723daf8b95b54b24f6988a8e74c6cc0aa
Add support to our ServerProxy class for XML-RPC over SSH.

This adds a new XML-RPC transport (SSHTransport) and a new supported
protocol to the ServerProxy class.  This protocol is accessed with a URI of
the following form:

  ssh://[user@]hostname[/path]

While xm makes too many assumptions about being on a local system for all
commands to work when a remote URI is used, many of the commands do work.

This is mostly useful to people who are using xmlrpclib2 to write Xen
management tools.  It may prove more useful in the future to support
authenticated migration though.

Signed-off-by: Anthony Liguori <aliguori@xxxxxxxxxx>

diff -r cf8e253723da -r 03bbc156d3e8 tools/python/xen/util/xmlrpclib2.py
--- a/tools/python/xen/util/xmlrpclib2.py       Sat Jun 10 19:11:55 2006
+++ b/tools/python/xen/util/xmlrpclib2.py       Sat Jun 10 19:16:33 2006
@@ -24,13 +24,51 @@
 import types
 
 from httplib import HTTPConnection, HTTP
-from xmlrpclib import Transport
+from xmlrpclib import Transport, getparser, Fault
 from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
-import xmlrpclib, socket, os, stat
+from subprocess import Popen, PIPE
+from getpass import getuser
+from fcntl import ioctl
+import xmlrpclib, socket, os, stat, termios
 import SocketServer
 
-import xen.xend.XendClient
 from xen.xend.XendLogging import log
+
+class SSHTransport(object):
+    def __init__(self, host, user, askpass=None):
+        if askpass:
+            f = open('/dev/tty', 'w')
+            os.environ['SSH_ASKPASS'] = askpass
+            ioctl(f.fileno(), termios.TIOCNOTTY)
+            f.close()
+            
+        self.ssh = Popen(['ssh', '%s@%s' % (user, host), 'xm serve'],
+                         bufsize=0, stdin=PIPE, stdout=PIPE)
+
+    def request(self, host, handler, request_body, verbose=0):
+        p, u = getparser()
+        self.ssh.stdin.write("""POST /%s HTTP/1.1
+User-Agent: Xen
+Host: %s
+Content-Type: text/xml
+Content-Length: %d
+
+%s""" % (handler, host, len(request_body), request_body))
+        self.ssh.stdin.flush()
+
+        content_length = 0
+        line = self.ssh.stdout.readline()
+        if line.split()[1] != '200':
+            raise Fault(0, 'Server returned %s' % (' '.join(line[1:])))
+        
+        while line not in ['', '\r\n', '\n']:
+            if line.lower().startswith('content-length:'):
+                content_length = int(line[15:].strip())
+            line = self.ssh.stdout.readline()
+        content = self.ssh.stdout.read(content_length)
+        p.feed(content)
+        p.close()
+        return u.close()
 
 
 # A new ServerProxy that also supports httpu urls.  An http URL comes in the
@@ -100,9 +138,22 @@
             if protocol == 'httpu':
                 uri = 'http:' + rest
                 transport = UnixTransport()
+            elif protocol == 'ssh':
+                if not rest.startswith('//'):
+                    raise ValueError("Invalid ssh URL '%s'" % uri)
+                rest = rest[2:]
+                user = getuser()
+                path = 'RPC2'
+                if rest.find('@') != -1:
+                    (user, rest) = rest.split('@', 1)
+                if rest.find('/') != -1:
+                    (host, rest) = rest.split('/', 1)
+                    if len(rest) > 0:
+                        path = rest
+                transport = SSHTransport(host, user)
+                uri = 'http://%s/%s' % (host, path)
         xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding,
                                        verbose, allow_none)
-
 
     def __request(self, methodname, params):
         response = xmlrpclib.ServerProxy.__request(self, methodname, params)
@@ -150,6 +201,7 @@
         except xmlrpclib.Fault, fault:
             response = xmlrpclib.dumps(fault)
         except Exception, exn:
+            import xen.xend.XendClient
             log.exception(exn)
             response = xmlrpclib.dumps(
                 xmlrpclib.Fault(xen.xend.XendClient.ERROR_INTERNAL, str(exn)))
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.