From 5f7025558cf468792d1c18605667888a5d775b36 Mon Sep 17 00:00:00 2001 From: Ethan Lynn Date: Thu, 29 May 2014 14:49:14 +0800 Subject: Add res_last_path to store last metadata of a resource Add res_last_path='/var/cache/heat-cfntools/last_metadata_' in Metadata.retrieve() function to store metadata of a resource. Remove md5 check between current metadata and last metadata, json.load() will organize json structure. Change-Id: Ie0c31a748f0add3fcab6a579431a28b60051f601 Closes-Bug: #1205375 Partial-Bug: #1133049 --- heat_cfntools/cfntools/cfn_helper.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'heat_cfntools') diff --git a/heat_cfntools/cfntools/cfn_helper.py b/heat_cfntools/cfntools/cfn_helper.py index b5dd5b4..408e6c1 100644 --- a/heat_cfntools/cfntools/cfn_helper.py +++ b/heat_cfntools/cfntools/cfn_helper.py @@ -22,7 +22,6 @@ import atexit import ConfigParser import errno import grp -import hashlib import json import logging import os @@ -1108,6 +1107,11 @@ class Metadata(object): True -- success False -- error """ + if self.resource is not None: + res_last_path = last_path + '_' + self.resource + else: + res_last_path = last_path + if meta_str: self._data = meta_str else: @@ -1127,7 +1131,7 @@ class Metadata(object): # cached metadata or the logic below could re-run a stale # cfn-init-data fd = None - for filepath in [last_path, default_path]: + for filepath in [res_last_path, last_path, default_path]: try: fd = open(filepath) except IOError: @@ -1150,18 +1154,21 @@ class Metadata(object): else: self._metadata = self._data - cm = hashlib.md5(json.dumps(self._metadata)) - current_md5 = cm.hexdigest() - old_md5 = None + last_data = "" + for metadata_file in [res_last_path, last_path]: + try: + with open(metadata_file) as lm: + try: + last_data = json.load(lm) + except ValueError: + pass + lm.close() + except IOError: + LOG.warn("Unable to open local metadata : %s" % + metadata_file) + continue - try: - with open(last_path) as lm: - om = hashlib.md5() - om.update(lm.read()) - old_md5 = om.hexdigest() - except Exception: - pass - if old_md5 != current_md5: + if self._metadata != last_data: self._has_changed = True # if cache dir does not exist try to create it @@ -1181,6 +1188,9 @@ class Metadata(object): os.chmod(cf.name, 0o600) cf.write(json.dumps(self._metadata)) os.rename(cf.name, last_path) + cf.close() + if res_last_path != last_path: + shutil.copy(last_path, res_last_path) return True -- cgit v1.2.1