4 from PyQt4.QtCore import QProcess, QString, QStringList
6 from sfa.util.rspecHelper import RSpec
9 def find_executable(exec_name):
10 """find the given executable in $PATH"""
11 paths = os.getenv("PATH").split(':')
13 exec_path = os.path.join(p, exec_name)
14 if os.path.exists(exec_path):
19 SFI_CMD = find_executable("sfi.py")
20 arguments = QStringList()
22 arguments << QString(c)
24 process.start(SFI_CMD, arguments)
25 process.waitForFinished( 200000 )
26 print process.readAll()
30 defaults = { 'SFI_AUTH' : None,
33 'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
34 'SFI_AM' : "http://www.planet-lab.org:12346",
35 'SFI_SM' : "http://www.planet-lab.org:12347",
36 'SFACE_VERBOSE' : False,
37 'SFACE_DEBUG' : False,
40 # let the UI set the slicename if that was set by user
41 def __init__(self, slice=None):
44 def read_config(self):
45 filename = os.path.expanduser("~/.sfi/sfi_config")
46 execfile(filename, SfaData.__dict__)
47 for (k,v) in SfaData.defaults.items():
48 if not hasattr(SfaData,k): setattr(SfaData,k,v)
49 if SfaData.SFACE_VERBOSE:
50 print "After reading config from %s"%filename
51 for (k,v) in SfaData.defaults.items():
52 print "%-20s: %r"%(k,getattr(SfaData,k))
54 def save_config(self):
55 config_keys = SfaData.defaults.keys()
56 configfile = os.path.expanduser("~/.sfi/sfi_config")
57 tmpfile = configfile + ".tmp"
59 out = open(tmpfile, "w")
60 for line in open(os.path.expanduser("~/.sfi/sfi_config")):
62 key, val = line.split('=')
65 if key in config_keys:
66 line = "%s = '%s'\n" % (key, getattr(self, key))
73 os.rename(tmpfile, configfile)
76 def getAuthority(self):
77 return SfaData.SFI_AUTH
80 return SfaData.SFI_USER
82 def setUser(self, user):
83 SfaData.SFI_USER = user
85 # Should probably get authority from user record instead...
87 SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
90 return SfaData.SFI_SLICE
92 def setSlice(self, slice):
93 SfaData.SFI_SLICE = slice
96 return SfaData.SFI_REGISTRY
102 return SfaData.SFI_AM
104 def trace_command (self, command):
105 self._trace=time.time()
106 if self.SFACE_VERBOSE:
107 print time.strftime('%M:%S'),'Invoking',' '.join(command)
109 def trace_end (self):
110 if self.SFACE_VERBOSE:
111 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
113 def getRecord(self, hrn):
114 command = ["-u", self.getUser(), "-a", self.getAuthority(),
115 "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
116 self.trace_command(command)
121 def listChildren(self, hrn):
123 command=["-u", self.getUser(), "-a", self.getAuthority(),
124 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
125 self.trace_command(command)
128 lines = text.split('\n')
131 (hrn, kind) = line.split()
132 children.append((hrn, kind))
136 def getRSpecFromSM(self):
137 return self.__getRSpec(self.slicemgr())
139 def getRSpecFromAM(self):
140 return self.__getRSpec(self.aggmgr())
142 def __getRSpec(self, mgr):
143 slice = self.getSlice()
144 # Write RSpec to file for testing.
145 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
150 command=["-u", self.getUser(), "-a", self.getAuthority(),
151 "-r", self.registry(), "-s", mgr, "resources",
152 "-o", filename, slice]
153 self.trace_command(command)
156 f = open(filename, "r")
161 def applyRSpec(self, xml):
162 slice = self.getSlice()
163 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
164 f = open(filename, "w")
167 command=["-u", self.getUser(), "-a", self.getAuthority(),
168 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
169 self.trace_command(command)
173 class PlanetLabData(SfaData):
175 SfaData.__init__(self)
178 def refreshRSpec(self):
179 xml = SfaData.getRSpec(self)
180 self.rspec = RSpec(xml)
183 if self.rspec is None:
187 def applyRSpec(self):
188 xml = self.rspec.toxml()
189 SfaData.applyRSpec(self, xml)
191 class PLEData(PlanetLabData):
193 PlanetLabData.__init__(self)
194 self.SFI_AM = "http://www.planet-lab.eu:12346"
196 class PLJData(PlanetLabData):
198 PlanetLabData.__init__(self)
199 self.SFI_AM = "http://www.planet-lab.jp:12346"
201 class ViniData(PlanetLabData):
203 PlanetLabData.__init__(self)
204 self.SFI_AM = "http://www.vini-veritas.net:12346"
206 class GpENIData(PlanetLabData):
208 PlanetLabData.__init__(self)
209 self.SFI_AM = "http://198.248.241.100:12346"
211 class OpenCirrusData(SfaData):
213 SfaData.__init__(self)
214 self.SFI_REGISTRY = "http://198.55.37.29:12345"
215 self.SFI_AM = "http://198.55.37.29:12346"
217 def refreshRSpec(self):
218 xml = SfaData.getRSpec(self)
222 if self.rspec is None:
226 def applyRSpec(self):
228 SfaData.applyRSpec(self, xml)