1 import sys, types, string, os
6 # ============================================================
7 # XML processing and query
10 def lookup(self, uuid):
11 """ lookup returns a new LustreDB instance"""
12 return self._lookup_by_uuid(uuid)
14 def lookup_name(self, name, class_name = ""):
15 """ lookup returns a new LustreDB instance"""
16 return self._lookup_by_name(name, class_name)
18 def lookup_class(self, class_name):
19 """ lookup returns a new LustreDB instance"""
20 return self._lookup_by_class(class_name)
22 def get_val(self, tag, default=None):
23 v = self._get_val(tag)
30 def get_hostaddr(self):
32 list = self.dom_node.getElementsByTagName('hostaddr')
34 ret.append(node.firstChild.data)
38 return self._get_class()
40 def get_val_int(self, tag, default=0):
41 str = self._get_val(tag)
47 raise Lustre.LconfError("text value is not integer: " + str)
49 def get_first_ref(self, tag):
50 """ Get the first uuidref of the type TAG. Only
51 one is expected. Returns the uuid."""
52 uuids = self._get_refs(tag)
57 def get_refs(self, tag):
58 """ Get all the refs of type TAG. Returns list of uuids. """
59 uuids = self._get_refs(tag)
62 def get_all_refs(self):
63 """ Get all the refs. Returns list of uuids. """
64 uuids = self._get_all_refs()
67 def get_lov_tgts(self, tag):
68 """ Returns list of lovtgts. """
69 tgts = self._get_lov_tgts(tag)
72 def nid2server(self, nid, net_type, cluster_id):
73 netlist = self.lookup_class('network')
74 for net_db in netlist:
75 if (net_db.get_val('nid') == nid and
76 net_db.get_val('nettype') == net_type and
77 net_db.get_val('clusterid') == cluster_id):
81 # Find the target_device for target on a node
82 # node->profiles->device_refs->target
83 def get_node_tgt_dev(self, node_name, target_uuid):
84 node_db = self.lookup_name(node_name)
87 return node_db.get_tgt_dev(target_uuid)
89 # get all network uuids for this node
90 def get_networks(self):
92 prof_list = self.get_refs('profile')
93 for prof_uuid in prof_list:
94 prof_db = self.lookup(prof_uuid)
95 net_list = prof_db.get_refs('network')
96 for net_uuid in net_list:
100 def get_active_dev(self, tgtuuid):
101 tgt = self.lookup(tgtuuid)
102 tgt_dev_uuid =tgt.get_first_ref('active')
105 def get_tgt_dev(self, tgtuuid):
106 prof_list = self.get_refs('profile')
107 for prof_uuid in prof_list:
108 prof_db = self.lookup(prof_uuid)
110 panic("profile:", profile, "not found.")
111 for ref_class, ref_uuid in prof_db.get_all_refs():
112 if ref_class in ('osd', 'mdsdev'):
113 devdb = self.lookup(ref_uuid)
114 uuid = devdb.get_first_ref('target')
119 def get_group(self, group):
121 devs = self.lookup_class('mds')
123 if tgt.get_val('group', "") == group:
124 ret.append(tgt.getUUID())
125 devs = self.lookup_class('ost')
127 if tgt.get_val('group', "") == group:
128 ret.append(tgt.getUUID())
131 # Change the current active device for a target
132 def update_active(self, tgtuuid, new_uuid):
133 self._update_active(tgtuuid, new_uuid)
135 def get_version(self):
136 return self.get_val('version')
138 class LustreDB_XML(LustreDB):
139 def __init__(self, dom, root_node):
142 self.root_node = root_node
148 def xmltext(self, dom_node, tag):
149 list = dom_node.getElementsByTagName(tag)
153 if dom_node.firstChild:
154 txt = string.strip(dom_node.firstChild.data)
158 def xmlattr(self, dom_node, attr):
159 return dom_node.getAttribute(attr)
161 def _get_val(self, tag):
162 """a value could be an attribute of the current node
163 or the text value in a child node"""
164 ret = self.xmlattr(self.dom_node, tag)
166 ret = self.xmltext(self.dom_node, tag)
169 def _get_class(self):
170 return self.dom_node.nodeName
172 def get_ref_type(self, ref_tag):
173 res = string.split(ref_tag, '_')
177 # [(ref_class, ref_uuid),]
178 def _get_all_refs(self):
180 for n in self.dom_node.childNodes:
181 if n.nodeType == n.ELEMENT_NODE:
182 ref_uuid = self.xml_get_ref(n)
183 ref_class = self.get_ref_type(n.nodeName)
184 list.append((ref_class, ref_uuid))
189 def _get_refs(self, tag):
190 """ Get all the refs of type TAG. Returns list of uuids. """
192 refname = '%s_ref' % tag
193 reflist = self.dom_node.getElementsByTagName(refname)
195 uuids.append(self.xml_get_ref(r))
198 def _get_lov_tgts(self, tag):
199 """ Get all the refs of type TAG. Returns list of lov_tgts. """
201 tgtlist = self.dom_node.getElementsByTagName(tag)
203 uuidref = tgt.getAttribute('uuidref')
204 index = tgt.getAttribute('index')
205 generation = tgt.getAttribute('generation')
206 active = int(tgt.getAttribute('active'))
207 tgts.append((uuidref, index, generation, active))
210 def xmllookup_by_uuid(self, dom_node, uuid):
211 for n in dom_node.childNodes:
212 if n.nodeType == n.ELEMENT_NODE:
213 if self.xml_get_uuid(n) == uuid:
216 n = self.xmllookup_by_uuid(n, uuid)
220 def _lookup_by_uuid(self, uuid):
221 dom = self. xmllookup_by_uuid(self.root_node, uuid)
223 return LustreDB_XML(dom, self.root_node)
225 def xmllookup_by_name(self, dom_node, name):
226 for n in dom_node.childNodes:
227 if n.nodeType == n.ELEMENT_NODE:
228 if self.xml_get_name(n) == name:
231 n = self.xmllookup_by_name(n, name)
235 def _lookup_by_name(self, name, class_name):
236 dom = self.xmllookup_by_name(self.root_node, name)
238 return LustreDB_XML(dom, self.root_node)
240 def xmllookup_by_class(self, dom_node, class_name):
241 return dom_node.getElementsByTagName(class_name)
243 def _lookup_by_class(self, class_name):
245 domlist = self.xmllookup_by_class(self.root_node, class_name)
247 ret.append(LustreDB_XML(node, self.root_node))
250 def xml_get_name(self, n):
251 return n.getAttribute('name')
254 return self.xml_get_name(self.dom_node)
256 def xml_get_ref(self, n):
257 return n.getAttribute('uuidref')
259 def xml_get_uuid(self, dom_node):
260 return dom_node.getAttribute('uuid')
263 return self.xml_get_uuid(self.dom_node)
265 # Convert routes from the router to a route that will be used
266 # on the local system. The network type and gw are changed to the
267 # interface on the router the local system will connect to.
268 def get_local_routes(self, type, gw):
269 """ Return the routes as a list of tuples of the form:
270 [(type, gw, lo, hi),]"""
272 tbl = self.dom_node.getElementsByTagName('routetbl')
274 routes = t.getElementsByTagName('route')
276 net_type = self.xmlattr(r, 'type')
278 lo = self.xmlattr(r, 'lo')
279 hi = self.xmlattr(r, 'hi')
280 tgt_cluster_id = self.xmlattr(r, 'tgtclusterid')
281 res.append((type, gw, tgt_cluster_id, lo, hi))
284 def get_route_tbl(self):
286 for r in self.dom_node.getElementsByTagName('route'):
287 net_type = self.xmlattr(r, 'type')
288 gw = self.xmlattr(r, 'gw')
289 gw_cluster_id = self.xmlattr(r, 'gwclusterid')
290 tgt_cluster_id = self.xmlattr(r, 'tgtclusterid')
291 lo = self.xmlattr(r, 'lo')
292 hi = self.xmlattr(r, 'hi')
293 ret.append((net_type, gw, gw_cluster_id, tgt_cluster_id, lo, hi))
296 def _update_active(self, tgt, new):
297 raise Lustre.LconfError("updates not implemented for XML")
299 # ================================================================
301 class LustreDB_LDAP(LustreDB):
302 def __init__(self, name, attrs,
305 url = "ldap://localhost",
306 user = "cn=Manager, fs=lustre",
312 self._parent = parent
318 self._base = parent._base
325 self.l = ldap.initialize(self._url)
326 # Set LDAP protocol version used
327 self.l.protocol_version=ldap.VERSION3
328 # user and pw only needed if modifying db
329 self.l.bind_s(self._user, self._pw, ldap.AUTH_SIMPLE);
330 except ldap.LDAPError, e:
331 raise Lustre.LconfError('Unable to connect to ldap server:' + self._url)
334 self._name, self._attrs = self.l.search_s(self._base,
336 except ldap.LDAPError, e:
337 raise Lustre.LconfError("no config found in ldap: %s"
342 def ldap_search(self, filter):
343 """Return list of uuids matching the filter."""
349 for name, attrs in self.l.search_s(dn, ldap.SCOPE_ONELEVEL,
351 for v in attrs['uuid']:
353 except ldap.NO_SUCH_OBJECT, e:
355 except ldap.LDAPError, e:
356 print e # FIXME: die here?
359 ret.append(self._lookup_by_uuid(uuid))
362 def _lookup_by_name(self, name, class_name):
363 list = self.ldap_search("lustreName=%s" %(name))
368 def _lookup_by_class(self, class_name):
369 return self.ldap_search("objectclass=%s" %(string.upper(class_name)))
371 def _lookup_by_uuid(self, uuid):
373 dn = "uuid=%s,%s" % (uuid, self._base)
376 for name, attrs in self.l.search_s(dn, ldap.SCOPE_BASE,
378 ret = LustreDB_LDAP(name, attrs, parent = self)
380 except ldap.NO_SUCH_OBJECT, e:
381 pass # just return empty list
382 except ldap.LDAPError, e:
383 print e # FIXME: die here?
387 def _get_val(self, k):
389 if self._attrs.has_key(k):
391 if type(v) == types.ListType:
397 def _get_class(self):
398 return string.lower(self._attrs['objectClass'][0])
400 def get_ref_type(self, ref_tag):
404 # [(ref_class, ref_uuid),]
405 def _get_all_refs(self):
407 for k in self._attrs.keys():
408 if re.search('.*Ref', k):
409 for uuid in self._attrs[k]:
410 ref_class = self.get_ref_type(k)
411 list.append((ref_class, uuid))
414 def _get_refs(self, tag):
415 """ Get all the refs of type TAG. Returns list of uuids. """
417 refname = '%sRef' % tag
418 if self._attrs.has_key(refname):
419 return self._attrs[refname]
423 return self._get_val('lustreName')
426 return self._get_val('uuid')
428 def get_route_tbl(self):
431 def _update_active(self, tgtuuid, newuuid):
432 """Return list of uuids matching the filter."""
434 dn = "uuid=%s,%s" %(tgtuuid, self._base)
438 self.l.modify_s(dn, [(ldap.MOD_REPLACE, "activeRef", newuuid)])
439 except ldap.NO_SUCH_OBJECT, e:
441 except ldap.LDAPError, e:
442 print e # FIXME: die here?