def get_all_compute_instances()

in source/soca/cluster_manager/nodes_manager.py [0:0]


def get_all_compute_instances(cluster_id):
    token = True
    next_token = ''
    job_stack = {}
    while token is True:
        # ATTENTION /!\
        # CHANGING THIS FILTER COULD POSSIBLE BRING DOWN OTHER EC2 INSTANCES IN YOUR AWS ACCOUNT
        response = ec2_client.describe_instances(
            Filters=[
                {
                    'Name': 'instance-state-name',
                    'Values': [
                        'running',
                    ]
                },
                {
                    'Name': 'tag:soca:NodeType',
                    'Values': ['soca-compute-node']
                },
                {
                    'Name': 'tag:soca:KeepForever',
                    'Values': ['true', 'false']
                },
                {
                    'Name': 'tag:soca:ClusterId',
                    'Values': [cluster_id]
                },

            ],
            MaxResults=1000,
            NextToken=next_token,
        )

        try:
            next_token = response['NextToken']
        except KeyError:
            token = False

        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                try:
                    instance_id = instance['InstanceId']
                    instance_type = instance['InstanceType']
                    subnet_id = instance['SubnetId']
                    availability_zone = instance['Placement']['AvailabilityZone']
                    job_id = [x['Value'] for x in instance['Tags'] if x['Key'] == 'soca:JobId']
                    job_queue = [x['Value'] for x in instance['Tags'] if x['Key'] == 'soca:JobQueue'][0]
                    keep_forever = [x['Value'] for x in instance['Tags'] if x['Key'] == 'soca:KeepForever'][0]
                    terminate_when_idle = [x['Value'] for x in instance['Tags'] if x['Key'] == 'soca:TerminateWhenIdle'][0]
                    cloudformation_stack = ""
                    stack_id = ""
                    asg_spotfleet_id = ""
                    for x in instance['Tags']:
                        if x['Key'] == 'aws:cloudformation:stack-name':
                            cloudformation_stack = x['Value']
                        if x['Key'] == 'aws:autoscaling:groupName':
                            asg_spotfleet_id = x['Value']
                        elif x['Key'] == 'aws:ec2spot:fleet-request-id':
                            asg_spotfleet_id = x['Value']
                        if x['Key'] == 'soca:StackId':
                            stack_id = x['Value']
                    if cloudformation_stack == "":
                        cloudformation_stack = stack_id
                    private_dns = instance['PrivateDnsName'].split('.')[0]

                    if not job_id:
                        job_id = 'do_not_delete'
                    else:
                        job_id = job_id[0]

                    if job_id in job_stack.keys():
                        job_stack[job_id]['instances'][private_dns] = {'instance_id' : instance_id,
                                                                       'instance_type' : instance_type,
                                                                       'subnet_id': subnet_id,
                                                                       'availability_zone': availability_zone}
                    else:
                        host = {}
                        host[private_dns] = {'instance_id' : instance_id,
                                             'instance_type' : instance_type,
                                             'subnet_id': subnet_id,
                                             'availability_zone': availability_zone}

                        job_stack[job_id] = {'stack_name': cloudformation_stack,
                                             'terminate_when_idle': terminate_when_idle,
                                             'asg_spotfleet_id': asg_spotfleet_id,
                                             'keep_forever': keep_forever,
                                             'instances': host,
                                             'job_queue': job_queue,
                                             'job_id': job_id}
                except Exception as e:
                    exc_type, exc_obj, exc_tb = sys.exc_info()
                    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
                    print(exc_type, fname, exc_tb.tb_lineno)

    return job_stack