Comment 2 for bug 1855616

Revision history for this message
Chad Smith (chad.smith) wrote :

I'll have to double check on UEC images and whether cloud-init is included in those image types, but we should be able to depend on the following function that that is the case:

def add_cloud_info(report):
    # EC2 and Ubuntu Enterprise Cloud instances
    try:
     instance_data = json.loads(
        subprocess.check_output(['cloud-init', 'query', '--all']).decode())
    except subprocess.CalledProcessError as e:
        return
    report = {}
    if instance_data.get('platform') == 'ec2':
        ami = instance_data.get('ds', {}).get('meta_data', {}).get('ami_id')
        if ami and ami.startswith('ami'):
            report['Ec2AMI'] = ami
            fields = {
                'Ec2AMIManifest': 'ds.meta_data.ami_manifest_path',
                'Ec2Kernel': 'ds.dynamic.instance_identity.document.kernelId',
                'Ec2Ramdisk':
                    'ds.dynamic.instance_identity.document.ramdiskId',
                'Ec2InstanceType': 'ds.meta_data.instance_type',
                'Ec2AvailabilityZone': 'availability_zone'}
            for key, path in fields.items():
                value = instance_data
                for path_part in path.split('.'):
                    value = value.get(path_part)
                    if not value:
                        break
                report[key] = value if value else 'unavailable'
        else:
            add_tag(report, 'uec-images')