Back to index

nordugrid-arc-nox  1.1.0~rc6
bartender_client.py
Go to the documentation of this file.
00001 import arc, sys, time, os
00002 from arcom.service import false
00003 from arcom.xmltree import XMLTree
00004 from storage.client import BartenderClient, ByteIOClient
00005 from storage.common import create_checksum, upload_to_turl, download_from_turl
00006 args = sys.argv[1:]
00007 if len(args) > 0 and args[0] == '-x':
00008     args.pop(0)
00009     print_xml = True
00010 else:
00011     print_xml = False
00012 try:
00013     bartender_url = os.environ['ARC_BARTENDER_URL']
00014     print '- The URL of the Bartender:', bartender_url
00015 except:
00016     bartender_url = 'http://localhost:60000/Bartender'
00017     print '- ARC_BARTENDER_URL environment variable not found, using', bartender_url
00018 ssl_config = {}
00019 if bartender_url.startswith('https'):
00020     key_file = os.environ.get('ARC_KEY_FILE', None)
00021     cert_file = os.environ.get('ARC_CERT_FILE', None)
00022     proxy_file = os.environ.get('ARC_PROXY_FILE', None)
00023     ca_file = os.environ.get('ARC_CA_FILE', None)
00024     ca_dir = os.environ.get('ARC_CA_DIR', None)
00025     if proxy_file:
00026         ssl_config['proxy_file'] = proxy_file
00027         print '- The proxy certificate file:', ssl_config['proxy_file']
00028     else:
00029         if key_file and cert_file:                
00030             ssl_config['key_file'] = key_file
00031             ssl_config['cert_file'] = cert_file
00032             print '- The key file:', ssl_config['key_file']
00033             print '- The cert file:', ssl_config['cert_file']
00034     if ca_file:
00035         ssl_config['ca_file'] = ca_file
00036         print '- The CA file:', ssl_config['ca_file']
00037     elif ca_dir:
00038         ssl_config['ca_dir'] = ca_dir
00039         print '- The CA dir:', ssl_config['ca_dir']
00040 bartender = BartenderClient(bartender_url, print_xml, ssl_config = ssl_config)    
00041 if len(args) == 0 or args[0] not in ['stat', 'makeMountpoint','unmakeMountpoint', 'unmakeCollection', 'makeCollection', 'list', 'move', 'putFile', 'getFile', 'delFile', 'addReplica', 'modify']:
00042     print 'Supported methods: stat, makeCollection, unmakeCollection, list, move, putFile, getFile, delFile, addReplica, modify' 
00043 else:
00044     command = args.pop(0)
00045     if command == 'stat':
00046         if len(args) < 1:
00047             print 'Usage: stat <LN> [<LN> ...]'
00048         else:
00049             request = dict([(i, args[i]) for i in range(len(args))])
00050             print 'stat', request
00051             stat = bartender.stat(request)
00052             print stat
00053             for i,s in stat.items():
00054                 print '%s:' % args[int(i)]
00055                 c = {}
00056                 for k,v in s.items():
00057                     sect, prop = k
00058                     c[sect] = c.get(sect,[])
00059                     c[sect].append((prop, v))
00060                 for k, vs in c.items():
00061                     print k
00062                     for p, v in vs:
00063                         print '  %s: %s' % (p, v)
00064     elif command == 'delFile':
00065         if len(args) < 1:
00066             print 'Usage: delFile <LN> [<LN> ...]'
00067         else:
00068             request = dict([(i, args[i]) for i in range(len(args))])
00069             print 'delFile', request
00070             response = bartender.delFile(request)
00071             print response
00072     elif command == 'getFile':
00073         if len(args) < 2:
00074             print 'Usage: getFile <target filename> <source LN>'
00075         else:
00076             LN = args[1]
00077             filename = args[0]
00078             f = file(filename, 'wb')
00079             request = {'0' : (LN, ['byteio', 'http'])}
00080             print 'getFile', request
00081             response = bartender.getFile(request)
00082             print response
00083             success, turl, protocol = response['0']
00084             print '\n', LN, success
00085             if success == 'done':
00086                 print 'Downloading from', turl, 'to', filename, 'with', protocol
00087                 download_from_turl(turl, protocol, f, ssl_config = ssl_config)
00088     elif command == 'addReplica':
00089         if len(args) < 2:
00090             print 'Usage: addReplica <source filename> <GUID>'
00091         else:
00092             filename = args[0]
00093             requests = {'0' : args[1]}
00094             protocols = ['byteio', 'http']
00095             print 'addReplica', requests, protocols
00096             response = bartender.addReplica(requests, protocols)
00097             print response
00098             success, turl, protocol = response['0']
00099             if success == 'done':
00100                 f = file(filename,'rb')
00101                 print 'Uploading from', filename, 'to', turl, 'with', protocol
00102                 upload_to_turl(turl, protocol, f, ssl_config = ssl_config)
00103     elif command == 'putFile':
00104         if len(args) < 2:
00105             print 'Usage: putFile <source filename> <target LN>'
00106         else:
00107             filename = args[0]
00108             size = os.path.getsize(filename)
00109             f = file(filename,'rb')
00110             checksum = create_checksum(f, 'md5')
00111             LN = args[1]
00112             if LN.endswith('/'):
00113                 LN = LN + filename.split('/')[-1]
00114             metadata = {('states', 'size') : size, ('states', 'checksum') : checksum,
00115                     ('states', 'checksumType') : 'md5', ('states', 'neededReplicas') : 2}
00116             request = {'0': (LN, metadata, ['byteio', 'http'])}
00117             print 'putFile', request
00118             response = bartender.putFile(request)
00119             print response
00120             success, turl, protocol = response['0']
00121             print '\n', LN, success
00122             if success == 'done':
00123                 f = file(filename,'rb')
00124                 print 'Uploading from', filename, 'to', turl, 'with', protocol
00125                 upload_to_turl(turl, protocol, f, ssl_config = ssl_config)
00126     elif command == 'unmakeCollection':
00127         if len(args) < 1:
00128             print 'Usage: unmakeCollection <LN>'
00129         else:
00130             request = {'0': (args[0])}
00131             print 'unmakeCollection', request
00132             response = bartender.unmakeCollection(request)
00133             print response
00134             print '\n', response['0']
00135     elif command == 'makeCollection':
00136         if len(args) < 1:
00137             print 'Usage: makeCollection <LN>'
00138         else:
00139             request = {'0': (args[0], {('states', 'closed') : false})}
00140             print 'makeCollection', request
00141             response = bartender.makeCollection(request)
00142             print response
00143             print '\n', response['0']
00144 
00145     ### Created by Salman Toor ###
00146     elif command == 'unmakeMountpoint':
00147         if len(args) < 1:
00148             print 'Usage: unmakeMountpoint <LN>'
00149         else:
00150             request = {'0': (args[0])}
00151             print 'unmakeMountpoint', request
00152             response = bartender.unmakeMountpoint(request)
00153             print response
00154             print '\n', response['0']
00155     elif command == 'makeMountpoint':
00156         if len(args) < 2:
00157             print 'Usage: makeMountpoint <LN> <URL>'
00158         else:
00159             request = {'0': (args[0], {('states', 'closed') : false}, args[1])}
00160             print 'makeMountpoint', request
00161             response = bartender.makeMountpoint(request)
00162             print response
00163             print '\n', response['0']
00164     ###    ###              
00165     elif command == 'list':
00166         if len(args) < 1:
00167             print 'Usage: list <LN> [<LN> ...]'
00168         else:
00169             request = dict([(str(i), args[i]) for i in range(len(args))])
00170             print 'list', request
00171             response = bartender.list(request,[('entry','')])
00172             
00173             if isinstance(response, str):
00174                 print response
00175             else:    
00176                 print response     
00177                 for rID, (entries, status) in response.items():
00178                     print
00179                     if status == 'found':
00180                         print '%s:' % request[rID]
00181                         for name, (GUID, metadata) in entries.items():
00182                             print '\t%s\t<%s>' % (name, metadata.get(('entry', 'type'),'unknown'))
00183                     else:
00184                         print '%s: %s' % (request[rID], status)
00185     elif command == 'move':
00186         if len(args) < 2:
00187             print 'Usage: move <sourceLN> <targetLN> [preserve]'
00188         else:
00189             sourceLN = args.pop(0)
00190             targetLN = args.pop(0)
00191             preserveOriginal = False
00192             if len(args) > 0:
00193                 if args[0] == 'preserve':
00194                     preserveOriginal = True
00195             request = {'0' : (sourceLN, targetLN, preserveOriginal)}
00196             print 'move', request
00197             response = bartender.move(request)
00198             print response
00199             print '\n', response['0'][0]
00200     elif command == 'modify':
00201         if len(args) < 5:
00202             print 'Usage: modify <LN> <changeType> <section> <property> <value>'
00203         else:
00204             request = {'0' : args}
00205             print 'modify', request
00206             print bartender.modify(request)