← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/include-robust into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/include-robust into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)
Related bugs:
  Bug #1203412 in cloud-init: "Handle inclusion robust"
  https://bugs.launchpad.net/cloud-init/+bug/1203412

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/include-robust/+merge/176036
-- 
https://code.launchpad.net/~harlowja/cloud-init/include-robust/+merge/176036
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/include-robust into lp:cloud-init.
=== modified file 'cloudinit/user_data.py'
--- cloudinit/user_data.py	2013-02-24 05:23:24 +0000
+++ cloudinit/user_data.py	2013-07-21 03:07:28 +0000
@@ -171,19 +171,41 @@
             if include_once_on:
                 include_once_fn = self._get_include_once_filename(include_url)
             if include_once_on and os.path.isfile(include_once_fn):
-                content = util.load_file(include_once_fn)
-            else:
-                resp = util.read_file_or_url(include_url,
-                                             ssl_details=self.ssl_details)
-                if include_once_on and resp.ok():
-                    util.write_file(include_once_fn, str(resp), mode=0600)
-                if resp.ok():
-                    content = str(resp)
-                else:
-                    LOG.warn(("Fetching from %s resulted in"
-                              " a invalid http code of %s"),
-                             include_url, resp.code)
-
+                try:
+                    content = util.load_file(include_once_fn)
+                except IOError as e:
+                    LOG.warn("Failed loading pre-fetched content from %s"
+                             " due to: %s", include_once_fn, e)
+
+            # NOTE(harlowja): if we failed reading from the include once file
+            # *or* we just need to fetch the content this call will now happen
+            # to attempt to fetch the include content. If succesfull and the
+            # data is the include-once type, then we will attempt to write to
+            # the file that should contain said data (but not fail the include
+            # of more data if reading/writing fails).
+            if content is None:
+                try:
+                    resp = util.read_file_or_url(include_url,
+                                                 ssl_details=self.ssl_details)
+                    if resp.ok():
+                        content = str(resp)
+                        if include_once_fn and include_once_on:
+                            try:
+                                util.write_file(include_once_fn, content,
+                                                mode=0600)
+                            except IOError as e:
+                                LOG.warn("Failed writing include-once data"
+                                         " to %s due to: %s", include_once_fn,
+                                         e)
+                    else:
+                        LOG.warn("Fetching from %s resulted in a invalid http"
+                                 " code of %s", include_url, resp.code)
+                except IOError as e:
+                    LOG.warn("Failed fetching content from %s due to: %s",
+                             include_url, e)
+
+            # If we got any content, attach any of its pieces to the
+            # accumulating message...
             if content is not None:
                 new_msg = convert_string(content)
                 self._process_msg(new_msg, append_msg)