summaryrefslogtreecommitdiff
path: root/cloudinit/sources
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-26 14:31:09 -0500
committerBarry Warsaw <barry@python.org>2015-01-26 14:31:09 -0500
commitde5974fe93dd717e0c7ba6de17db3192cc258cff (patch)
treea877b3ee6d3362892c0683e054d62f8c0e924908 /cloudinit/sources
parent926a3df79a10ede61967c60f48ff0670a36e689a (diff)
downloadvyos-cloud-init-de5974fe93dd717e0c7ba6de17db3192cc258cff.tar.gz
vyos-cloud-init-de5974fe93dd717e0c7ba6de17db3192cc258cff.zip
* More str/bytes fixes.
* Temporarily skip the MAAS tests in py3 since they need to be ported to oauthlib.
Diffstat (limited to 'cloudinit/sources')
-rw-r--r--cloudinit/sources/DataSourceOpenNebula.py12
-rw-r--r--cloudinit/sources/DataSourceSmartOS.py15
2 files changed, 22 insertions, 5 deletions
diff --git a/cloudinit/sources/DataSourceOpenNebula.py b/cloudinit/sources/DataSourceOpenNebula.py
index f9dac29e..691b39f8 100644
--- a/cloudinit/sources/DataSourceOpenNebula.py
+++ b/cloudinit/sources/DataSourceOpenNebula.py
@@ -379,7 +379,8 @@ def read_context_disk_dir(source_dir, asuser=None):
raise BrokenContextDiskDir("configured user '%s' "
"does not exist", asuser)
try:
- with open(os.path.join(source_dir, 'context.sh'), 'r') as f:
+ path = os.path.join(source_dir, 'context.sh')
+ with open(path, 'r', encoding='utf-8') as f:
content = f.read().strip()
context = parse_shell_config(content, asuser=asuser)
@@ -426,14 +427,19 @@ def read_context_disk_dir(source_dir, asuser=None):
context.get('USER_DATA_ENCODING'))
if encoding == "base64":
try:
- results['userdata'] = base64.b64decode(results['userdata'])
+ userdata = base64.b64decode(results['userdata'])
+ # In Python 3 we still expect a str, but b64decode will return
+ # bytes. Convert to str.
+ if isinstance(userdata, bytes):
+ userdata = userdata.decode('utf-8')
+ results['userdata'] = userdata
except TypeError:
LOG.warn("Failed base64 decoding of userdata")
# generate static /etc/network/interfaces
# only if there are any required context variables
# http://opennebula.org/documentation:rel3.8:cong#network_configuration
- for k in context.keys():
+ for k in context:
if re.match(r'^ETH\d+_IP$', k):
(out, _) = util.subp(['/sbin/ip', 'link'])
net = OpenNebulaNetwork(out, context)
diff --git a/cloudinit/sources/DataSourceSmartOS.py b/cloudinit/sources/DataSourceSmartOS.py
index 7a975d78..d3ed40c5 100644
--- a/cloudinit/sources/DataSourceSmartOS.py
+++ b/cloudinit/sources/DataSourceSmartOS.py
@@ -30,6 +30,7 @@
# Comments with "@datadictionary" are snippets of the definition
import base64
+import binascii
import os
import serial
@@ -350,8 +351,18 @@ def query_data(noun, seed_device, seed_timeout, strip=False, default=None,
if b64:
try:
- return base64.b64decode(resp)
- except TypeError:
+ # Generally, we want native strings in the values. Python 3's
+ # b64decode will return bytes though, so decode them to utf-8 if
+ # possible. If that fails, return the bytes.
+ decoded = base64.b64decode(resp)
+ try:
+ if isinstance(decoded, bytes):
+ return decoded.decode('utf-8')
+ except UnicodeDecodeError:
+ pass
+ return decoded
+ # Bogus input produces different errors in Python 2 and 3; catch both.
+ except (TypeError, binascii.Error):
LOG.warn("Failed base64 decoding key '%s'", noun)
return resp