layout
[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
107     def trace_end (self):
108         if self.SFAUI_VERBOSE:
109             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
110
111     def getRecord(self, hrn):
112         command = ["-u", self.getUser(), "-a", self.getAuthority(), 
113                    "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
114         self.trace_command(command)
115         process(command)
116         self.trace_end()
117         return text
118
119     def listChildren(self, hrn):
120         children = []
121         command=["-u", self.getUser(), "-a", self.getAuthority(), 
122                  "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
123         self.trace_command(command)
124         process(command)
125         self.trace_end()
126         lines = text.split('\n')
127         for line in lines:
128             if line:
129                 (hrn, kind) = line.split() 
130                 children.append((hrn, kind))
131                 
132         return children
133
134     def getRSpecFromSM(self):
135         return self.__getRSpec(self.slicemgr())
136
137     def getRSpecFromAM(self):
138         return self.__getRSpec(self.aggmgr())
139
140     def __getRSpec(self, mgr):
141         slice = self.getSlice()
142         # Write RSpec to file for testing.
143         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
144         try:
145             os.remove(filename)
146         except:
147             pass
148         command=["-u", self.getUser(), "-a", self.getAuthority(), 
149                  "-r", self.registry(), "-s", mgr, "resources", 
150                  "-o", filename, slice]
151         self.trace_command(command)
152         process(command)
153         self.trace_end()
154         f = open(filename, "r")
155         xml = f.read()
156         f.close()
157         return xml
158
159     def applyRSpec(self, xml):
160         slice = self.getSlice()
161         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
162         f = open(filename, "w")
163         f.write(xml)
164         f.close()
165         command=["-u", self.getUser(), "-a", self.getAuthority(), 
166                  "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
167         self.trace_command(command)
168         process(command)
169         self.trace_end()
170
171 class PlanetLabData(SfaData):
172     def __init__(self):
173         SfaData.__init__(self)
174         self.rspec = None
175
176     def refreshRSpec(self):
177         xml = SfaData.getRSpec(self)
178         self.rspec = RSpec(xml)
179
180     def getRSpec(self):
181         if self.rspec is None:
182             self.refreshRSpec()
183         return self.rspec
184     
185     def applyRSpec(self):
186         xml = self.rspec.toxml()
187         SfaData.applyRSpec(self, xml)
188
189 class PLEData(PlanetLabData):
190     def __init__(self):
191         PlanetLabData.__init__(self)
192         self.SFI_AM = "http://www.planet-lab.eu:12346"
193
194 class PLJData(PlanetLabData):
195     def __init__(self):
196         PlanetLabData.__init__(self)
197         self.SFI_AM = "http://www.planet-lab.jp:12346"
198
199 class ViniData(PlanetLabData):
200     def __init__(self):
201         PlanetLabData.__init__(self)
202         self.SFI_AM = "http://www.vini-veritas.net:12346"
203
204 class GpENIData(PlanetLabData):
205     def __init__(self):
206         PlanetLabData.__init__(self)
207         self.SFI_AM = "http://198.248.241.100:12346"
208
209 class OpenCirrusData(SfaData):
210     def __init__(self):
211         SfaData.__init__(self)
212         self.SFI_REGISTRY = "http://198.55.37.29:12345"
213         self.SFI_AM = "http://198.55.37.29:12346"
214
215     def refreshRSpec(self):
216         xml = SfaData.getRSpec(self)
217         self.rspec = xml
218
219     def getRSpec(self):
220         if self.rspec is None:
221             self.refreshRSpec()
222         return self.rspec
223     
224     def applyRSpec(self):
225         xml = self.rspec
226         SfaData.applyRSpec(self, xml)