← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:bug/1717598-fix-gce-user-data into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:bug/1717598-fix-gce-user-data into cloud-init:master.

Commit message:
GCE: Fix usage of user-data.

This regressed in the rework of GCE datasource to have a main.
The fix really just stores the user-data read as userdata_raw, which is
correct and consistent with other datasources.

The main is updated to address the fact that user-data is binary.

LP: #1717598

Requested reviews:
  cloud-init commiters (cloud-init-dev)
Related bugs:
  Bug #1717598 in cloud-init (Ubuntu): "Traceback when passing user-data on GCE"
  https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1717598

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/330880
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:bug/1717598-fix-gce-user-data into cloud-init:master.
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
index 94484d6..5438842 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,7 @@ def get_datasource_list(depends):
 if __name__ == "__main__":
     import argparse
     import json
+    import sys
 
     parser = argparse.ArgumentParser(description='Query GCE Metadata Service')
     parser.add_argument("--endpoint", metavar="URL",
@@ -211,8 +209,17 @@ 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)
+    # user-data is binary
+    if 'user-data' in data:
+        try:
+            user_data = data['user-data'].decode()
+            data['user-data'] = data['user-data'].decode()
+        except UnicodeDecodeError:
+            sys.stderr.write("User-data cannot be decoded. "
+                             "Writing as base64\n")
+            data['user-data-b64'] = b64decode(data['user-data']).decode()
+
+    print(json.dumps(data, indent=1, sort_keys=True, separators=(',', ': ')))
 
 # vi: ts=4 expandtab

References