5 from sfa.util.rspecHelper import RSpec
8 defaults = { 'SFI_AUTH' : None,
11 'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
12 'SFI_SM' : "http://www.planet-lab.org:12346",
13 'SFAUI_VERBOSE' : False,
19 def read_config(self):
20 filename = os.path.expanduser("~/.sfi/sfi_config")
21 execfile(filename, SfaData.__dict__)
22 for (k,v) in SfaData.defaults.items():
23 if not hasattr(SfaData,k): setattr(SfaData,k,v)
24 if SfaData.SFAUI_VERBOSE:
25 print "After reading config from %s"%filename
26 for (k,v) in SfaData.defaults.items():
27 print "%-20s: %r"%(k,getattr(SfaData,k))
29 def save_config(self):
30 config_keys = SfaData.defaults.keys()
31 configfile = os.path.expanduser("~/.sfi/sfi_config")
32 tmpfile = configfile + ".tmp"
34 out = open(tmpfile, "w")
35 for line in open(os.path.expanduser("~/.sfi/sfi_config")):
37 key, val = line.split('=')
40 if key in config_keys:
41 line = "%s = '%s'\n" % (key, getattr(self, key))
48 os.rename(tmpfile, configfile)
51 def getAuthority(self):
52 return SfaData.SFI_AUTH
55 return SfaData.SFI_USER
57 def setUser(self, user):
58 SfaData.SFI_USER = user
60 # Should probably get authority from user record instead...
62 SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
65 return SfaData.SFI_SLICE
67 def setSlice(self, slice):
68 SfaData.SFI_SLICE = slice
71 return SfaData.SFI_REGISTRY
76 def trace_command (self, command):
77 self._trace=time.time()
78 if self.SFAUI_VERBOSE:
79 print time.strftime('%M:%S'),'Invoking',' '.join(command)
81 if self.SFAUI_VERBOSE:
82 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
84 def getRecord(self, hrn):
85 command = ["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
86 "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
87 self.trace_command(command)
88 text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
92 def listChildren(self, hrn):
94 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
95 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
96 self.trace_command(command)
97 text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
99 lines = text.split('\n')
102 (hrn, kind) = line.split()
103 children.append((hrn, kind))
108 slice = self.getSlice()
109 # Write RSpec to file for testing.
110 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
115 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
116 "-r", self.registry(), "-s", self.slicemgr(), "resources",
117 "-o", filename, slice]
118 self.trace_command(command)
119 subprocess.call(command)
121 f = open(filename, "r")
126 def applyRSpec(self, xml):
127 slice = self.getSlice()
128 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
129 f = open(filename, "w")
132 command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(),
133 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
134 self.trace_command(command)
135 subprocess.call(command)
138 class PlanetLabData(SfaData):
140 SfaData.__init__(self)
143 def refreshRSpec(self):
144 xml = SfaData.getRSpec(self)
145 self.rspec = RSpec(xml)
148 if self.rspec is None:
152 def applyRSpec(self):
153 xml = self.rspec.toxml()
154 SfaData.applyRSpec(self, xml)
156 class PLEData(PlanetLabData):
158 PlanetLabData.__init__(self)
159 self.SFI_SM = "http://www.planet-lab.eu:12346"
161 class PLJData(PlanetLabData):
163 PlanetLabData.__init__(self)
164 self.SFI_SM = "http://www.planet-lab.jp:12346"
166 class ViniData(PlanetLabData):
168 PlanetLabData.__init__(self)
169 self.SFI_SM = "http://www.vini-veritas.net:12346"
171 class GpENIData(PlanetLabData):
173 PlanetLabData.__init__(self)
174 self.SFI_SM = "http://198.248.241.100:12346"
176 class OpenCirrusData(SfaData):
178 SfaData.__init__(self)
179 self.SFI_REGISTRY = "http://198.55.37.29:12345"
180 self.SFI_SM = "http://198.55.37.29:12346"
182 def refreshRSpec(self):
183 xml = SfaData.getRSpec(self)
187 if self.rspec is None:
191 def applyRSpec(self):
193 SfaData.applyRSpec(self, xml)