diff options
-rw-r--r-- | cloudinit/sources/DataSourceGCE.py | 26 | ||||
-rw-r--r-- | tests/unittests/test_datasource/test_gce.py | 3 |
2 files changed, 21 insertions, 8 deletions
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index 94484d60..ccae4200 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -62,7 +62,7 @@ class DataSourceGCE(sources.DataSource): LOG.debug(ret['reason']) return False self.metadata = ret['meta-data'] - self.userdata = ret['user-data'] + self.userdata_raw = ret['user-data'] return True @property @@ -80,9 +80,6 @@ class DataSourceGCE(sources.DataSource): # GCE has long FDQN's and has asked for short hostnames return self.metadata['local-hostname'].split('.')[0] - def get_userdata_raw(self): - return self.userdata - @property def availability_zone(self): return self.metadata['availability-zone'] @@ -202,6 +199,9 @@ def get_datasource_list(depends): if __name__ == "__main__": import argparse import json + import sys + + from base64 import b64encode parser = argparse.ArgumentParser(description='Query GCE Metadata Service') parser.add_argument("--endpoint", metavar="URL", @@ -211,8 +211,20 @@ if __name__ == "__main__": help="Ignore smbios platform check", action='store_false', default=True) args = parser.parse_args() - print(json.dumps( - read_md(address=args.endpoint, platform_check=args.platform_check), - indent=1, sort_keys=True, separators=(',', ': '))) + data = read_md(address=args.endpoint, platform_check=args.platform_check) + if 'user-data' in data: + # user-data is bytes not string like other things. Handle it specially. + # if it can be represented as utf-8 then do so. Otherwise print base64 + # encoded value in the key user-data-b64. + try: + data['user-data'] = data['user-data'].decode() + except UnicodeDecodeError: + sys.stderr.write("User-data cannot be decoded. " + "Writing as base64\n") + del data['user-data'] + # b64encode returns a bytes value. decode to get the string. + data['user-data-b64'] = b64encode(data['user-data']).decode() + + print(json.dumps(data, indent=1, sort_keys=True, separators=(',', ': '))) # vi: ts=4 expandtab diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py index 50e49a10..d399ae7a 100644 --- a/tests/unittests/test_datasource/test_gce.py +++ b/tests/unittests/test_datasource/test_gce.py @@ -23,7 +23,8 @@ GCE_META = { 'instance/zone': 'foo/bar', 'project/attributes/sshKeys': 'user:ssh-rsa AA2..+aRD0fyVw== root@server', 'instance/hostname': 'server.project-foo.local', - 'instance/attributes/user-data': b'/bin/echo foo\n', + # UnicodeDecodeError below if set to ds.userdata instead of userdata_raw + 'instance/attributes/user-data': b'/bin/echo \xff\n', } GCE_META_PARTIAL = { |