ia64/xen-unstable

view tools/xfrd/xfrdClient.py @ 2422:2274a0386cc9

bitkeeper revision 1.1159.69.5 (4138e882jA1YaR_OfTfNHe_uT4PDIg)

trivial
author iap10@labyrinth.cl.cam.ac.uk
date Fri Sep 03 21:56:18 2004 +0000 (2004-09-03)
parents 06e9774f9633
children
line source
1 #!/bin/env python
2 """
3 Test client for the migration daemon (xfrd).
5 Author: Mike Wray <mike.wray@hp.com>
7 """
8 import getopt
9 import sys
10 import os
11 from socket import *
12 import StringIO
14 sys.path.append("/home/mjw/repos-bk/xeno-unstable.bk/tools/python")
16 import xen.xend.sxp as sxp
18 XFRD_PORT = 8002
20 verbose = 0
22 class TCPClient:
24 def __init__(self, host, port):
25 print ">TCPClient"
26 self.sock = socket(AF_INET, SOCK_STREAM, 0)
27 print ">TCPClient sock=", self.sock
28 print ">TCPClient> connect ", host, port
29 v = self.sock.connect((host, port))
30 print ">TCPClient> connect=", v
31 # Send plain header (no gzip).
32 #self.sock.send("\0\0")
34 self.sockin = self.sock.makefile("r")
35 self.sockout = self.sock.makefile("w")
36 #pass
38 def request(self, req):
39 print "request>", req
40 sxp.show(req, out=self.sockout)
41 self.sockout.flush()
42 print "request<"
44 def request_hello(self):
45 self.request(['xfr.hello', XFR_PROTO_MAJOR, XFR_PROTO_MINOR])
47 def request_migrate(self, vmid, vhost, vport, vmconfig='(vm)'):
48 self.request(['xfr.migrate', vmid, vmconfig, vhost, vport])
50 def read(self):
51 while(1):
52 v = self.sockin.read()
53 print 'read>', v
54 #if v[0] == 'xfr.err' and v[1]: return
55 #if v[0] == 'xfr.ok': return
57 XFR_PROTO_MAJOR = 1
58 XFR_PROTO_MINOR = 0
60 host_default = "localhost"
61 port_default = XFRD_PORT
62 vhost_default = "localhost"
63 vport_default = 8003
64 vmid_default = 1
66 # Short options. Options followed by ':' need a parameter.
67 short_opts = 'h'
69 # Long options. Options ending in '=' need a parameter.
70 long_opts = [ 'host=', 'port=', 'vhost=', 'vport=', 'vmid=', 'verbose', 'help']
72 def usage(err=None):
73 if err:
74 out = sys.stderr
75 else:
76 out = sys.stdout
77 print >> out, 'Usage: %s [options] [command...]\n' % sys.argv[0]
78 print >> out, '--host <host>\n\tHost to initiate transfer on. Default %s.' % host_default
79 print >> out, '--port <port>\n\tPort to initiate transfer on. Default %d.' % port_default
80 print >> out, '--vhost <vhost>\n\tHost to transfer VM to. Default %s.' % vhost_default
81 print >> out, '--vport <vport>\n\tPort to transfer VM to. Default %d.' % vport_default
82 print >> out, '--vmid <vmid>\n\tVM id. Default %d.' % vmid_default
83 print >> out, '--help\n\tPrint help.'
85 def main(argv):
86 global verbose
87 host = host_default
88 port = port_default
89 vhost = vhost_default
90 vport = vport_default
91 vmid = vmid_default
93 try:
94 opts, args = getopt.getopt(argv[1:], short_opts, long_opts)
95 except getopt.GetoptError, ex:
96 print >>sys.stderr, 'Error:', ex
97 usage(1)
98 sys.exit(1)
100 for key, val in opts:
101 if key == '--help':
102 usage()
103 sys.exit(0)
104 elif key == '--host':
105 host = val
106 elif key == '--port':
107 port = int(val)
108 elif key == '--vhost':
109 vhost = val
110 elif key == '--vport':
111 vport = int(val)
112 elif key == '--vmid':
113 vmid = int(val)
115 print "host=%s port=%d" % (host, port)
116 print "vhost=%s vport=%d vmid=%d" % (vhost, vport, vmid)
117 client = TCPClient(gethostbyname(host), port)
118 client.request_hello()
119 client.request_migrate(vmid, gethostbyname(vhost), vport)
120 client.read()
122 if __name__ == '__main__':
123 main(sys.argv)