summaryrefslogtreecommitdiff
path: root/cloudinit/handlers/__init__.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-04-04 12:07:19 -0400
committerScott Moser <smoser@ubuntu.com>2016-04-04 12:07:19 -0400
commit7d8a3194552387fa9e21216bcd9a3bfc76fa2b04 (patch)
treec8dc45b013208a4e5e09e6ade63b3b5994f80aa3 /cloudinit/handlers/__init__.py
parent93f5af9f5075a416c65c1d0350c374e16f32f0d5 (diff)
parent210b041b2fead7a57af91f60a6f89d9e5aa1ed4a (diff)
downloadvyos-cloud-init-7d8a3194552387fa9e21216bcd9a3bfc76fa2b04.tar.gz
vyos-cloud-init-7d8a3194552387fa9e21216bcd9a3bfc76fa2b04.zip
merge with trunk
Diffstat (limited to 'cloudinit/handlers/__init__.py')
-rw-r--r--cloudinit/handlers/__init__.py43
1 files changed, 30 insertions, 13 deletions
diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py
index 059d7495..53d5604a 100644
--- a/cloudinit/handlers/__init__.py
+++ b/cloudinit/handlers/__init__.py
@@ -22,6 +22,7 @@
import abc
import os
+import six
from cloudinit.settings import (PER_ALWAYS, PER_INSTANCE, FREQUENCIES)
@@ -147,7 +148,7 @@ def walker_handle_handler(pdata, _ctype, _filename, payload):
if not modfname.endswith(".py"):
modfname = "%s.py" % (modfname)
# TODO(harlowja): Check if path exists??
- util.write_file(modfname, payload, 0600)
+ util.write_file(modfname, payload, 0o600)
handlers = pdata['handlers']
try:
mod = fixup_handler(importer.import_module(modname))
@@ -162,26 +163,38 @@ def walker_handle_handler(pdata, _ctype, _filename, payload):
def _extract_first_or_bytes(blob, size):
- # Extract the first line upto X bytes or X bytes from more than the
- # first line if the first line does not contain enough bytes
- first_line = blob.split("\n", 1)[0]
- if len(first_line) >= size:
- start = first_line[:size]
- else:
+ # Extract the first line or upto X symbols for text objects
+ # Extract first X bytes for binary objects
+ try:
+ if isinstance(blob, six.string_types):
+ start = blob.split("\n", 1)[0]
+ else:
+ # We want to avoid decoding the whole blob (it might be huge)
+ # By taking 4*size bytes we guarantee to decode size utf8 chars
+ start = blob[:4 * size].decode(errors='ignore').split("\n", 1)[0]
+ if len(start) >= size:
+ start = start[:size]
+ except UnicodeDecodeError:
+ # Bytes array doesn't contain text so return chunk of raw bytes
start = blob[0:size]
return start
def _escape_string(text):
try:
- return text.encode("string-escape")
- except TypeError:
+ return text.encode("string_escape")
+ except (LookupError, TypeError):
try:
- # Unicode doesn't support string-escape...
- return text.encode('unicode-escape')
+ # Unicode (and Python 3's str) doesn't support string_escape...
+ return text.encode('unicode_escape')
except TypeError:
# Give up...
pass
+ except AttributeError:
+ # We're in Python3 and received blob as text
+ # No escaping is needed because bytes are printed
+ # as 'b\xAA\xBB' automatically in Python3
+ pass
return text
@@ -232,7 +245,8 @@ def walk(msg, callback, data):
headers = dict(part)
LOG.debug(headers)
headers['Content-Type'] = ctype
- callback(data, filename, part.get_payload(decode=True), headers)
+ payload = util.fully_decoded_payload(part)
+ callback(data, filename, payload, headers)
partnum = partnum + 1
@@ -249,7 +263,10 @@ def fixup_handler(mod, def_freq=PER_INSTANCE):
def type_from_starts_with(payload, default=None):
- payload_lc = payload.lower()
+ try:
+ payload_lc = util.decode_binary(payload).lower()
+ except UnicodeDecodeError:
+ return default
payload_lc = payload_lc.lstrip()
for text in INCLUSION_SRCH:
if payload_lc.startswith(text):