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 |
def __init__(self, serverName):
|
11 |
import string
|
12 |
serverName = string.lower(serverName)
|
13 |
common.logger.debug(5,'Calling ServerConfig')
|
14 |
# self.url = 'http://cmsdoc.cern.ch/cms/ccs/wm/www/Crab/useful_script/'
|
15 |
|
16 |
self.url ='https://cmsweb.cern.ch/crabconf/files/'
|
17 |
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 |
else:
|
43 |
configFileName = 'server_%s.conf'%serverName
|
44 |
|
45 |
localCfg = self.getConfig_(configFileName)
|
46 |
|
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 |
data = f.read()
|
64 |
if '<!' in data[:2]:
|
65 |
raise IOError
|
66 |
|
67 |
ff = open(destination, 'w')
|
68 |
ff.write(data)
|
69 |
ff.close()
|
70 |
except IOError:
|
71 |
raise CrabException('Cannot download config file '+destination+' from '+self.url)
|
72 |
|
73 |
def getConfig_(self, configFileName):
|
74 |
url = self.url+configFileName
|
75 |
if not os.path.exists(configFileName):
|
76 |
common.logger.message('Downloading config files for '+url)
|
77 |
self.downloadFile( url, configFileName)
|
78 |
else:
|
79 |
statinfo = os.stat(configFileName)
|
80 |
## 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 |
self.downloadFile( url, configFileName)
|
85 |
pass
|
86 |
return os.getcwd()+'/'+configFileName
|