Changeset 45ebc4d
- Timestamp:
- Sep 9, 2008 2:54:07 PM (16 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
- Children:
- 03e0290
- Parents:
- 987aaa1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/fedd_create.py
r987aaa1 r45ebc4d 145 145 type="string", help="output certificate file") 146 146 147 # Base class that will do a the SOAP/XMLRPC exchange for a request. 148 class fedd_rpc: 149 class RPCException: 150 def __init__(self, fb): 151 self.desc = fb.get('desc', None) 152 self.code = fb.get('code', None) 153 self.errstr = fb.get('errstr', None) 154 155 def __init__(self, pre): 156 """ 157 Specialize the class for the prc method 158 """ 159 self.RequestMessage = globals()["%sRequestMessage" % pre] 160 self.ResponseMessage = globals()["%sResponseMessage" % pre] 161 self.RequestBody="%sRequestBody" % pre 162 self.ResponseBody="%sResponseBody" % pre 163 self.method = pre 164 self.RPCException = fedd_rpc.RPCException 165 166 def do_rpc(self, req_dict, url, transport, cert, trusted, tracefile=None, 167 serialize_only=False): 168 """ 169 The work of sending and parsing the RPC as either XMLRPC or SOAP 170 """ 171 172 context = None 173 while context == None: 174 try: 175 context = fedd_ssl_context(cert, trusted) 176 except SSL.SSLError, e: 177 # Yes, doing this on message type is not ideal. The string 178 # comes from OpenSSL, so check there is this stops working. 179 if str(e) == "bad decrypt": 180 print >>sys.stderr, "Bad Passphrase given." 181 else: raise 182 183 if transport == "soap": 184 loc = feddServiceLocator(); 185 port = loc.getfeddPortType(url, 186 transport=M2Crypto.httpslib.HTTPSConnection, 187 transdict={ 'ssl_context' : context }, 188 tracefile=tracefile) 189 190 req = self.RequestMessage() 191 192 set_req = getattr(req, "set_element_%s" % self.RequestBody, None) 193 set_req(pack_soap(req, self.RequestBody, req_dict)) 194 195 if serialize_only: 196 sw = SoapWriter() 197 sw.serialize(req) 198 print str(sw) 199 sys.exit(0) 200 201 try: 202 method_call = getattr(port, self.method, None) 203 resp = method_call(req) 204 except ZSI.ParseException, e: 205 raise RuntimeError("Malformed response (XMLPRC?): %s" % e) 206 except ZSI.FaultException, e: 207 resp = e.fault.detail[0] 208 209 if resp: 210 resp_call = getattr(resp, "get_element_%s" %self.ResponseBody, 211 None) 212 if resp_call: 213 resp_body = resp_call() 214 if ( resp_body != None): 215 try: 216 return unpack_soap(resp_body) 217 except RuntimeError, e: 218 raise RuntimeError("Bad response. %s" % e.message) 219 elif 'get_element_FeddFaultBody' in dir(resp): 220 resp_body = resp.get_element_FeddFaultBody() 221 if resp_body != None: 222 try: 223 fb = unpack_soap(resp_body) 224 except RuntimeError, e: 225 raise RuntimeError("Bad response. %s" % e.message) 226 raise self.RPCException(fb) 227 else: 228 raise RuntimeError("No body in response!?") 229 else: 230 raise RuntimeError("No response?!?") 231 elif transport == "xmlrpc": 232 if serialize_only: 233 ser = dumps((req_dict,)) 234 print ser 235 sys.exit(0) 236 237 xtransport = SSL_Transport(context) 238 port = ServerProxy(url, transport=xtransport) 239 240 try: 241 method_call = getattr(port, self.method, None) 242 resp = method_call( 243 encapsulate_binaries({ self.RequestBody: msg},\ 244 ('fedid',))) 245 except Error, e: 246 resp = { 'FeddFaultBody': \ 247 { 'errstr' : e.faultCode, 'desc' : e.faultString } } 248 if resp: 249 if resp.has_key(self.ResponseBody): 250 return resp[self.ResponseBody] 251 elif resp.has_key('FeddFaultBody'): 252 raise self.RPCException(resp['FeddFaultBody']) 253 else: 254 raise RuntimeError("No body in response!?") 255 else: 256 raise RuntimeError("No response?!?") 257 else: 258 raise RuntimeError("Unknown RPC transport: %s" % transport) 259 147 260 # Querying experiment data follows the same control flow regardless of the 148 261 # specific data retrieved. This class encapsulates that control flow. 149 class exp_data :262 class exp_data(fedd_rpc): 150 263 def __init__(self, op): 151 264 """ 152 265 Specialize the class for the type of data requested (op) 153 266 """ 154 if op =='vtopo': 155 self.RequestMessage = VtopoRequestMessage; 156 self.ResponseMessage = VtopoResponseMessage; 157 self.RequestBody="VtopoRequestBody" 158 self.ResponseBody="VtopoResponseBody" 159 self.method = "Vtopo" 267 268 fedd_rpc.__init__(self, op) 269 if op =='Vtopo': 160 270 self.key="vtopo" 161 271 self.xml='experiment' 162 elif op == 'vis': 163 self.RequestMessage = VisRequestMessage; 164 self.ResponseMessage = VisResponseMessage; 165 self.RequestBody="VisRequestBody" 166 self.ResponseBody="VisResponseBody" 167 self.method = "Vis" 272 elif op == 'Vis': 168 273 self.key="vis" 169 274 self.xml='vis' … … 218 323 sys.exit("Experiment certfile required") 219 324 220 context = None 221 while context == None: 222 try: 223 context = fedd_ssl_context(cert, opts.trusted) 224 except SSL.SSLError, e: 225 # Yes, doing this on message type is not ideal. The string 226 # comes from OpenSSL, so check there is this stops working. 227 if str(e) == "bad decrypt": 228 print >>sys.stderr, "Bad Passphrase given." 229 else: raise 230 231 msg = { 'experiment': { 'fedid': exp_fedid } } 232 233 if opts.debug > 1: print >>sys.stderr, msg 234 235 if opts.transport == "soap": 236 loc = feddServiceLocator(); 237 port = loc.getfeddPortType(opts.url, 238 transport=M2Crypto.httpslib.HTTPSConnection, 239 transdict={ 'ssl_context' : context }, 325 try: 326 resp_dict = self.do_rpc({ 'experiment': { 'fedid': exp_fedid } }, 327 opts.url, opts.transport, cert, opts.trusted, 328 serialize_only=opts.serialize_only, 240 329 tracefile=opts.tracefile) 241 242 req = self.RequestMessage() 243 244 set_req = getattr(req, "set_element_%s" % self.RequestBody, None) 245 246 set_req(pack_soap(req, self.RequestBody, msg)) 247 248 if opts.serialize_only: 249 sw = SoapWriter() 250 sw.serialize(req) 251 print str(sw) 252 sys.exit(0) 253 254 try: 255 method_call = getattr(port, self.method, None) 256 resp = method_call(req) 257 except ZSI.ParseException, e: 258 sys.exit("Malformed response (XMLPRC?): %s" % e) 259 except ZSI.FaultException, e: 260 resp = e.fault.detail[0] 261 262 if resp: 263 resp_call = getattr(resp, "get_element_%s" %self.ResponseBody, 264 None) 265 if resp_call: 266 resp_body = resp_call() 267 if ( resp_body != None): 268 try: 269 resp_dict = unpack_soap(resp_body) 270 if opts.debug > 1: print >>sys.stderr, resp_dict 271 if resp_dict.has_key(self.key): 272 self.print_xml(resp_dict[self.key]) 273 except RuntimeError, e: 274 sys.exit("Bad response. %s" % e.message) 275 elif 'get_element_FeddFaultBody' in dir(resp): 276 resp_body = resp.get_element_FeddFaultBody() 277 if resp_body != None: 278 exit_with_fault(unpack_soap(resp_body)) 279 else: sys.exit("No body in response!?") 280 else: sys.exit("No response?!?") 281 elif opts.transport == "xmlrpc": 282 if opts.serialize_only: 283 ser = dumps((msg,)) 284 print ser 285 sys.exit(0) 286 287 transport = SSL_Transport(context) 288 port = ServerProxy(opts.url, transport=transport) 289 290 try: 291 method_call = getattr(port, self.method, None) 292 resp = method_call( 293 encapsulate_binaries({ self.RequestBody: msg},\ 294 ('fedid',))) 295 except Error, e: 296 resp = { 'FeddFaultBody': \ 297 { 'errstr' : e.faultCode, 'desc' : e.faultString } } 298 if resp: 299 if resp.has_key(self.ResponseBody): 300 try: 301 resp_dict = resp[self.ResponseBody] 302 if opts.debug > 1: print >>sys.stderr, resp_dict 303 if resp_dict.has_key(self.key): 304 self.print_xml(resp_dict[self.key]) 305 except RuntimeError, e: 306 sys.exit("Bad response. %s" % e.messgae) 307 elif resp.has_key('FeddFaultBody'): 308 exit_with_fault(resp['FeddFaultBody']) 309 else: sys.exit("No body in response!?") 310 else: sys.exit("No response?!?") 311 312 class create: 313 def __init__(self): pass 330 except self.RPCException, e: 331 exit_with_fault(\ 332 {'desc': e.desc, 'errstr': e.errstr, 'code': e.code}) 333 except RuntimeError, e: 334 sys.exit("Error processing RPC: %s" % e.message) 335 336 try: 337 if resp_dict.has_key(self.key): 338 self.print_xml(resp_dict[self.key]) 339 except RuntimeError, e: 340 sys.exit("Bad response. %s" % e.message) 341 342 class create(fedd_rpc): 343 def __init__(self): 344 fedd_rpc.__init__(self, "Create") 314 345 315 346 def add_ssh_key(self, option, opt_str, value, parser, access_keys): … … 398 429 out_certfile = opts.out_certfile 399 430 400 context = None401 while context == None:402 try:403 context = fedd_ssl_context(cert, opts.trusted)404 except SSL.SSLError, e:405 # Yes, doing this on message type is not ideal. The string comes406 # from OpenSSL, so check there is this stops working.407 if str(e) == "bad decrypt":408 print >>sys.stderr, "Bad Passphrase given."409 else: raise410 411 431 msg = { 412 432 'experimentdescription': exp_desc, … … 420 440 if opts.debug > 1: print >>sys.stderr, msg 421 441 422 if opts.transport == "soap": 423 loc = feddServiceLocator(); 424 port = loc.getfeddPortType(opts.url, 425 transport=M2Crypto.httpslib.HTTPSConnection, 426 transdict={ 'ssl_context' : context }, 442 try: 443 resp_dict = self.do_rpc(msg, 444 opts.url, opts.transport, cert, opts.trusted, 445 serialize_only=opts.serialize_only, 427 446 tracefile=opts.tracefile) 428 429 req = CreateRequestMessage() 430 431 req.set_element_CreateRequestBody( 432 pack_soap(req, "CreateRequestBody", msg)) 433 434 if opts.serialize_only: 435 sw = SoapWriter() 436 sw.serialize(req) 437 print str(sw) 438 sys.exit(0) 439 447 except self.RPCException, e: 448 exit_with_fault(\ 449 {'desc': e.desc, 'errstr': e.errstr, 'code': e.code}) 450 except RuntimeError, e: 451 sys.exit("Error processing RPC: %s" % e.message) 452 453 if opts.debug > 1: print >>sys.stderr, resp_dict 454 455 ea = resp_dict.get('experimentAccess', None) 456 if out_certfile and ea and ea.has_key('X509'): 440 457 try: 441 resp = port.Create(req) 442 except ZSI.ParseException, e: 443 sys.exit("Malformed response (XMLPRC?): %s" % e) 444 except ZSI.FaultException, e: 445 resp = e.fault.detail[0] 446 447 if resp: 448 if 'get_element_CreateResponseBody' in dir(resp): 449 resp_body = resp.get_element_CreateResponseBody() 450 if ( resp_body != None): 451 try: 452 resp_dict = unpack_soap(resp_body) 453 if opts.debug > 1: print >>sys.stderr, resp_dict 454 ea = resp_dict.get('experimentAccess', None) 455 if out_certfile and ea and ea.has_key('X509'): 456 try: 457 f = open(out_certfile, "w") 458 print >>f, ea['X509'] 459 f.close() 460 except IOError: 461 sys.exit('Could not write to %s' % \ 462 out_certfile) 463 except RuntimeError, e: 464 sys.exit("Bad response. %s" % e.message) 465 elif 'get_element_FeddFaultBody' in dir(resp): 466 resp_body = resp.get_element_FeddFaultBody() 467 if resp_body != None: 468 exit_with_fault(unpack_soap(resp_body)) 469 else: sys.exit("No body in response!?") 470 else: sys.exit("No response?!?") 471 elif opts.transport == "xmlrpc": 472 if opts.serialize_only: 473 ser = dumps((msg,)) 474 print ser 475 sys.exit(0) 476 477 transport = SSL_Transport(context) 478 port = ServerProxy(opts.url, transport=transport) 479 480 try: 481 resp = port.Create({ 'CreateRequestBody': msg}) 482 except Error, e: 483 resp = { 'FeddFaultBody': \ 484 { 'errstr' : e.faultCode, 'desc' : e.faultString } } 485 if resp: 486 if resp.has_key('CreateResponseBody'): 487 try: 488 resp_dict = resp['CreateResponseBody'] 489 decapsulate_binaries(resp_dict, ('fedid',)) 490 if opts.debug > 1: print >>sys.stderr, resp_dict 491 ea = resp_dict.get('experimentAccess', None) 492 if out_certfile and ea and ea.has_key('X509'): 493 try: 494 f = open(out_certfile, "w") 495 print >>f, ea['X509'] 496 f.close() 497 except IOError: 498 sys.exit('Could not write to %s' % out_certfile) 499 except RuntimeError, e: 500 sys.exit("Bad response. %s" % e.messgae) 501 elif resp.has_key('FeddFaultBody'): 502 exit_with_fault(resp['FeddFaultBody']) 503 else: sys.exit("No body in response!?") 504 505 else: sys.exit("No response?!?") 458 f = open(out_certfile, "w") 459 print >>f, ea['X509'] 460 f.close() 461 except IOError: 462 sys.exit('Could not write to %s' % out_certfile) 506 463 507 464 valid_cmds = ['create'] … … 513 470 elif sys.argv[1] == 'vtopo': 514 471 del sys.argv[1] 515 f = exp_data(' vtopo')472 f = exp_data('Vtopo') 516 473 elif sys.argv[1] == 'vis': 517 474 del sys.argv[1] 518 f = exp_data(' vis')475 f = exp_data('Vis') 519 476 else: 520 477 sys.exit("Bad command: %s. Valid ones are: %s" % \
Note: See TracChangeset
for help on using the changeset viewer.