def get_os_version(instance): """Get OS Version for instances.""" if instance.cloud == 'aws': client = boto3.client('ec2', instance.region) image_id = client.describe_instances( InstanceIds=[instance.id ])['Reservations'][0]['Instances'][0]['ImageId'] return '16.04' if '16.04' in client.describe_images( ImageIds=[image_id])['Images'][0]['Name'] else '14.04' if instance.cloud == 'gcp': credentials = GoogleCredentials.get_application_default() compute = discovery.build('compute', 'v1', credentials=credentials) for disk in compute.instances().get( instance=instance.name, zone=instance.zone, project=instance.project).execute()['disks']: if not disk.get('boot'): continue for value in disk.get('licenses', []): if '1604' in value: return '16.04' if '1404' in value: return '14.04' return '14.04' return '14.04'
def _aws_get_instance_by_tag(region, name, tag, raw): """Get all instances matching a tag.""" client = boto3.session.Session().client('ec2', region) matching_reservations = client.describe_instances(Filters=[{'Name': tag, 'Values': [name]}]).get('Reservations', []) instances = [] [[instances.append(_aws_instance_from_dict(region, instance, raw)) # pylint: disable=expression-not-assigned for instance in reservation.get('Instances')] for reservation in matching_reservations if reservation] return instances
def aws_get_instances_by_id(region, instance_id, raw=True): """Returns instances mathing an id.""" client = boto3.session.Session().client('ec2', region) try: matching_reservations = client.describe_instances(InstanceIds=[instance_id]).get('Reservations', []) except ClientError as exc: if exc.response.get('Error', {}).get('Code') != 'InvalidInstanceID.NotFound': raise return [] instances = [] [[instances.append(_aws_instance_from_dict(region, instance, raw)) # pylint: disable=expression-not-assigned for instance in reservation.get('Instances')] for reservation in matching_reservations if reservation] return instances