5 from sfa.util.rspecHelper import RSpec
8 defaults = { 'SFI_AUTH' : None,
11 'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
12 'SFI_AM' : "http://www.planet-lab.org:12346",
13 'SFI_SM' : "http://www.planet-lab.org:12347",
14 'SFAUI_VERBOSE' : False,
20 def read_config(self):
21 filename = os.path.expanduser("~/.sfi/sfi_config")
22 execfile(filename, SfaData.__dict__)
23 for (k,v) in SfaData.defaults.items():
24 if not hasattr(SfaData,k): setattr(SfaData,k,v)
25 if SfaData.SFAUI_VERBOSE:
26 print "After reading config from %s"%filename
27 for (k,v) in SfaData.defaults.items():
28 print "%-20s: %r"%(k,getattr(SfaData,k))
30 def save_config(self):
31 config_keys = SfaData.defaults.keys()
32 configfile = os.path.expanduser("~/.sfi/sfi_config")
33 tmpfile = configfile + ".tmp"
35 out = open(tmpfile, "w")
36 for line in open(os.path.expanduser("~/.sfi/sfi_config")):
38 key, val = line.split('=')
41 if key in config_keys:
42 line = "%s = '%s'\n" % (key, getattr(self, key))
49 os.rename(tmpfile, configfile)
52 def getAuthority(self):
53 return SfaData.SFI_AUTH
56 return SfaData.SFI_USER
58 def setUser(self, user):
59 SfaData.SFI_USER = user
61 # Should probably get authority from user record instead...
63 SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
66 return SfaData.SFI_SLICE
68 def setSlice(self, slice):
69 SfaData.SFI_SLICE = slice
72 return SfaData.SFI_REGISTRY
80 def trace_command (self, command):
81 self._trace=time.time()
82 if self.SFAUI_VERBOSE:
83 print time.strftime('%M:%S'),'Invoking',' '.join(command)
85 if self.SFAUI_VERBOSE:
86 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
88 def getRecord(self, hrn):
89 command = ["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
90 "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
91 self.trace_command(command)
92 text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
96 def listChildren(self, hrn):
98 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
99 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
100 self.trace_command(command)
101 text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
103 lines = text.split('\n')
106 (hrn, kind) = line.split()
107 children.append((hrn, kind))
111 def getRSpecFromSM(self):
112 return __getRSpec(self.slicemgr)
114 def getRSpecFromAM(self):
115 return __getRSpec(self.aggmgr)
117 def __getRspec(self, mgr):
118 slice = self.getSlice()
119 # Write RSpec to file for testing.
120 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
125 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
126 "-r", self.registry(), "-s", mgr, "resources",
127 "-o", filename, slice]
128 self.trace_command(command)
129 subprocess.call(command)
131 f = open(filename, "r")
136 def applyRSpec(self, xml):
137 slice = self.getSlice()
138 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
139 f = open(filename, "w")
142 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
143 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
144 self.trace_command(command)
145 subprocess.call(command)
148 class PlanetLabData(SfaData):
150 SfaData.__init__(self)
153 def refreshRSpec(self):
154 xml = SfaData.getRSpec(self)
155 self.rspec = RSpec(xml)
158 if self.rspec is None:
162 def applyRSpec(self):
163 xml = self.rspec.toxml()
164 SfaData.applyRSpec(self, xml)
166 class PLEData(PlanetLabData):
168 PlanetLabData.__init__(self)
169 self.SFI_AM = "http://www.planet-lab.eu:12346"
171 class PLJData(PlanetLabData):
173 PlanetLabData.__init__(self)
174 self.SFI_AM = "http://www.planet-lab.jp:12346"
176 class ViniData(PlanetLabData):
178 PlanetLabData.__init__(self)
179 self.SFI_AM = "http://www.vini-veritas.net:12346"
181 class GpENIData(PlanetLabData):
183 PlanetLabData.__init__(self)
184 self.SFI_AM = "http://198.248.241.100:12346"
186 class OpenCirrusData(SfaData):
188 SfaData.__init__(self)
189 self.SFI_REGISTRY = "http://198.55.37.29:12345"
190 self.SFI_AM = "http://198.55.37.29:12346"
192 def refreshRSpec(self):
193 xml = SfaData.getRSpec(self)
197 if self.rspec is None:
201 def applyRSpec(self):
203 SfaData.applyRSpec(self, xml)