summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Laszlo <tim.laszlo@gmail.com>2011-01-31 21:02:55 -0600
committerTim Laszlo <tim.laszlo@gmail.com>2011-02-10 15:56:46 -0600
commit5fb87fe7b27ab603ba50753b570d32506fa00e6d (patch)
tree77d07e2b6eecfd91a9829e7d9a0ea7436ad518a5
parent25d5b4447c7c8bee0c5fc7e5d2ce7c4f679197eb (diff)
downloadbcfg2-5fb87fe7b27ab603ba50753b570d32506fa00e6d.tar.gz
bcfg2-5fb87fe7b27ab603ba50753b570d32506fa00e6d.tar.bz2
bcfg2-5fb87fe7b27ab603ba50753b570d32506fa00e6d.zip
Metadata: Move client routines to new class
-rw-r--r--src/lib/Server/Plugins/Metadata.py54
1 files changed, 18 insertions, 36 deletions
diff --git a/src/lib/Server/Plugins/Metadata.py b/src/lib/Server/Plugins/Metadata.py
index 64d6668fb..aef65f960 100644
--- a/src/lib/Server/Plugins/Metadata.py
+++ b/src/lib/Server/Plugins/Metadata.py
@@ -404,50 +404,32 @@ class Metadata(Bcfg2.Server.Plugin.Plugin,
def add_client(self, client_name, attribs):
"""Add client to clients.xml."""
- tree = lxml.etree.parse(self.data + "/clients.xml")
- root = tree.getroot()
- element = lxml.etree.Element("Client", name=client_name)
- for key, val in attribs.iteritems():
- element.set(key, val)
- node = self.search_client(client_name, tree)
+ node = self.search_client(client_name, self.clients_xml.xdata)
if node != None:
self.logger.error("Client \"%s\" already exists" % (client_name))
raise MetadataConsistencyError
- root.append(element)
- client_tree = open(self.data + "/clients.xml","w")
- fd = client_tree.fileno()
- while True:
- try:
- fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
- except IOError:
- continue
- else:
- break
- tree.write(client_tree, pretty_print=True)
- fcntl.lockf(fd, fcntl.LOCK_UN)
- client_tree.close()
+
+ element = lxml.etree.SubElement(self.clients_xml.base_xdata.getroot(),
+ "Client", name=client_name)
+ for key, val in attribs.iteritems():
+ element.set(key, val)
+ self.clients_xml.write()
def update_client(self, client_name, attribs):
"""Update a clients attributes."""
- tree = lxml.etree.parse(self.data + "/clients.xml")
- root = tree.getroot()
- node = self.search_client(client_name, tree)
+ node = self.search_client(client_name, self.clients_xml.xdata)
if node == None:
- self.logger.error("Client \"%s\" not found" % (client_name))
+ self.logger.error("Client \"%s\" does not exist" % (client_name))
raise MetadataConsistencyError
- node.attrib.update(attribs)
- client_tree = open(self.data + "/clients.xml","w")
- fd = client_tree.fileno()
- while True:
- try:
- fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
- except IOError:
- continue
- else:
- break
- tree.write(client_tree, pretty_print=True)
- fcntl.lockf(fd, fcntl.LOCK_UN)
- client_tree.close()
+
+ xdict = self.clients_xml.find_xml_for_xpath('.//Client[@name="%s"]' % (node.get('name')))
+ if not xdict:
+ self.logger.error("Unexpected error finding client")
+ raise MetadataConsistencyError
+
+ node = xdict['xquery'][0]
+ [node.set(key, value) for key, value in attribs.items()]
+ self.clients_xml.write_xml(xdict['filename'], xdict['xmltree'])
def HandleEvent(self, event):
"""Handle update events for data files."""