Revision 93384844 lib/ssconf.py
b/lib/ssconf.py | ||
---|---|---|
112 | 112 |
mode=0600) |
113 | 113 |
|
114 | 114 |
|
115 |
class SimpleStore(object): |
|
116 |
"""Interface to static cluster data. |
|
117 |
|
|
118 |
This is different that the config.ConfigWriter and |
|
119 |
SimpleConfigReader classes in that it holds data that will always be |
|
120 |
present, even on nodes which don't have all the cluster data. |
|
121 |
|
|
122 |
Other particularities of the datastore: |
|
123 |
- keys are restricted to predefined values |
|
124 |
|
|
125 |
""" |
|
126 |
_SS_FILEPREFIX = "ssconf_" |
|
127 |
_VALID_KEYS = ( |
|
128 |
constants.SS_CLUSTER_NAME, |
|
129 |
constants.SS_FILE_STORAGE_DIR, |
|
130 |
constants.SS_MASTER_IP, |
|
131 |
constants.SS_MASTER_NETDEV, |
|
132 |
constants.SS_MASTER_NODE, |
|
133 |
constants.SS_NODE_LIST, |
|
134 |
) |
|
135 |
_MAX_SIZE = 131072 |
|
136 |
|
|
137 |
def __init__(self, cfg_location=None): |
|
138 |
if cfg_location is None: |
|
139 |
self._cfg_dir = constants.DATA_DIR |
|
140 |
else: |
|
141 |
self._cfg_dir = cfg_location |
|
142 |
|
|
143 |
def KeyToFilename(self, key): |
|
144 |
"""Convert a given key into filename. |
|
145 |
|
|
146 |
""" |
|
147 |
if key not in self._VALID_KEYS: |
|
148 |
raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'" |
|
149 |
% str(key)) |
|
150 |
|
|
151 |
filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key |
|
152 |
return filename |
|
153 |
|
|
154 |
def _ReadFile(self, key): |
|
155 |
"""Generic routine to read keys. |
|
156 |
|
|
157 |
This will read the file which holds the value requested. Errors |
|
158 |
will be changed into ConfigurationErrors. |
|
159 |
|
|
160 |
""" |
|
161 |
filename = self.KeyToFilename(key) |
|
162 |
try: |
|
163 |
fh = file(filename, 'r') |
|
164 |
try: |
|
165 |
data = fh.read(self._MAX_SIZE) |
|
166 |
data = data.rstrip('\n') |
|
167 |
finally: |
|
168 |
fh.close() |
|
169 |
except EnvironmentError, err: |
|
170 |
raise errors.ConfigurationError("Can't read from the ssconf file:" |
|
171 |
" '%s'" % str(err)) |
|
172 |
return data |
|
173 |
|
|
174 |
def GetFileList(self): |
|
175 |
"""Return the list of all config files. |
|
176 |
|
|
177 |
This is used for computing node replication data. |
|
178 |
|
|
179 |
""" |
|
180 |
return [self.KeyToFilename(key) for key in self._VALID_KEYS] |
|
181 |
|
|
182 |
def GetClusterName(self): |
|
183 |
"""Get the cluster name. |
|
184 |
|
|
185 |
""" |
|
186 |
return self._ReadFile(constants.SS_CLUSTER_NAME) |
|
187 |
|
|
188 |
def GetFileStorageDir(self): |
|
189 |
"""Get the file storage dir. |
|
190 |
|
|
191 |
""" |
|
192 |
return self._ReadFile(constants.SS_FILE_STORAGE_DIR) |
|
193 |
|
|
194 |
def GetMasterIP(self): |
|
195 |
"""Get the IP of the master node for this cluster. |
|
196 |
|
|
197 |
""" |
|
198 |
return self._ReadFile(constants.SS_MASTER_IP) |
|
199 |
|
|
200 |
def GetMasterNetdev(self): |
|
201 |
"""Get the netdev to which we'll add the master ip. |
|
202 |
|
|
203 |
""" |
|
204 |
return self._ReadFile(constants.SS_MASTER_NETDEV) |
|
205 |
|
|
206 |
def GetMasterNode(self): |
|
207 |
"""Get the hostname of the master node for this cluster. |
|
208 |
|
|
209 |
""" |
|
210 |
return self._ReadFile(constants.SS_MASTER_NODE) |
|
211 |
|
|
212 |
def GetNodeList(self): |
|
213 |
"""Return the list of cluster nodes. |
|
214 |
|
|
215 |
""" |
|
216 |
data = self._ReadFile(constants.SS_NODE_LIST) |
|
217 |
nl = data.splitlines(False) |
|
218 |
return nl |
|
219 |
|
|
220 |
|
|
115 | 221 |
def _SsconfPath(name): |
116 | 222 |
if not RE_VALID_SSCONF_NAME.match(name): |
117 | 223 |
raise errors.ParameterError("Invalid ssconf name: %s" % name) |
... | ... | |
150 | 256 |
|
151 | 257 |
""" |
152 | 258 |
if ss is None: |
153 |
ss = SimpleConfigReader()
|
|
259 |
ss = SimpleStore()
|
|
154 | 260 |
return ss.GetMasterNode(), utils.HostInfo().name |
155 | 261 |
|
156 | 262 |
|
Also available in: Unified diff