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