ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ServerConfig.py
Revision: 1.15
Committed: Tue Dec 16 11:29:00 2008 UTC (16 years, 4 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
Changes since 1.14: +37 -15 lines
Log Message:
if [CRAB] use_server=1 and no server_name is set, a list of avilable servers is downloaded from web and one is choosen (via round robin)

File Contents

# User Rev Content
1 farinafa 1.1 from crab_logger import Logger
2     from crab_exceptions import *
3     from crab_util import *
4     import common
5    
6     import urllib
7     import os, time
8    
9     class ServerConfig:
10 spiga 1.2 def __init__(self, serverName):
11 slacapra 1.15 import string
12     serverName = string.lower(serverName)
13 farinafa 1.1 common.logger.debug(5,'Calling ServerConfig')
14 spiga 1.10 # self.url = 'http://cmsdoc.cern.ch/cms/ccs/wm/www/Crab/useful_script/'
15 slacapra 1.15
16 fanzago 1.14 self.url ='https://cmsweb.cern.ch/crabconf/files/'
17 slacapra 1.15 if 'default' in serverName:
18     common.logger.debug(5,'getting serverlist from web')
19     # get a list of available servers
20     serverListFileName ='AvalableServerList'
21     serverListFile = self.getConfig_(serverListFileName)
22     #f = urllib.urlopen('http://www.pd.infn.it/~lacaprar/Computing/'+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'
30     raise CrabException(msg)
31     # clean up empty lines and "\n"
32     serverList=[]
33     [serverList.append(string.strip(it)) for it in tmp if (it.strip() and not it.strip()[0]=="#")]
34    
35     # if more than one, pick up a random one, waiting for something smarter (SiteDB)
36     import random
37     serverName = random.choice(serverList)
38     common.logger.debug(5,'Avaialble servers: '+str(serverList)+' choosen: '+serverName)
39     common.logger.write('Avaialble servers: '+str(serverList)+' choosen: '+serverName)
40     if 'server_' in serverName:
41     configFileName = '%s.conf'%serverName
42 farinafa 1.6 else:
43 slacapra 1.15 configFileName = 'server_%s.conf'%serverName
44 farinafa 1.6
45 slacapra 1.15 localCfg = self.getConfig_(configFileName)
46 farinafa 1.1
47     # parse the localCfg file
48     f = open(localCfg, 'r')
49     l = ''.join( f.readlines() )
50     f.close()
51    
52     if not l:
53     l = str('{}')
54     self.theConfig = eval(l)
55     pass
56    
57     def config(self):
58     return self.theConfig
59    
60     def downloadFile(self, url, destination):
61     try:
62     f = urllib.urlopen(url)
63 farinafa 1.6 data = f.read()
64     if '<!' in data[:2]:
65     raise IOError
66    
67 farinafa 1.1 ff = open(destination, 'w')
68 farinafa 1.6 ff.write(data)
69 farinafa 1.1 ff.close()
70     except IOError:
71     raise CrabException('Cannot download config file '+destination+' from '+self.url)
72    
73 slacapra 1.15 def getConfig_(self, configFileName):
74     url = self.url+configFileName
75     if not os.path.exists(configFileName):
76 farinafa 1.1 common.logger.message('Downloading config files for '+url)
77 slacapra 1.15 self.downloadFile( url, configFileName)
78 farinafa 1.1 else:
79 slacapra 1.15 statinfo = os.stat(configFileName)
80 farinafa 1.1 ## if the file is older then 12 hours it is re-downloaded to update the configuration
81     oldness = 12*3600
82     if (time.time() - statinfo.st_ctime) > oldness:
83     common.logger.message('Downloading config files for '+url)
84 slacapra 1.15 self.downloadFile( url, configFileName)
85 farinafa 1.1 pass
86 slacapra 1.15 return os.getcwd()+'/'+configFileName