typo
[sfa-gui.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         filename = os.path.expanduser("~/.sfi/sfi_config")
18         execfile(filename, SfaData.__dict__)
19         for (k,v) in SfaData.defaults.items():
20             if not hasattr(SfaData,k): setattr(SfaData,k,v)
21         if SfaData.SFAUI_VERBOSE:
22             print "After reading config from %s"%filename
23             for (k,v) in SfaData.defaults.items():
24                 print "%-20s: %r"%(k,getattr(SfaData,k))
25
26     def getAuthority(self):
27         return SfaData.SFI_AUTH
28
29     def getUser(self):
30         return SfaData.SFI_USER
31
32     def setUser(self, user):
33         SfaData.SFI_USER = user
34
35         # Should probably get authority from user record instead...
36         a = user.split('.')
37         SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
38
39     def getSlice(self):
40         return SfaData.SFI_SLICE
41
42     def setSlice(self, slice):
43         SfaData.SFI_SLICE = slice
44
45     def registry(self):
46         return SfaData.SFI_REGISTRY
47
48     def slicemgr(self):
49         return SfaData.SFI_SM
50
51     def trace_command (self, command):
52         self._trace=time.time()
53         if self.SFAUI_VERBOSE:
54             print time.strftime('%M:%S'),'Invoking',' '.join(command)
55     def trace_end (self):
56         if self.SFAUI_VERBOSE:
57             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
58
59     def getRecord(self, hrn):
60         command = ["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
61                    "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
62         self.trace_command(command)
63         text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
64         self.trace_end()
65         return text
66
67     def listChildren(self, hrn):
68         children = []
69         command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
70                  "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
71         self.trace_command(command)
72         text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
73         self.trace_end()
74         lines = text.split('\n')
75         for line in lines:
76             if line:
77                 (hrn, kind) = line.split() 
78                 children.append((hrn, kind))
79                 
80         return children
81
82     def getRSpec(self):
83         slice = self.getSlice()
84         # Write RSpec to file for testing.
85         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
86         try:
87             os.remove(filename)
88         except:
89             pass
90         command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
91                  "-r", self.registry(), "-s", self.slicemgr(), "resources", 
92                  "-o", filename, slice]
93         self.trace_command(command)
94         subprocess.call(command)
95         self.trace_end()
96         f = open(filename, "r")
97         xml = f.read()
98         f.close()
99         return xml
100
101     def applyRSpec(self, xml):
102         slice = self.getSlice()
103         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
104         f = open(filename, "w")
105         f.write(xml)
106         f.close()
107         command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
108                  "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
109         self.trace_command(command)
110         subprocess.call(command)
111         self.trace_end()
112
113 class PlanetLabData(SfaData):
114     def __init__(self):
115         SfaData.__init__(self)
116         self.rspec = None
117
118     def refreshRSpec(self):
119         xml = SfaData.getRSpec(self)
120         self.rspec = RSpec(xml)
121
122     def getRSpec(self):
123         if self.rspec is None:
124             self.refreshRSpec()
125         return self.rspec
126     
127     def applyRSpec(self):
128         xml = self.rspec.toxml()
129         SfaData.applyRSpec(self, xml)
130
131 class PLEData(PlanetLabData):
132     def __init__(self):
133         PlanetLabData.__init__(self)
134         self.SFI_SM = "http://www.planet-lab.eu:12346"
135
136 class PLJData(PlanetLabData):
137     def __init__(self):
138         PlanetLabData.__init__(self)
139         self.SFI_SM = "http://www.planet-lab.jp:12346"
140
141 class ViniData(PlanetLabData):
142     def __init__(self):
143         PlanetLabData.__init__(self)
144         self.SFI_SM = "http://www.vini-veritas.net:12346"
145
146 class GpENIData(PlanetLabData):
147     def __init__(self):
148         PlanetLabData.__init__(self)
149         self.SFI_SM = "http://198.248.241.100:12346"
150
151 class OpenCirrusData(SfaData):
152     def __init__(self):
153         SfaData.__init__(self)
154         self.SFI_REGISTRY = "http://198.55.37.29:12345"
155         self.SFI_SM = "http://198.55.37.29:12346"
156
157     def refreshRSpec(self):
158         xml = SfaData.getRSpec(self)
159         self.rspec = xml
160
161     def getRSpec(self):
162         if self.rspec is None:
163             self.refreshRSpec()
164         return self.rspec
165     
166     def applyRSpec(self):
167         xml = self.rspec
168         SfaData.applyRSpec(self, xml)