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.
|
caused a problem on rhel5 if missing.
|
||||||
- support individual MIME segments to be gzip compressed (LP: #1203203)
|
- support individual MIME segments to be gzip compressed (LP: #1203203)
|
||||||
- always finalize handlers even if processing failed (LP: #1203368)
|
- always finalize handlers even if processing failed (LP: #1203368)
|
||||||
|
- support merging into cloud-config via jsonp. (LP: #1200476)
|
||||||
0.7.2:
|
0.7.2:
|
||||||
- add a debian watch file
|
- add a debian watch file
|
||||||
- add 'sudo' entry to ubuntu's default user (LP: #1080717)
|
- add 'sudo' entry to ubuntu's default user (LP: #1080717)
|
||||||
|
3
Requires
3
Requires
@ -27,3 +27,6 @@ requests
|
|||||||
|
|
||||||
# Boto for ec2
|
# Boto for ec2
|
||||||
boto
|
boto
|
||||||
|
|
||||||
|
# For patching pieces of cloud-config together
|
||||||
|
jsonpatch
|
||||||
|
@ -62,6 +62,7 @@ INCLUSION_TYPES_MAP = {
|
|||||||
'#part-handler': 'text/part-handler',
|
'#part-handler': 'text/part-handler',
|
||||||
'#cloud-boothook': 'text/cloud-boothook',
|
'#cloud-boothook': 'text/cloud-boothook',
|
||||||
'#cloud-config-archive': 'text/cloud-config-archive',
|
'#cloud-config-archive': 'text/cloud-config-archive',
|
||||||
|
'#cloud-config-jsonp': 'text/cloud-config-jsonp',
|
||||||
}
|
}
|
||||||
|
|
||||||
# Sorted longest first
|
# Sorted longest first
|
||||||
|
@ -20,6 +20,8 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import jsonpatch
|
||||||
|
|
||||||
from cloudinit import handlers
|
from cloudinit import handlers
|
||||||
from cloudinit import log as logging
|
from cloudinit import log as logging
|
||||||
from cloudinit import mergers
|
from cloudinit import mergers
|
||||||
@ -50,6 +52,13 @@ MERGE_HEADER = 'Merge-Type'
|
|||||||
# This gets loaded into yaml with final result {'a': 22}
|
# This gets loaded into yaml with final result {'a': 22}
|
||||||
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
|
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
|
||||||
CLOUD_PREFIX = "#cloud-config"
|
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):
|
class CloudConfigPartHandler(handlers.Handler):
|
||||||
@ -60,9 +69,7 @@ class CloudConfigPartHandler(handlers.Handler):
|
|||||||
self.file_names = []
|
self.file_names = []
|
||||||
|
|
||||||
def list_types(self):
|
def list_types(self):
|
||||||
return [
|
return list(CC_TYPES.values())
|
||||||
handlers.type_from_starts_with(CLOUD_PREFIX),
|
|
||||||
]
|
|
||||||
|
|
||||||
def _write_cloud_config(self):
|
def _write_cloud_config(self):
|
||||||
if not self.cloud_fn:
|
if not self.cloud_fn:
|
||||||
@ -108,13 +115,22 @@ class CloudConfigPartHandler(handlers.Handler):
|
|||||||
all_mergers = DEF_MERGERS
|
all_mergers = DEF_MERGERS
|
||||||
return (payload_yaml, all_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):
|
def _merge_part(self, payload, headers):
|
||||||
(payload_yaml, my_mergers) = self._extract_mergers(payload, headers)
|
(payload_yaml, my_mergers) = self._extract_mergers(payload, headers)
|
||||||
LOG.debug("Merging by applying %s", my_mergers)
|
LOG.debug("Merging by applying %s", my_mergers)
|
||||||
merger = mergers.construct(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)
|
self.cloud_buf = merger.merge(self.cloud_buf, payload_yaml)
|
||||||
|
|
||||||
def _reset(self):
|
def _reset(self):
|
||||||
@ -131,7 +147,13 @@ class CloudConfigPartHandler(handlers.Handler):
|
|||||||
self._reset()
|
self._reset()
|
||||||
return
|
return
|
||||||
try:
|
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
|
# Ensure filename is ok to store
|
||||||
for i in ("\n", "\r", "\t"):
|
for i in ("\n", "\r", "\t"):
|
||||||
filename = filename.replace(i, " ")
|
filename = filename.replace(i, " ")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user