Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MIME policy failure on python version upgrade (SC-201) #934

Merged
merged 5 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions cloudinit/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
_NETCFG_SOURCE_NAMES)(*_NETCFG_SOURCE_NAMES)


class DatasourceUnpickleUserDataError(Exception):
"""Raised when userdata is unable to be unpickled due to python upgrades"""


class DataSourceNotFoundException(Exception):
pass

Expand Down Expand Up @@ -239,6 +243,20 @@ def _unpickle(self, ci_pkl_version: int) -> None:
self.vendordata2 = None
if not hasattr(self, 'vendordata2_raw'):
self.vendordata2_raw = None
if hasattr(self, 'userdata') and self.userdata is not None:
# If userdata stores MIME data, on < python3.6 it will be
# missing the 'policy' attribute that exists on >=python3.6.
# Calling str() on the userdata will attempt to access this
# policy attribute. This will raise an exception, causing
# the pickle load to fail, so cloud-init will discard the cache
try:
str(self.userdata)
except AttributeError as e:
LOG.debug(
"Unable to unpickle datasource: %s."
" Ignoring current cache.", e
)
raise DatasourceUnpickleUserDataError() from e

def __str__(self):
return type_utils.obj_name(self)
Expand Down
2 changes: 2 additions & 0 deletions cloudinit/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ def _pkl_load(fname):
return None
try:
return pickle.loads(pickle_contents)
except sources.DatasourceUnpickleUserDataError:
return None
except Exception:
util.logexc(LOG, "Failed loading pickled blob from %s", fname)
return None
Expand Down