← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~chad.smith/cloud-init:unify-datasource-get-data into cloud-init:master

 


Diff comments:

> diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py
> index 9a43fbe..ec0fe2a 100644
> --- a/cloudinit/sources/__init__.py
> +++ b/cloudinit/sources/__init__.py
> @@ -78,6 +103,52 @@ class DataSource(object):
>      def __str__(self):
>          return type_utils.obj_name(self)
>  
> +    def _get_standardized_metadata(self):
> +        """Return a dictionary of standardized metadata keys."""
> +        return {'v1': {
> +            'public-hostname': self.get_hostname(),
> +            'public-ipv4-address': self.metadata.get('public-ipv4'),
> +            'public-ipv6-address': self.metadata.get('ipv6-address'),
> +            'instance-id': self.get_instance_id(),
> +            'cloud-name': self.cloud_name,
> +            'region': self.region,
> +            'availability-zone': self.metadata.get('availability_zone')}}
> +
> +    def get_data(self):
> +        """Datasources implement _get_data to setup metadata and userdata_raw.
> +
> +        Minimally, the datasource should return a boolean True on success.
> +        """
> +        return_value = self._get_data()
> +        json_file = os.path.join(self.paths.run_dir, INSTANCE_JSON_FILE)
> +        if return_value:
> +            instance_data = {
> +                'ds': {
> +                    'meta-data': self.metadata,
> +                    'user-data': self.get_userdata_raw(),
> +                    'vendor-data': self.get_vendordata_raw()}}
> +            instance_data.update(
> +                self._get_standardized_metadata())
> +            try:
> +                # Process content base64encoding unserializable values
> +                content = util.json_dumps(instance_data)
> +                # Strip base64: prefix and return base64-encoded-keys
> +                processed_data = process_base64_metadata(json.loads(content))
> +                content = util.json_dumps(processed_data)
> +            except TypeError as e:
> +                LOG.warning('Error persisting instance-data.json: %s', str(e))
> +                return return_value
> +            except UnicodeDecodeError as e:
> +                LOG.warning('Error persisting instance-data.json: %s', str(e))
> +                return return_value
> +            util.write_file(json_file, content, mode=0o600)

use atomic_helper.write_json here. thats almost exactly what it is for.

> +        return return_value
> +
> +    def _get_data(self):
> +        raise NotImplementedError(
> +            'Subclasses of DataSource must implement _get_data which'
> +            ' sets self.metadata, vendordata_raw and userdata_raw.')
> +
>      def get_userdata(self, apply_filter=False):
>          if self.userdata is None:
>              self.userdata = self.ud_proc.process(self.get_userdata_raw())


-- 
https://code.launchpad.net/~chad.smith/cloud-init/+git/cloud-init/+merge/330115
Your team cloud-init commiters is requested to review the proposed merge of ~chad.smith/cloud-init:unify-datasource-get-data into cloud-init:master.


References