ia64/xen-unstable

view tools/python/xen/xend/server/SrvServer.py @ 8323:566395e5a14f

Minor tidy.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Dec 12 16:32:50 2005 +0000 (2005-12-12)
parents 1aaa1abab953
children faa1eb1621b9
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 """Example xend HTTP
20 Can be accessed from a browser or from a program.
21 Do 'python SrvServer.py' to run the server.
22 Then point a web browser at http://localhost:8000/xend and follow the links.
23 Most are stubs, except /domain which has a list of domains and a 'create domain'
24 button.
26 You can also access the server from a program.
27 Do 'python XendClient.py' to run a few test operations.
29 The data served differs depending on the client (as defined by User-Agent
30 and Accept in the HTTP headers). If the client is a browser, data
31 is returned in HTML, with interactive forms. If the client is a program,
32 data is returned in SXP format, with no forms.
34 The server serves to the world by default. To restrict it to the local host
35 change 'interface' in main().
37 Mike Wray <mike.wray@hp.com>
38 """
39 # todo Support security settings etc. in the config file.
40 # todo Support command-line args.
42 import fcntl
43 import time
44 from threading import Thread
46 from xen.web.httpserver import HttpServer, UnixHttpServer
48 from xen.xend import XendRoot
49 from xen.xend import Vifctl
50 from xen.xend.XendLogging import log
51 from xen.web.SrvDir import SrvDir
53 from SrvRoot import SrvRoot
56 xroot = XendRoot.instance()
59 class XendServers:
61 def __init__(self):
62 self.servers = []
64 def add(self, server):
65 self.servers.append(server)
67 def start(self, status):
68 # Running the network script will spawn another process, which takes
69 # the status fd with it unless we set FD_CLOEXEC. Failing to do this
70 # causes the read in SrvDaemon to hang even when we have written here.
71 if status:
72 fcntl.fcntl(status, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
74 Vifctl.network('start')
75 threads = []
76 for server in self.servers:
77 thread = Thread(target=server.run)
78 thread.start()
79 threads.append(thread)
82 # check for when all threads have initialized themselves and then
83 # close the status pipe
85 threads_left = True
86 while threads_left:
87 threads_left = False
89 for server in self.servers:
90 if not server.ready:
91 threads_left = True
92 break
94 if threads_left:
95 time.sleep(.5)
97 if status:
98 status.write('0')
99 status.close()
101 for t in threads:
102 t.join()
104 def create():
105 root = SrvDir()
106 root.putChild('xend', SrvRoot())
107 servers = XendServers()
108 if xroot.get_xend_http_server():
109 port = xroot.get_xend_port()
110 interface = xroot.get_xend_address()
111 servers.add(HttpServer(root=root, interface=interface, port=port))
112 if xroot.get_xend_unix_server():
113 path = xroot.get_xend_unix_path()
114 log.info('unix path=' + path)
115 servers.add(UnixHttpServer(path=path, root=root))
116 return servers