diff options
author | Joshua Harlow <harlowja@gmail.com> | 2013-07-24 11:10:17 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2013-07-24 11:10:17 -0400 |
commit | 2939f498f51025ebd0c9bdf302b578dfcbf54d0c (patch) | |
tree | f5be8769f8de4a40935c29f5d8eb3b7422032f93 | |
parent | 0891f6611d1c264220a6f71306802db1e70651fc (diff) | |
parent | 97b19f3b1992e56ef8e1a055fbe64a19d1eacfbf (diff) | |
download | vyos-cloud-init-2939f498f51025ebd0c9bdf302b578dfcbf54d0c.tar.gz vyos-cloud-init-2939f498f51025ebd0c9bdf302b578dfcbf54d0c.zip |
Add support for merging via jsonp patch format.
This adds a very well defined and understood mechanism for applying changes to
the cloud-config. Had we seen this previously, we might have not done the
merge-types work.
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | Requires | 3 | ||||
-rw-r--r-- | cloudinit/handlers/__init__.py | 1 | ||||
-rw-r--r-- | cloudinit/handlers/cloud_config.py | 36 |
4 files changed, 34 insertions, 7 deletions
@@ -9,6 +9,7 @@ caused a problem on rhel5 if missing. - support individual MIME segments to be gzip compressed (LP: #1203203) - always finalize handlers even if processing failed (LP: #1203368) + - support merging into cloud-config via jsonp. (LP: #1200476) 0.7.2: - add a debian watch file - add 'sudo' entry to ubuntu's default user (LP: #1080717) @@ -27,3 +27,6 @@ requests # Boto for ec2 boto + +# For patching pieces of cloud-config together +jsonpatch diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py index 1d450061..2ddc75f4 100644 --- a/cloudinit/handlers/__init__.py +++ b/cloudinit/handlers/__init__.py @@ -62,6 +62,7 @@ INCLUSION_TYPES_MAP = { '#part-handler': 'text/part-handler', '#cloud-boothook': 'text/cloud-boothook', '#cloud-config-archive': 'text/cloud-config-archive', + '#cloud-config-jsonp': 'text/cloud-config-jsonp', } # Sorted longest first diff --git a/cloudinit/handlers/cloud_config.py b/cloudinit/handlers/cloud_config.py index 730672d7..7edae13d 100644 --- a/cloudinit/handlers/cloud_config.py +++ b/cloudinit/handlers/cloud_config.py @@ -20,6 +20,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import jsonpatch + from cloudinit import handlers from cloudinit import log as logging from cloudinit import mergers @@ -50,6 +52,13 @@ MERGE_HEADER = 'Merge-Type' # This gets loaded into yaml with final result {'a': 22} DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()') CLOUD_PREFIX = "#cloud-config" +JSONP_PREFIX = "#cloud-config-jsonp" + +# The file header -> content types this module will handle. +CC_TYPES = { + JSONP_PREFIX: handlers.type_from_starts_with(JSONP_PREFIX), + CLOUD_PREFIX: handlers.type_from_starts_with(CLOUD_PREFIX), +} class CloudConfigPartHandler(handlers.Handler): @@ -60,9 +69,7 @@ class CloudConfigPartHandler(handlers.Handler): self.file_names = [] def list_types(self): - return [ - handlers.type_from_starts_with(CLOUD_PREFIX), - ] + return list(CC_TYPES.values()) def _write_cloud_config(self): if not self.cloud_fn: @@ -108,13 +115,22 @@ class CloudConfigPartHandler(handlers.Handler): all_mergers = DEF_MERGERS return (payload_yaml, all_mergers) + def _merge_patch(self, payload): + payload = payload.lstrip() + if payload.lower().startswith(JSONP_PREFIX): + # JSON doesn't handle comments in this manner, so ensure that + # if we started with this 'type' that we remove it before + # attempting to load it as json (which the jsonpatch library will + # attempt to do). + payload = payload[JSONP_PREFIX:] + patch = jsonpatch.JsonPatch.from_string(payload) + LOG.debug("Merging by applying json patch %s", patch) + self.cloud_buf = patch.apply(self.cloud_buf, in_place=False) + def _merge_part(self, payload, headers): (payload_yaml, my_mergers) = self._extract_mergers(payload, headers) LOG.debug("Merging by applying %s", my_mergers) merger = mergers.construct(my_mergers) - if self.cloud_buf is None: - # First time through, merge with an empty dict... - self.cloud_buf = {} self.cloud_buf = merger.merge(self.cloud_buf, payload_yaml) def _reset(self): @@ -131,7 +147,13 @@ class CloudConfigPartHandler(handlers.Handler): self._reset() return try: - self._merge_part(payload, headers) + # First time through, merge with an empty dict... + if self.cloud_buf is None or not self.file_names: + self.cloud_buf = {} + if ctype == CC_TYPES[JSONP_PREFIX]: + self._merge_patch(payload) + else: + self._merge_part(payload, headers) # Ensure filename is ok to store for i in ("\n", "\r", "\t"): filename = filename.replace(i, " ") |