diff options
author | Narayan Desai <desai@mcs.anl.gov> | 2009-12-23 19:57:42 +0000 |
---|---|---|
committer | Narayan Desai <desai@mcs.anl.gov> | 2009-12-23 19:57:42 +0000 |
commit | 7abcf2f24ecb1fdb75db341bffa2451f081aca6d (patch) | |
tree | c1f8143869ec1c06ac2c312249afcb7e17325bdf /src/lib/Server | |
parent | 9f8578f99ccc3d2944ef5d3d8a1385d351743f59 (diff) | |
download | bcfg2-7abcf2f24ecb1fdb75db341bffa2451f081aca6d.tar.gz bcfg2-7abcf2f24ecb1fdb75db341bffa2451f081aca6d.tar.bz2 bcfg2-7abcf2f24ecb1fdb75db341bffa2451f081aca6d.zip |
Metadata: handle errors more robustly, and perform clients.xml wranging in new file
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5632 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server')
-rw-r--r-- | src/lib/Server/Plugins/Metadata.py | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/lib/Server/Plugins/Metadata.py b/src/lib/Server/Plugins/Metadata.py index 4468cd990..d92168dd3 100644 --- a/src/lib/Server/Plugins/Metadata.py +++ b/src/lib/Server/Plugins/Metadata.py @@ -4,6 +4,7 @@ __revision__ = '$Revision$' import copy import fcntl import lxml.etree +import os import socket import time import Bcfg2.Server.Plugin @@ -472,13 +473,11 @@ class Metadata(Bcfg2.Server.Plugin.Plugin, def write_back_clients(self): '''Write changes to client.xml back to disk''' try: - datafile = open("%s/%s" % (self.data, 'clients.xml'), 'w') + datafile = open("%s/%s" % (self.data, 'clients.xml.new'), 'w') except IOError: - self.logger.error("Failed to write clients.xml") + self.logger.error("Failed to write clients.xml.new") raise MetadataRuntimeError - fd = datafile.fileno() - while self.locked(fd) == True: - pass + # prep data dataroot = self.clientdata_original.getroot() if hasattr(dataroot, 'iter'): items = dataroot.iter() @@ -488,9 +487,26 @@ class Metadata(Bcfg2.Server.Plugin.Plugin, # no items have text data of any sort item.tail = None item.text = None - datafile.write(lxml.etree.tostring(dataroot, pretty_print=True)) - fcntl.lockf(fd, fcntl.LOCK_UN) - datafile.close() + newcontents = lxml.etree.tostring(dataroot, pretty_print=True) + + fd = datafile.fileno() + while self.locked(fd) == True: + pass + try: + datafile.write(newcontents) + except: + fcntl.lockf(fd, fcntl.LOCK_UN) + self.logger.error("Metadata: Failed to write new clients data to clients.xml.new", exc_info=1) + os.unlink("%s/%s" % (self.data, "clients.xml.new")) + raise MetadataRuntimeError + datafile.close() + + try: + os.rename("%s/%s" % (self.data, 'clients.xml.new'), + "%s/%s" % (self.data, 'clients.xml')) + except: + self.logger.error("Metadata: Failed to rename clients.xml.new") + raise MetadataRuntimeError def locked(self, fd): try: |