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

Comparing COMP/CRAB/python/ServerConfig.py (file contents):
Revision 1.24 by spiga, Wed Oct 14 06:28:07 2009 UTC vs.
Revision 1.25 by spiga, Sun Jan 17 17:31:36 2010 UTC

# Line 1 | Line 1
1   from crab_exceptions import *
2   from crab_util import *
3   import common
4 <
5 < import urllib
4 > from Downloader import Downloader
5   import os, time
6  
7   class ServerConfig:
# Line 11 | Line 10 | class ServerConfig:
10          serverName = string.lower(serverName)
11          common.logger.debug('Calling ServerConfig '+serverName)
12  
13 <        self.url ='http://cmsdoc.cern.ch/cms/LCG/crab/config/'
14 <      #  self.url ='https://cmsweb.cern.ch/crabconf/'
13 >        url ='http://cmsdoc.cern.ch/cms/LCG/crab/config/'
14 >
15 >        downloader = Downloader(url, os.getcwd())
16 >          
17  
18 <        # self.url ='http://www.pd.infn.it/~lacaprar/Computing/'
18 >   def config(self):
19 >        """
20 >        """
21          if 'default' in  serverName:
22 <            common.logger.debug('getting serverlist from web')
20 <            # get a list of available servers
21 <            serverListFileName ='AvalableServerList'
22 <            serverListFile = self.getConfig_(serverListFileName)
23 <            # parse the localCfg file
24 <            f = open(serverListFile, 'r')
25 <            tmp = f.readlines()
26 <            f.close()
27 <            if not tmp:
28 <                msg = 'List of avalable Server '+serverListFileName+' from '+self.url+' is empty\n'
29 <                msg += 'Please report to CRAB feedback hypernews hn-cms-crabFeedback@cern.ch'
30 <                raise CrabException(msg)
31 <            # clean up empty lines and comments
32 <            serverList=[]
33 <            [serverList.append(string.split(string.strip(it))) for it in tmp if (it.strip() and not it.strip()[0]=="#")]
34 <            common.logger.debug('All avaialble servers: '+str(serverList))
35 <
36 <            # select servers from client version
37 <            compatibleServerList=[]
38 <            for s in serverList:
39 <                vv=string.split(s[1],'-')
40 <                if len(vv[0])==0: vv[0]='0.0.0'
41 <                if len(vv[1])==0: vv[1]='99.99.99'
42 <                for i in 0,1:
43 <                    tmp=[]
44 <                    [tmp.append(int(t)) for t in vv[i].split('.')]
45 <                    vv[i]=tuple(tmp)
46 <                
47 <                if vv[0]<=common.prog_version and common.prog_version<=vv[1] and common.scheduler.name()==string.lower(s[2]):
48 <                    compatibleServerList.append(s[0])
49 <
50 <            common.logger.debug('All avaialble servers compatible with %s: '%common.prog_version_str +str(serverList))
51 <            if len(compatibleServerList)==0:
52 <                msg = "No compatible server available with client version %s\n"%common.prog_version_str
53 <                msg += "Exiting"
54 <                raise CrabException(msg)
55 <            # if more than one, pick up a random one, waiting for something smarter (SiteDB)
56 <            import random
57 <            serverName = random.choice(compatibleServerList)
58 <            common.logger.debug('Avaialble servers: '+str(compatibleServerList)+' choosen: '+serverName)
22 >            serverName = self.selectServer()
23          if 'server_' in serverName:
24              configFileName = '%s.conf'%serverName
25          else:
26              configFileName = 'server_%s.conf'%serverName
27  
28 <        localCfg = self.getConfig_(configFileName)
28 >        serverConfig = downloader.config(configFileName)
29 >
30 >        if not serverConfig:
31 >            serverConfig = {}
32 >        serverConfig['serverGenericName']=serverName
33 >
34 >        return serveConfig
35 >
36 >    def selectServer(self):
37 >        """
38 >        """
39 >        common.logger.debug('getting serverlist from web')
40 >        # get a list of available servers
41 >        serverListFileName ='AvalableServerList'
42 >
43 >        ## downloader
44 >        serverListFile = downloader.config(serverListFileName)
45 >
46 >        if not serverListFile:
47 >            msg = 'List of avalable Server '+serverListFileName+' from '+self.url+' is empty\n'
48 >            msg += 'Please report to CRAB feedback hypernews hn-cms-crabFeedback@cern.ch'
49 >            raise CrabException(msg)
50 >        # clean up empty lines and comments
51 >        serverList=[]
52 >        [serverList.append(string.split(string.strip(it))) for it in serverListFile if (it.strip() and not it.strip()[0]=="#")]
53 >        common.logger.debug('All avaialble servers: '+str(serverList))
54 >
55 >        # select servers from client version
56 >        compatibleServerList=[]
57 >        for s in serverList:
58 >            vv=string.split(s[1],'-')
59 >            if len(vv[0])==0: vv[0]='0.0.0'
60 >            if len(vv[1])==0: vv[1]='99.99.99'
61 >            for i in 0,1:
62 >                tmp=[]
63 >                [tmp.append(int(t)) for t in vv[i].split('.')]
64 >                vv[i]=tuple(tmp)
65 >            
66 >            if vv[0]<=common.prog_version and common.prog_version<=vv[1] and common.scheduler.name()==string.lower(s[2]):
67 >                compatibleServerList.append(s[0])
68 >
69 >        common.logger.debug('All avaialble servers compatible with %s: '%common.prog_version_str +str(serverList))
70 >        if len(compatibleServerList)==0:
71 >            msg = "No compatible server available with client version %s\n"%common.prog_version_str
72 >            msg += "Exiting"
73 >            raise CrabException(msg)
74 >
75 >        # if more than one, pick up a random one, waiting for something smarter (SiteDB)
76 >        import random
77 >        serverName = random.choice(compatibleServerList)
78 >        common.logger.debug('Avaialble servers: '+str(compatibleServerList)+' choosen: '+serverName)
79  
80 <        # parse the localCfg file
67 <        f = open(localCfg, 'r')
68 <        l = ''.join( f.readlines() )
69 <        f.close()
70 <        
71 <        if not l:
72 <            l = str('{}')
73 <        self.theConfig = eval(l)
74 <        self.theConfig['serverGenericName']=serverName
75 <        pass
76 <        
77 <    def config(self):
78 <        return self.theConfig
79 <
80 <    def downloadFile(self, url, destination):
81 <        try:
82 <            f = urllib.urlopen(url)
83 <            data = f.read()
84 <            if '<!' in data[:2]:
85 <                raise IOError
86 <
87 <            ff = open(destination, 'w')
88 <            ff.write(data)
89 <            ff.close()
90 <        except IOError:
91 <            raise CrabException('Cannot download config file '+destination+' from '+self.url)
92 <
93 <    def getConfig_(self, configFileName):
94 <        url = self.url+configFileName
95 <        if not os.path.exists(configFileName):
96 <            common.logger.info('Downloading config files for '+url)
97 <            self.downloadFile( url, configFileName)
98 <        else:
99 <            statinfo = os.stat(configFileName)
100 <            ## if the file is older then 12 hours it is re-downloaded to update the configuration
101 <            oldness = 12*3600
102 <            if (time.time() - statinfo.st_ctime) > oldness:
103 <                common.logger.info('Downloading config files for '+url)
104 <                self.downloadFile( url, configFileName)
105 <            pass
106 <        return os.getcwd()+'/'+configFileName
80 >        return serverName

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines