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 '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)
107 def trace_end (self):
108 if self.SFAUI_VERBOSE:
109 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
111 def getRecord(self, hrn):
112 command = ["-u", self.getUser(), "-a", self.getAuthority(),
113 "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
114 self.trace_command(command)
119 def listChildren(self, hrn):
121 command=["-u", self.getUser(), "-a", self.getAuthority(),
122 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
123 self.trace_command(command)
126 lines = text.split('\n')
129 (hrn, kind) = line.split()
130 children.append((hrn, kind))
134 def getRSpecFromSM(self):
135 return self.__getRSpec(self.slicemgr())
137 def getRSpecFromAM(self):
138 return self.__getRSpec(self.aggmgr())
140 def __getRSpec(self, mgr):
141 slice = self.getSlice()
142 # Write RSpec to file for testing.
143 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
148 command=["-u", self.getUser(), "-a", self.getAuthority(),
149 "-r", self.registry(), "-s", mgr, "resources",
150 "-o", filename, slice]
151 self.trace_command(command)
154 f = open(filename, "r")
159 def applyRSpec(self, xml):
160 slice = self.getSlice()
161 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
162 f = open(filename, "w")
165 command=["-u", self.getUser(), "-a", self.getAuthority(),
166 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
167 self.trace_command(command)
171 class PlanetLabData(SfaData):
173 SfaData.__init__(self)
176 def refreshRSpec(self):
177 xml = SfaData.getRSpec(self)
178 self.rspec = RSpec(xml)
181 if self.rspec is None:
185 def applyRSpec(self):
186 xml = self.rspec.toxml()
187 SfaData.applyRSpec(self, xml)
189 class PLEData(PlanetLabData):
191 PlanetLabData.__init__(self)
192 self.SFI_AM = "http://www.planet-lab.eu:12346"
194 class PLJData(PlanetLabData):
196 PlanetLabData.__init__(self)
197 self.SFI_AM = "http://www.planet-lab.jp:12346"
199 class ViniData(PlanetLabData):
201 PlanetLabData.__init__(self)
202 self.SFI_AM = "http://www.vini-veritas.net:12346"
204 class GpENIData(PlanetLabData):
206 PlanetLabData.__init__(self)
207 self.SFI_AM = "http://198.248.241.100:12346"
209 class OpenCirrusData(SfaData):
211 SfaData.__init__(self)
212 self.SFI_REGISTRY = "http://198.55.37.29:12345"
213 self.SFI_AM = "http://198.55.37.29:12346"
215 def refreshRSpec(self):
216 xml = SfaData.getRSpec(self)
220 if self.rspec is None:
224 def applyRSpec(self):
226 SfaData.applyRSpec(self, xml)