add nodeview and nodemodel
[sface.git] / sfadata.py
1 import os
2 import re
3 import time
4 from PyQt4.QtCore import QProcess, QString, QStringList
5
6 from sfa.util.rspecHelper import RSpec
7
8
9 def find_executable(exec_name):
10     """find the given executable in $PATH"""
11     paths = os.getenv("PATH").split(':')
12     for p in paths:
13         exec_path = os.path.join(p, exec_name)
14         if os.path.exists(exec_path):
15             return exec_path
16     return None
17
18 def process(command):
19     SFI_CMD = find_executable("sfi.py")
20     arguments = QStringList()
21     for c in command:
22         arguments << QString(c)
23     process = QProcess()
24     process.start(SFI_CMD, arguments)
25     process.waitForFinished( 200000 )
26     print process.readAll()
27
28
29 class SfaData:
30     defaults = { 'SFI_AUTH' : None,
31                  'SFI_USER' : None,
32                  'SFI_SLICE' : 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,
37                  }
38
39     def __init__(self):
40         self.read_config()
41
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))
51
52     def save_config(self):
53         config_keys = SfaData.defaults.keys()
54         configfile = os.path.expanduser("~/.sfi/sfi_config")
55         tmpfile = configfile + ".tmp"
56
57         out = open(tmpfile, "w")
58         for line in open(os.path.expanduser("~/.sfi/sfi_config")):
59             try:
60                 key, val = line.split('=')
61                 key = key.strip()
62                 val = val.strip()
63                 if key in config_keys:
64                     line = "%s = '%s'\n" % (key, getattr(self, key))
65             except:
66                 pass
67             out.write(line)
68         out.close()
69
70         os.unlink(configfile)
71         os.rename(tmpfile, configfile)
72                     
73
74     def getAuthority(self):
75         return SfaData.SFI_AUTH
76
77     def getUser(self):
78         return SfaData.SFI_USER
79
80     def setUser(self, user):
81         SfaData.SFI_USER = user
82
83         # Should probably get authority from user record instead...
84         a = user.split('.')
85         SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
86
87     def getSlice(self):
88         return SfaData.SFI_SLICE
89
90     def setSlice(self, slice):
91         SfaData.SFI_SLICE = slice
92
93     def registry(self):
94         return SfaData.SFI_REGISTRY
95
96     def slicemgr(self):
97         return SfaData.SFI_SM
98
99     def aggmgr(self):
100         return SfaData.SFI_AM
101
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)
106     def trace_end (self):
107         if self.SFAUI_VERBOSE:
108             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
109
110     def getRecord(self, hrn):
111         command = ["-u", self.getUser(), "-a", self.getAuthority(), 
112                    "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
113         self.trace_command(command)
114         process(command)
115         self.trace_end()
116         return text
117
118     def listChildren(self, hrn):
119         children = []
120         command=["-u", self.getUser(), "-a", self.getAuthority(), 
121                  "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
122         self.trace_command(command)
123         process(command)
124         self.trace_end()
125         lines = text.split('\n')
126         for line in lines:
127             if line:
128                 (hrn, kind) = line.split() 
129                 children.append((hrn, kind))
130                 
131         return children
132
133     def getRSpecFromSM(self):
134         return self.__getRSpec(self.slicemgr())
135
136     def getRSpecFromAM(self):
137         return self.__getRSpec(self.aggmgr())
138
139     def __getRSpec(self, mgr):
140         slice = self.getSlice()
141         # Write RSpec to file for testing.
142         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
143         try:
144             os.remove(filename)
145         except:
146             pass
147         command=["-u", self.getUser(), "-a", self.getAuthority(), 
148                  "-r", self.registry(), "-s", mgr, "resources", 
149                  "-o", filename, slice]
150         self.trace_command(command)
151         process(command)
152         self.trace_end()
153         f = open(filename, "r")
154         xml = f.read()
155         f.close()
156         return xml
157
158     def applyRSpec(self, xml):
159         slice = self.getSlice()
160         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
161         f = open(filename, "w")
162         f.write(xml)
163         f.close()
164         command=["-u", self.getUser(), "-a", self.getAuthority(), 
165                  "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
166         self.trace_command(command)
167         process(command)
168         self.trace_end()
169
170 class PlanetLabData(SfaData):
171     def __init__(self):
172         SfaData.__init__(self)
173         self.rspec = None
174
175     def refreshRSpec(self):
176         xml = SfaData.getRSpec(self)
177         self.rspec = RSpec(xml)
178
179     def getRSpec(self):
180         if self.rspec is None:
181             self.refreshRSpec()
182         return self.rspec
183     
184     def applyRSpec(self):
185         xml = self.rspec.toxml()
186         SfaData.applyRSpec(self, xml)
187
188 class PLEData(PlanetLabData):
189     def __init__(self):
190         PlanetLabData.__init__(self)
191         self.SFI_AM = "http://www.planet-lab.eu:12346"
192
193 class PLJData(PlanetLabData):
194     def __init__(self):
195         PlanetLabData.__init__(self)
196         self.SFI_AM = "http://www.planet-lab.jp:12346"
197
198 class ViniData(PlanetLabData):
199     def __init__(self):
200         PlanetLabData.__init__(self)
201         self.SFI_AM = "http://www.vini-veritas.net:12346"
202
203 class GpENIData(PlanetLabData):
204     def __init__(self):
205         PlanetLabData.__init__(self)
206         self.SFI_AM = "http://198.248.241.100:12346"
207
208 class OpenCirrusData(SfaData):
209     def __init__(self):
210         SfaData.__init__(self)
211         self.SFI_REGISTRY = "http://198.55.37.29:12345"
212         self.SFI_AM = "http://198.55.37.29:12346"
213
214     def refreshRSpec(self):
215         xml = SfaData.getRSpec(self)
216         self.rspec = xml
217
218     def getRSpec(self):
219         if self.rspec is None:
220             self.refreshRSpec()
221         return self.rspec
222     
223     def applyRSpec(self):
224         xml = self.rspec
225         SfaData.applyRSpec(self, xml)