* AM's port is 12346 and SM's port is 12347.
[sface.git] / sfadata.py
1 import os
2 import re
3 import time
4 import subprocess
5 from sfa.util.rspecHelper import RSpec
6
7 class SfaData:
8     defaults = { 'SFI_AUTH' : None,
9                  'SFI_USER' : None,
10                  'SFI_SLICE' : 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,
15                  }
16
17     def __init__(self):
18         self.read_config()
19
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))
29
30     def save_config(self):
31         config_keys = SfaData.defaults.keys()
32         configfile = os.path.expanduser("~/.sfi/sfi_config")
33         tmpfile = configfile + ".tmp"
34
35         out = open(tmpfile, "w")
36         for line in open(os.path.expanduser("~/.sfi/sfi_config")):
37             try:
38                 key, val = line.split('=')
39                 key = key.strip()
40                 val = val.strip()
41                 if key in config_keys:
42                     line = "%s = '%s'\n" % (key, getattr(self, key))
43             except:
44                 pass
45             out.write(line)
46         out.close()
47
48         os.unlink(configfile)
49         os.rename(tmpfile, configfile)
50                     
51
52     def getAuthority(self):
53         return SfaData.SFI_AUTH
54
55     def getUser(self):
56         return SfaData.SFI_USER
57
58     def setUser(self, user):
59         SfaData.SFI_USER = user
60
61         # Should probably get authority from user record instead...
62         a = user.split('.')
63         SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
64
65     def getSlice(self):
66         return SfaData.SFI_SLICE
67
68     def setSlice(self, slice):
69         SfaData.SFI_SLICE = slice
70
71     def registry(self):
72         return SfaData.SFI_REGISTRY
73
74     def slicemgr(self):
75         return SfaData.SFI_SM
76
77     def aggmgr(self):
78         return SfaData.SFI_AM
79
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)
84     def trace_end (self):
85         if self.SFAUI_VERBOSE:
86             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
87
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]
93         self.trace_end()
94         return text
95
96     def listChildren(self, hrn):
97         children = []
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]
102         self.trace_end()
103         lines = text.split('\n')
104         for line in lines:
105             if line:
106                 (hrn, kind) = line.split() 
107                 children.append((hrn, kind))
108                 
109         return children
110
111     def getRSpecFromSM(self):
112         return __getRSpec(self.slicemgr)
113
114     def getRSpecFromAM(self):
115         return __getRSpec(self.aggmgr)
116
117     def __getRspec(self, mgr):
118         slice = self.getSlice()
119         # Write RSpec to file for testing.
120         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
121         try:
122             os.remove(filename)
123         except:
124             pass
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)
130         self.trace_end()
131         f = open(filename, "r")
132         xml = f.read()
133         f.close()
134         return xml
135
136     def applyRSpec(self, xml):
137         slice = self.getSlice()
138         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
139         f = open(filename, "w")
140         f.write(xml)
141         f.close()
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)
146         self.trace_end()
147
148 class PlanetLabData(SfaData):
149     def __init__(self):
150         SfaData.__init__(self)
151         self.rspec = None
152
153     def refreshRSpec(self):
154         xml = SfaData.getRSpec(self)
155         self.rspec = RSpec(xml)
156
157     def getRSpec(self):
158         if self.rspec is None:
159             self.refreshRSpec()
160         return self.rspec
161     
162     def applyRSpec(self):
163         xml = self.rspec.toxml()
164         SfaData.applyRSpec(self, xml)
165
166 class PLEData(PlanetLabData):
167     def __init__(self):
168         PlanetLabData.__init__(self)
169         self.SFI_AM = "http://www.planet-lab.eu:12346"
170
171 class PLJData(PlanetLabData):
172     def __init__(self):
173         PlanetLabData.__init__(self)
174         self.SFI_AM = "http://www.planet-lab.jp:12346"
175
176 class ViniData(PlanetLabData):
177     def __init__(self):
178         PlanetLabData.__init__(self)
179         self.SFI_AM = "http://www.vini-veritas.net:12346"
180
181 class GpENIData(PlanetLabData):
182     def __init__(self):
183         PlanetLabData.__init__(self)
184         self.SFI_AM = "http://198.248.241.100:12346"
185
186 class OpenCirrusData(SfaData):
187     def __init__(self):
188         SfaData.__init__(self)
189         self.SFI_REGISTRY = "http://198.55.37.29:12345"
190         self.SFI_AM = "http://198.55.37.29:12346"
191
192     def refreshRSpec(self):
193         xml = SfaData.getRSpec(self)
194         self.rspec = xml
195
196     def getRSpec(self):
197         if self.rspec is None:
198             self.refreshRSpec()
199         return self.rspec
200     
201     def applyRSpec(self):
202         xml = self.rspec
203         SfaData.applyRSpec(self, xml)