From 197d7d4fee1d402cda0e2054ae48247b4c361f53 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 29 Jun 2012 12:18:54 -0700 Subject: Cleanup the pickling. 1. When loading the pickled file, don't log if it isn't there a. Do log though if it is there and unpickling fails 2. On writing log if pickling fails and log if pickle writing fails. --- cloudinit/stages.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 4fcc66e4..ef89e77c 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -159,15 +159,23 @@ class Init(object): return merger.cfg def _restore_from_cache(self): + # We try to restore from a current link and static path + # by using the instance link, if purge_cache was called + # the file wont exist. pickled_fn = self.paths.get_ipath_cur('obj_pkl') + pickle_contents = None try: - # we try to restore from a current link and static path - # by using the instance link, if purge_cache was called - # the file wont exist - return pickle.loads(util.load_file(pickled_fn)) + pickle_contents = util.load_file(pickled_fn) except Exception: - util.logexc(LOG, "Failed loading pickled datasource from %s", - pickled_fn) + pass + # This is expected so just return nothing + # successfully loaded... + if not pickle_contents: + return None + try: + return pickle.loads(pickle_contents) + except Exception: + util.logexc(LOG, "Failed loading pickled blob from %s", pickled_fn) return None def _write_to_cache(self): @@ -175,12 +183,16 @@ class Init(object): return False pickled_fn = self.paths.get_ipath_cur("obj_pkl") try: - contents = pickle.dumps(self.datasource) - util.write_file(pickled_fn, contents, mode=0400) - return True + pk_contents = pickle.dumps(self.datasource) + except Exception: + util.logexc(LOG, "Failed pickling datasource %s", self.datasource) + return False + try: + util.write_file(pickled_fn, pk_contents, mode=0400) except Exception: util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn) return False + return True def _get_datasources(self): # Any config provided??? -- cgit v1.2.3