|
2 | 2 |
|
3 | 3 | import os, sys, time, socket, traceback, syslog, json, argparse |
4 | 4 |
|
5 | | -log_f = os.fdopen(os.dup(sys.stdout.fileno()), "aw") |
6 | | -pid = None |
7 | | -use_syslog = False |
8 | | - |
9 | | -def reopenlog(log_file): |
10 | | - global log_f |
11 | | - if log_f: |
12 | | - log_f.close() |
13 | | - if log_file and log_file <> "stdout:": |
14 | | - log_f = open(log_file, "aw") |
15 | | - elif log_file and log_file == "stdout:": |
16 | | - log_f = os.fdopen(os.dup(sys.stdout.fileno()), "aw") |
17 | | - |
18 | | -def log(txt): |
19 | | - global log_f, pid, use_syslog |
20 | | - if use_syslog: |
21 | | - syslog.syslog(txt) |
22 | | - return |
23 | | - if not pid: |
24 | | - pid = os.getpid() |
25 | | - t = time.strftime("%Y%m%dT%H:%M:%SZ", time.gmtime()) |
26 | | - print >>log_f, "%s [%d] %s" % (t, pid, txt) |
27 | | - log_f.flush() |
28 | | - |
29 | 5 | def success(result): |
30 | 6 | return { "Status": "Success", "Value": result } |
31 | 7 |
|
@@ -116,108 +92,3 @@ def __call__(self, parser, namespace, values, option_string=None): |
116 | 92 | getattr(namespace, self.dest)[k] = v |
117 | 93 | else: |
118 | 94 | setattr(namespace, self.dest, { k: v }) |
119 | | - |
120 | | -# Helper function to daemonise ############################################## |
121 | | -def daemonize(): |
122 | | - def fork(): |
123 | | - try: |
124 | | - if os.fork() > 0: |
125 | | - # parent |
126 | | - os._exit(0) |
127 | | - except OSError, e: |
128 | | - print >>sys.stderr, "fork() failed: %s" % e |
129 | | - traceback.print_exc() |
130 | | - raise |
131 | | - fork() |
132 | | - os.umask(0) |
133 | | - os.chdir("/") |
134 | | - os.setsid() |
135 | | - fork() |
136 | | - devnull = open("/dev/null", "r") |
137 | | - os.dup2(devnull.fileno(), sys.stdin.fileno()) |
138 | | - devnull = open("/dev/null", "aw") |
139 | | - os.dup2(devnull.fileno(), sys.stdout.fileno()) |
140 | | - os.dup2(devnull.fileno(), sys.stderr.fileno()) |
141 | | - |
142 | | -from SocketServer import UnixStreamServer |
143 | | -from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler, SimpleXMLRPCDispatcher |
144 | | -from xmlrpclib import ServerProxy, Fault, Transport |
145 | | -from socket import socket, SOL_SOCKET, SO_REUSEADDR, AF_UNIX, SOCK_STREAM |
146 | | - |
147 | | -# Server XMLRPC from any HTTP POST path ##################################### |
148 | | - |
149 | | -class RequestHandler(SimpleXMLRPCRequestHandler): |
150 | | - rpc_paths = [] |
151 | | - def do_OPTIONS(self): |
152 | | - log("running options thingy") |
153 | | - self.send_response(200, "ok") |
154 | | - self.send_header('Access-Control-Allow-Origin', '*') |
155 | | - self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS, GET') |
156 | | - self.send_header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept') |
157 | | - |
158 | | - |
159 | | -class UnixServer(UnixStreamServer, SimpleXMLRPCDispatcher): |
160 | | - def __init__(self, addr, requestHandler=RequestHandler): |
161 | | - self.logRequests = 0 |
162 | | - if os.path.exists(addr): |
163 | | - os.unlink(addr) |
164 | | - dir = os.path.dirname(addr) |
165 | | - if not(os.path.exists(dir)): |
166 | | - os.makedirs(dir) |
167 | | - SimpleXMLRPCDispatcher.__init__(self) |
168 | | - UnixStreamServer.__init__(self, addr, requestHandler) |
169 | | - |
170 | | -class TCPServer(SimpleXMLRPCServer): |
171 | | - def __init__(self, ip, port, requestHandler=RequestHandler): |
172 | | - SimpleXMLRPCServer.__init__(self, (ip, port), requestHandler=requestHandler) |
173 | | - def server_bind(self): |
174 | | - self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) |
175 | | - SimpleXMLRPCServer.server_bind(self) |
176 | | - |
177 | | -# This is a hack to patch slow socket.getfqdn calls that |
178 | | -# BaseHTTPServer (and its subclasses) make. |
179 | | -# See: http://bugs.python.org/issue6085 |
180 | | -# See: http://www.answermysearches.com/xmlrpc-server-slow-in-python-how-to-fix/2140/ |
181 | | -import BaseHTTPServer |
182 | | - |
183 | | -def _bare_address_string(self): |
184 | | - host, port = self.client_address[:2] |
185 | | - return '%s' % host |
186 | | - |
187 | | -BaseHTTPServer.BaseHTTPRequestHandler.address_string = \ |
188 | | - _bare_address_string |
189 | | - |
190 | | -# This is a hack to allow_none by default, which only became settable in |
191 | | -# python 2.5's SimpleXMLRPCServer |
192 | | - |
193 | | -import xmlrpclib |
194 | | - |
195 | | -original_dumps = xmlrpclib.dumps |
196 | | -def dumps(params, methodname=None, methodresponse=None, encoding=None, |
197 | | - allow_none=1): |
198 | | - return original_dumps(params, methodname, methodresponse, encoding, allow_none) |
199 | | -xmlrpclib.dumps = dumps |
200 | | - |
201 | | -# Well-known feature flags understood by xapi ############################## |
202 | | -# XXX: add an enum to the IDL? |
203 | | - |
204 | | -feature_sr_probe = "SR_PROBE" |
205 | | -feature_sr_update = "SR_UPDATE" |
206 | | -feature_sr_supports_local_caching = "SR_SUPPORTS_LOCAL_CACHING" |
207 | | -feature_vdi_create = "VDI_CREATE" |
208 | | -feature_vdi_delete = "VDI_DELETE" |
209 | | -feature_vdi_attach = "VDI_ATTACH" |
210 | | -feature_vdi_detach = "VDI_DETACH" |
211 | | -feature_vdi_resize = "VDI_RESIZE" |
212 | | -feature_vdi_resize_online = "VDI_RESIZE_ONLINE" |
213 | | -feature_vdi_clone = "VDI_CLONE" |
214 | | -feature_vdi_snapshot = "VDI_SNAPSHOT" |
215 | | -feature_vdi_activate = "VDI_ACTIVATE" |
216 | | -feature_vdi_deactivate = "VDI_DEACTIVATE" |
217 | | -feature_vdi_update = "VDI_UPDATE" |
218 | | -feature_vdi_introduce = "VDI_INTRODUCE" |
219 | | -feature_vdi_generate_config = "VDI_GENERATE_CONFIG" |
220 | | -feature_vdi_reset_on_boot = "VDI_RESET_ON_BOOT" |
221 | | - |
222 | | -def connect(): |
223 | | - return xmlrpclib.Server("http://localhost:80") |
0 commit comments