1fc3708757aa5d911aebec77b3babaded21a8b50
[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                  'SFACE_VERBOSE' : False,
37                  'SFACE_DEBUG' : False,
38                  }
39
40     # let the UI set the slicename if that was set by user
41     def __init__(self, slice=None):
42         self.read_config()
43
44     def read_config(self):
45         filename = os.path.expanduser("~/.sfi/sfi_config")
46         execfile(filename, SfaData.__dict__)
47         for (k,v) in SfaData.defaults.items():
48             if not hasattr(SfaData,k): setattr(SfaData,k,v)
49         if SfaData.SFACE_VERBOSE:
50             print "After reading config from %s"%filename
51             for (k,v) in SfaData.defaults.items():
52                 print "%-20s: %r"%(k,getattr(SfaData,k))
53
54     def save_config(self):
55         config_keys = SfaData.defaults.keys()
56         configfile = os.path.expanduser("~/.sfi/sfi_config")
57         tmpfile = configfile + ".tmp"
58
59         out = open(tmpfile, "w")
60         for line in open(os.path.expanduser("~/.sfi/sfi_config")):
61             try:
62                 key, val = line.split('=')
63                 key = key.strip()
64                 val = val.strip()
65                 if key in config_keys:
66                     line = "%s = '%s'\n" % (key, getattr(self, key))
67             except:
68                 pass
69             out.write(line)
70         out.close()
71
72         os.unlink(configfile)
73         os.rename(tmpfile, configfile)
74                     
75
76     def getAuthority(self):
77         return SfaData.SFI_AUTH
78
79     def getUser(self):
80         return SfaData.SFI_USER
81
82     def setUser(self, user):
83         SfaData.SFI_USER = user
84
85         # Should probably get authority from user record instead...
86         a = user.split('.')
87         SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
88
89     def getSlice(self):
90         return SfaData.SFI_SLICE
91
92     def setSlice(self, slice):
93         SfaData.SFI_SLICE = slice
94
95     def registry(self):
96         return SfaData.SFI_REGISTRY
97
98     def slicemgr(self):
99         return SfaData.SFI_SM
100
101     def aggmgr(self):
102         return SfaData.SFI_AM
103
104     def trace_command (self, command):
105         self._trace=time.time()
106         if self.SFACE_VERBOSE:
107             print time.strftime('%M:%S'),'Invoking',' '.join(command)
108
109     def trace_end (self):
110         if self.SFACE_VERBOSE:
111             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
112
113     def getRecord(self, hrn):
114         command = ["-u", self.getUser(), "-a", self.getAuthority(), 
115                    "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
116         self.trace_command(command)
117         process(command)
118         self.trace_end()
119         return text
120
121     def listChildren(self, hrn):
122         children = []
123         command=["-u", self.getUser(), "-a", self.getAuthority(), 
124                  "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
125         self.trace_command(command)
126         process(command)
127         self.trace_end()
128         lines = text.split('\n')
129         for line in lines:
130             if line:
131                 (hrn, kind) = line.split() 
132                 children.append((hrn, kind))
133                 
134         return children
135
136     def getRSpecFromSM(self):
137         return self.__getRSpec(self.slicemgr())
138
139     def getRSpecFromAM(self):
140         return self.__getRSpec(self.aggmgr())
141
142     def __getRSpec(self, mgr):
143         slice = self.getSlice()
144         # Write RSpec to file for testing.
145         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
146         try:
147             os.remove(filename)
148         except:
149             pass
150         command=["-u", self.getUser(), "-a", self.getAuthority(), 
151                  "-r", self.registry(), "-s", mgr, "resources", 
152                  "-o", filename, slice]
153         self.trace_command(command)
154         process(command)
155         self.trace_end()
156         f = open(filename, "r")
157         xml = f.read()
158         f.close()
159         return xml
160
161     def applyRSpec(self, xml):
162         slice = self.getSlice()
163         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
164         f = open(filename, "w")
165         f.write(xml)
166         f.close()
167         command=["-u", self.getUser(), "-a", self.getAuthority(), 
168                  "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
169         self.trace_command(command)
170         process(command)
171         self.trace_end()
172
173 class PlanetLabData(SfaData):
174     def __init__(self):
175         SfaData.__init__(self)
176         self.rspec = None
177
178     def refreshRSpec(self):
179         xml = SfaData.getRSpec(self)
180         self.rspec = RSpec(xml)
181
182     def getRSpec(self):
183         if self.rspec is None:
184             self.refreshRSpec()
185         return self.rspec
186     
187     def applyRSpec(self):
188         xml = self.rspec.toxml()
189         SfaData.applyRSpec(self, xml)
190
191 class PLEData(PlanetLabData):
192     def __init__(self):
193         PlanetLabData.__init__(self)
194         self.SFI_AM = "http://www.planet-lab.eu:12346"
195
196 class PLJData(PlanetLabData):
197     def __init__(self):
198         PlanetLabData.__init__(self)
199         self.SFI_AM = "http://www.planet-lab.jp:12346"
200
201 class ViniData(PlanetLabData):
202     def __init__(self):
203         PlanetLabData.__init__(self)
204         self.SFI_AM = "http://www.vini-veritas.net:12346"
205
206 class GpENIData(PlanetLabData):
207     def __init__(self):
208         PlanetLabData.__init__(self)
209         self.SFI_AM = "http://198.248.241.100:12346"
210
211 class OpenCirrusData(SfaData):
212     def __init__(self):
213         SfaData.__init__(self)
214         self.SFI_REGISTRY = "http://198.55.37.29:12345"
215         self.SFI_AM = "http://198.55.37.29:12346"
216
217     def refreshRSpec(self):
218         xml = SfaData.getRSpec(self)
219         self.rspec = xml
220
221     def getRSpec(self):
222         if self.rspec is None:
223             self.refreshRSpec()
224         return self.rspec
225     
226     def applyRSpec(self):
227         xml = self.rspec
228         SfaData.applyRSpec(self, xml)