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()
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 'SFAUI_VERBOSE' : False,
42 def read_config(self):
43 filename = os.path.expanduser("~/.sfi/sfi_config")
44 execfile(filename, SfaData.__dict__)
45 for (k,v) in SfaData.defaults.items():
46 if not hasattr(SfaData,k): setattr(SfaData,k,v)
47 if SfaData.SFAUI_VERBOSE:
48 print "After reading config from %s"%filename
49 for (k,v) in SfaData.defaults.items():
50 print "%-20s: %r"%(k,getattr(SfaData,k))
52 def save_config(self):
53 config_keys = SfaData.defaults.keys()
54 configfile = os.path.expanduser("~/.sfi/sfi_config")
55 tmpfile = configfile + ".tmp"
57 out = open(tmpfile, "w")
58 for line in open(os.path.expanduser("~/.sfi/sfi_config")):
60 key, val = line.split('=')
63 if key in config_keys:
64 line = "%s = '%s'\n" % (key, getattr(self, key))
71 os.rename(tmpfile, configfile)
74 def getAuthority(self):
75 return SfaData.SFI_AUTH
78 return SfaData.SFI_USER
80 def setUser(self, user):
81 SfaData.SFI_USER = user
83 # Should probably get authority from user record instead...
85 SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
88 return SfaData.SFI_SLICE
90 def setSlice(self, slice):
91 SfaData.SFI_SLICE = slice
94 return SfaData.SFI_REGISTRY
100 return SfaData.SFI_AM
102 def trace_command (self, command):
103 self._trace=time.time()
104 if self.SFAUI_VERBOSE:
105 print time.strftime('%M:%S'),'Invoking',' '.join(command)
106 def trace_end (self):
107 if self.SFAUI_VERBOSE:
108 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
110 def getRecord(self, hrn):
111 command = ["-u", self.getUser(), "-a", self.getAuthority(),
112 "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
113 self.trace_command(command)
118 def listChildren(self, hrn):
120 command=["-u", self.getUser(), "-a", self.getAuthority(),
121 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
122 self.trace_command(command)
125 lines = text.split('\n')
128 (hrn, kind) = line.split()
129 children.append((hrn, kind))
133 def getRSpecFromSM(self):
134 return self.__getRSpec(self.slicemgr())
136 def getRSpecFromAM(self):
137 return self.__getRSpec(self.aggmgr())
139 def __getRSpec(self, mgr):
140 slice = self.getSlice()
141 # Write RSpec to file for testing.
142 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
147 command=["-u", self.getUser(), "-a", self.getAuthority(),
148 "-r", self.registry(), "-s", mgr, "resources",
149 "-o", filename, slice]
150 self.trace_command(command)
153 f = open(filename, "r")
158 def applyRSpec(self, xml):
159 slice = self.getSlice()
160 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
161 f = open(filename, "w")
164 command=["-u", self.getUser(), "-a", self.getAuthority(),
165 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
166 self.trace_command(command)
170 class PlanetLabData(SfaData):
172 SfaData.__init__(self)
175 def refreshRSpec(self):
176 xml = SfaData.getRSpec(self)
177 self.rspec = RSpec(xml)
180 if self.rspec is None:
184 def applyRSpec(self):
185 xml = self.rspec.toxml()
186 SfaData.applyRSpec(self, xml)
188 class PLEData(PlanetLabData):
190 PlanetLabData.__init__(self)
191 self.SFI_AM = "http://www.planet-lab.eu:12346"
193 class PLJData(PlanetLabData):
195 PlanetLabData.__init__(self)
196 self.SFI_AM = "http://www.planet-lab.jp:12346"
198 class ViniData(PlanetLabData):
200 PlanetLabData.__init__(self)
201 self.SFI_AM = "http://www.vini-veritas.net:12346"
203 class GpENIData(PlanetLabData):
205 PlanetLabData.__init__(self)
206 self.SFI_AM = "http://198.248.241.100:12346"
208 class OpenCirrusData(SfaData):
210 SfaData.__init__(self)
211 self.SFI_REGISTRY = "http://198.55.37.29:12345"
212 self.SFI_AM = "http://198.55.37.29:12346"
214 def refreshRSpec(self):
215 xml = SfaData.getRSpec(self)
219 if self.rspec is None:
223 def applyRSpec(self):
225 SfaData.applyRSpec(self, xml)