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.
This commit is contained in:
commit
750438ad7f
@ -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)
|
||||
|
3
Requires
3
Requires
@ -27,3 +27,6 @@ requests
|
||||
|
||||
# Boto for ec2
|
||||
boto
|
||||
|
||||
# For patching pieces of cloud-config together
|
||||
jsonpatch
|
||||
|
@ -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
|
||||
|
@ -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, " ")
|
||||
|
Loading…
x
Reference in New Issue
Block a user