Back to index

nordugrid-arc-nox  1.1.0~rc6
gateway.py
Go to the documentation of this file.
00001 """
00002 Gateway Component of ARC1 storage System. 
00003 
00004 Service Class = GatewayService
00005 Worker Class  = Gateway
00006 
00007 Author: Salman Zubair Toor
00008 email: salman.toor@it.uu.se
00009 
00010 """
00011 import arc
00012 import time
00013 from arcom import get_child_nodes, datapoint_from_url
00014 from arcom.service import gateway_uri, true, create_response
00015 from arcom.service import Service 
00016 import commands
00017 import os
00018 import base64
00019 from arcom.logger import Logger
00020 log = Logger(arc.Logger(arc.Logger_getRootLogger(), 'Bartender.gateway.Gateway'))
00021 
00022 class Gateway:
00023 
00024     def __init__(self,cfg):
00025         #print "Gateway constructor..."
00026         self.service_name = 'Gateway'
00027         self.cfg = cfg
00028         self.proxy_store = str(self.cfg.Get('ProxyStore'))
00029         self.ca_dir = str(self.cfg.Get('CACertificatesDir'))
00030         if len(self.proxy_store) == 0:
00031            log.msg(arc.VERBOSE,'proxy store is not accessable.')
00032          
00033     def get(self, auth ,sourceURL, flags):
00034         response = {}  
00035         status = ''  
00036         protocol = ''
00037         proxyfile = base64.b64encode(auth.get_identity()) 
00038         if len(self.proxy_store) == 0:
00039            log.msg(arc.VERBOSE,'proxy store is not accessable.')
00040         filepath = self.proxy_store+'/'+proxyfile+'.proxy'
00041         if os.path.isfile(filepath):
00042             url = arc.URL(sourceURL);
00043             handle = arc.DataHandle(url);
00044             handle.__deref__().AssignCredentials(filepath,'','',self.ca_dir) 
00045             (files, status) = handle.__deref__().ListFiles(True);
00046             if files:
00047                 status  = 'successful'
00048                 for file in files:
00049                     #print file.GetName()
00050                     if sourceURL[:6] == 'gsiftp':
00051                         protocol = 'gridftp'
00052                     elif sourceURL[:3] == 'srm':
00053                         protocol = 'srm'
00054                     response[file.GetName()] = {'turl':sourceURL,'status': status,'protocol':protocol}
00055             else:
00056                 status = 'failed: cannot access file'
00057                 response[url] = {'turl':'','status': status,'protocol':''}
00058         else:
00059             status = 'cannot find valid credentials'
00060             response[url] = {'turl':'','status': status,'protocol':''}
00061         log.msg(arc.VERBOSE,'get response: %s',response)
00062         return response
00063             
00064     def put(self, auth, url, flags):
00065         response = {}
00066         if url[:6] == 'gsiftp':
00067             protocol = 'gridftp'
00068             status = 'done'
00069         elif url[:3] == 'srm':
00070             protocol = 'srm'
00071             status = 'done'
00072         else:
00073             protocol = 'unkonwn'
00074             status = 'failed' 
00075         response[url] = {'turl':url,'status':status, 'protocol':protocol}
00076         return response
00077 
00078     def list(self, auth, url, flags=''):
00079         """Input arguments
00080         URL of the file or directory """
00081         response = {}
00082         tmpList = []
00083         longlisting = '-l' in flags
00084         status = ''
00085         if url.startswith('gsiftp'):
00086             protocol = 'gridftp'
00087         elif url.startswith('srm'):
00088             protocol = 'srm'
00089         else:
00090             protocol = 'unkonwn'
00091         if len(self.proxy_store) == 0:
00092             log.msg(arc.VERBOSE,'proxy store is not accessable.')
00093         if protocol != 'unknown':
00094             proxyfile = base64.b64encode(auth.get_identity())
00095             filepath = self.proxy_store + '/' + proxyfile + '.proxy'
00096             if os.path.isfile(filepath):
00097                 dp = datapoint_from_url(url)
00098                 dp.AssignCredentials(filepath, '', '', self.ca_dir)
00099                 (files, stat) = dp.ListFiles(longlisting)
00100                 if files:
00101                     status = 'found'
00102                     for f in files:
00103                         if longlisting:
00104                             #print f.GetName(), " ", f.GetSize(), " ", f.GetCreated()
00105                             if (f.GetType() == 1):
00106                                 type = 'file'
00107                             elif (f.GetType() == 2):
00108                                 type = 'dir'
00109                             else:
00110                                 type = 'known'       
00111                             tmpList.append(f.GetName() + ':' + str(f.GetSize()) + ':' + type + '\n')
00112                         else:
00113                             tmpList.append(f.GetName())
00114                 else:
00115                     status = 'Cannot access external store. Reason: %s' % str(stat)
00116             else:
00117                 status = 'Your proxy cannot be found. Please delegate your credentials!'
00118         response[url] = {'list': tmpList, 'status': status, 'protocol': protocol}
00119         log.msg(arc.VERBOSE, 'list response: %s', response)
00120         return response
00121         
00122     def remove(self, auth, url, flags):
00123         """ remove file or direcotory """
00124         response = {}
00125         protocol = ''
00126         status = ''
00127         if url[:6] == 'gsiftp':
00128             protocol = 'gridftp'
00129         elif url[:3] == 'srm':
00130             protocol = 'srm'
00131         else:
00132             protocol = 'unkonwn'
00133         if len(self.proxy_store) == 0:
00134             status = 'failed' 
00135             log.msg(arc.VERBOSE,'proxy store is not accessable.')
00136         if protocol != 'unknown':
00137             proxyfile = base64.b64encode(auth.get_identity())
00138             filepath = self.proxy_store+'/'+proxyfile+'.proxy'
00139             if os.path.isfile(filepath):
00140                 externalURL = arc.URL(url)
00141                 handle = arc.DataHandle(externalURL)
00142                 handle.__deref__().AssignCredentials(filepath,'','',self.ca_dir)
00143                 status = handle.__deref__().Remove()
00144                 #status = 'successful'
00145         else:
00146             status = 'failed'  
00147         response[url]={'status':str(status),'protocol':protocol}
00148         #print "File or directory removed"     
00149         return response