dbf7f30f97803c9a639b0cbc08d0fd4fdbc73143
[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_SM' : "http://www.planet-lab.org:12346",
13                  'SFAUI_VERBOSE' : False,
14                  }
15
16     def __init__(self):
17         self.read_config()
18
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))
28
29     def save_config(self):
30         config_keys = SfaData.defaults.keys()
31         configfile = os.path.expanduser("~/.sfi/sfi_config")
32         tmpfile = configfile + ".tmp"
33
34         out = open(tmpfile, "w")
35         for line in open(os.path.expanduser("~/.sfi/sfi_config")):
36             try:
37                 key, val = line.split('=')
38                 key = key.strip()
39                 val = val.strip()
40                 if key in config_keys:
41                     line = "%s = '%s'\n" % (key, getattr(self, key))
42             except:
43                 pass
44             out.write(line)
45         out.close()
46
47         os.unlink(configfile)
48         os.rename(tmpfile, configfile)
49                     
50
51     def getAuthority(self):
52         return SfaData.SFI_AUTH
53
54     def getUser(self):
55         return SfaData.SFI_USER
56
57     def setUser(self, user):
58         SfaData.SFI_USER = user
59
60         # Should probably get authority from user record instead...
61         a = user.split('.')
62         SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
63
64     def getSlice(self):
65         return SfaData.SFI_SLICE
66
67     def setSlice(self, slice):
68         SfaData.SFI_SLICE = slice
69
70     def registry(self):
71         return SfaData.SFI_REGISTRY
72
73     def slicemgr(self):
74         return SfaData.SFI_SM
75
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)
80     def trace_end (self):
81         if self.SFAUI_VERBOSE:
82             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
83
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]
89         self.trace_end()
90         return text
91
92     def listChildren(self, hrn):
93         children = []
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]
98         self.trace_end()
99         lines = text.split('\n')
100         for line in lines:
101             if line:
102                 (hrn, kind) = line.split() 
103                 children.append((hrn, kind))
104                 
105         return children
106
107     def getRSpec(self):
108         slice = self.getSlice()
109         # Write RSpec to file for testing.
110         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
111         try:
112             os.remove(filename)
113         except:
114             pass
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)
120         self.trace_end()
121         f = open(filename, "r")
122         xml = f.read()
123         f.close()
124         return xml
125
126     def applyRSpec(self, xml):
127         slice = self.getSlice()
128         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
129         f = open(filename, "w")
130         f.write(xml)
131         f.close()
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)
136         self.trace_end()
137
138 class PlanetLabData(SfaData):
139     def __init__(self):
140         SfaData.__init__(self)
141         self.rspec = None
142
143     def refreshRSpec(self):
144         xml = SfaData.getRSpec(self)
145         self.rspec = RSpec(xml)
146
147     def getRSpec(self):
148         if self.rspec is None:
149             self.refreshRSpec()
150         return self.rspec
151     
152     def applyRSpec(self):
153         xml = self.rspec.toxml()
154         SfaData.applyRSpec(self, xml)
155
156 class PLEData(PlanetLabData):
157     def __init__(self):
158         PlanetLabData.__init__(self)
159         self.SFI_SM = "http://www.planet-lab.eu:12346"
160
161 class PLJData(PlanetLabData):
162     def __init__(self):
163         PlanetLabData.__init__(self)
164         self.SFI_SM = "http://www.planet-lab.jp:12346"
165
166 class ViniData(PlanetLabData):
167     def __init__(self):
168         PlanetLabData.__init__(self)
169         self.SFI_SM = "http://www.vini-veritas.net:12346"
170
171 class GpENIData(PlanetLabData):
172     def __init__(self):
173         PlanetLabData.__init__(self)
174         self.SFI_SM = "http://198.248.241.100:12346"
175
176 class OpenCirrusData(SfaData):
177     def __init__(self):
178         SfaData.__init__(self)
179         self.SFI_REGISTRY = "http://198.55.37.29:12345"
180         self.SFI_SM = "http://198.55.37.29:12346"
181
182     def refreshRSpec(self):
183         xml = SfaData.getRSpec(self)
184         self.rspec = xml
185
186     def getRSpec(self):
187         if self.rspec is None:
188             self.refreshRSpec()
189         return self.rspec
190     
191     def applyRSpec(self):
192         xml = self.rspec
193         SfaData.applyRSpec(self, xml)