ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/Proxy.py
(Generate patch)

Comparing COMP/CRAB/python/Proxy.py (file contents):
Revision 1.1 by spiga, Tue Dec 2 15:16:56 2008 UTC vs.
Revision 1.2 by spiga, Thu Dec 4 09:09:52 2008 UTC

# Line 2 | Line 2 | import os,sys
2   import commands
3   import traceback
4   import time
5 <
5 > import re
6   from ProdCommon.BossLite.Common.System import executeCommand
7  
8   class Proxy:
# Line 37 | Line 37 | class Proxy:
37              msg = ('Error %s in getUserProxy search\n' %str(ex))
38              if self.debug : msg += traceback.format_exc()
39              raise Exception(msg)
40
40          return proxy.strip()
41  
42      def getSubject(self, proxy = None):
# Line 82 | Line 81 | class Proxy:
81          if proxy == None: proxy=self.getUserProxy()
82          minTimeLeft=int(Time)*3600 # in seconds
83  
84 <        cmd = 'voms-proxy-info -file '+proxy+' -timeleft '
84 >        cmd = 'voms-proxy-info -file '+proxy+' -timeleft 2>/dev/null'
85  
86 <        out, ret
87 <
88 <        timeLeftLocal =
89 <
86 >        timeLeftLocal,  ret = self.ExecuteCommand(cmd)
87 >      
88 >        if ret != 0 and ret != 1:
89 >            msg = "Error while checking proxy timeleft for %s"%proxy
90 >            raise Exception(msg)
91 >        
92          ## if no valid proxy
93 <        if timeLeftLocal == None or int(timeLeftLocal)<minTimeLeft :
93 >        if not timeLeftLocal :
94 >            valid = False
95 >        elif int(timeLeftLocal)<minTimeLeft :
96              valid = False
94      
97          return valid
98  
99      def renewCredential( self, proxy=None ):
# Line 105 | Line 107 | class Proxy:
107              pass
108          return
109  
110 <    def checkAttribute( self, proxy=None ):
110 >    def checkAttribute( self, proxy=None, vo='cms', group=None, role=None):
111          """
112          """
113 +        valid = True
114          if proxy == None: proxy=self.getUserProxy()
115  
116          ## check first attribute
117 <      #  cmd = 'voms-proxy-info -fqan | head -1'
117 >        cmd = 'export X509_USER_PROXY=%s; voms-proxy-info -fqan 2>/dev/null | head -1'%proxy
118  
119 <      #  reg="/%s/"%self.VO
120 <      #  if self.group:
121 <      #      reg+=self.group
122 <      #  if self.role:
123 <      #      reg+="/Role=%s"%self.role
119 >        reg="/%s/"%vo
120 >        if group:
121 >            reg+=group
122 >        if role:
123 >            reg+="/Role=%s"%role
124  
125 <        return
125 >        att, ret = self.ExecuteCommand(cmd)
126 >
127 >        if ret != 0 :
128 >            msg = "Error while checking proxy timeleft for %s"%proxy
129 >            raise Exception(msg)
130 >
131 >       ## you always have at least  /cms/Role=NULL/Capability=NULL
132 >        if not re.compile(r"^"+reg).search(att):
133 >            if self.debug: print "\tWrong VO group/role.\n"
134 >            valid = False
135 >        return valid
136  
137 <    def ManualRenewCredential( self, VO='cms', group=None, role=None ):
137 >    def ManualRenewCredential( self, proxy=None, vo='cms', group=None, role=None ):
138          """
139          """
127   #     ## you always have at least  /cms/Role=NULL/Capability=NULL
128   #     if not re.compile(r"^"+reg).search(att):
129   #         if not mustRenew:
130   #             common.logger.message( "Valid proxy found, but with wrong VO group/role.\n")
131   #         mustRenew = 1
132        ######
133
134        if not self.checkCredential:
135            cmd = 'voms-proxy-init -voms '+VO
136            if group:
137                cmd += ':/'+VO+'/'+group
138            if role:
139                cmd += '/role='+role
140            cmd += ' -valid 192:00'
141            try:
142                out = os.system(cmd)
143                if (out>0): raise Exception("Unable to create a valid proxy!\n")
144            except:
145                msg = "Unable to create a valid proxy!\n"
146                raise Exception(msg)
140  
141 <    def checkMyProxy( self, proxyServer ):
141 >        cmd = 'voms-proxy-init -voms %s'%vo
142 >
143 >        if group:
144 >            cmd += ':/'+vo+'/'+group
145 >        if role:
146 >            cmd += '/role='+role
147 >        cmd += ' -valid 192:00'
148 >        print cmd
149 >        try:
150 >            out = os.system(cmd)
151 >            if (out>0): raise Exception("Unable to create a valid proxy!\n")
152 >        except:
153 >            msg = "Unable to create a valid proxy!\n"
154 >            raise Exception(msg)
155 >
156 >    def checkMyProxy( self , proxy=None, Time=4 ):
157          """
158          """
159 +        if proxy == None: proxy=self.getUserProxy()
160          ## check the myproxy server
161          valid = True
162 <        cmd = 'myproxy-info -d -s %s'%proxyServer
162 >
163 >        #cmd = 'export X509_USER_PROXY=%s; myproxy-info -d -s %s 2>/dev/null'%(proxy,self.myproxyServer)
164 >        cmd = 'myproxy-info -d -s %s 2>/dev/null'%(self.myproxyServer)
165 >
166 >        out, ret = self.ExecuteCommand(cmd)
167 >        if ret != 0 and ret != 1 :
168 >            msg = "Error while checking myproxy timeleft for %s"%proxy
169 >            raise Exception(msg)
170  
171          if not out:
172 <            print 'No credential delegated to myproxy server %s will do now'%proxyServer
172 >            if self.debug: print '\tNo credential delegated to myproxy server %s will do now'%self.myproxyServer
173              valid = False
174          else:
175              ## minimum time: 5 days
176 <            minTime = 4 * 24 * 3600
176 >            minTime = int(Time) * 24 * 3600
177              ## regex to extract the right information
178              myproxyRE = re.compile("timeleft: (?P<hours>[\\d]*):(?P<minutes>[\\d]*):(?P<seconds>[\\d]*)")
179              for row in out.split("\n"):
# Line 168 | Line 184 | class Proxy:
184                      seconds = g.group("seconds")
185                      timeleft = int(hours)*3600 + int(minutes)*60 + int(seconds)
186                      if timeleft < minTime:
187 <                        print 'Your proxy will expire in:\n\t%s hours %s minutes %s seconds\n'%(hours,minutes,seconds)
187 >                        if self.debug: print '\tYour proxy will expire in:\n\t%s hours %s minutes %s seconds\n'%(hours,minutes,seconds)
188                          valid = False
189          return valid    
190  
191      def ManualRenewMyProxy( self ):
192          """
193          """
194 <        if not self.checkMyProxy:
195 <            cmd = 'myproxy-init -d -n -s '+self.proxyServer
196 <            out = os.system(cmd)
197 <            if (out>0):
182 <                raise CrabException("Unable to delegate the proxy to myproxyserver "+self.proxyServer+" !\n")
183 <            pass
194 >        cmd = 'myproxy-init -d -n -s %s'%self.myproxyServer
195 >        out = os.system(cmd)
196 >        if (out>0):
197 >            raise Exception("Unable to delegate the proxy to myproxyserver %s"%self.myproxyServer+" !\n")
198          return
199    
200      def logonProxy( self ):

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines