def retrieve_host()

in source/soca/cluster_web_ui/scheduled_tasks/manage_dcv_instances_lifecycle.py [0:0]


def retrieve_host(instances_info, instance_state):
    instance_ids = list(instances_info.keys())
    host_info = {}
    token = True
    next_token = ''
    while token is True:
        if instances_info:
            response = client_ec2.describe_instances(
                Filters=[
                    {
                        'Name': "instance-state-name",
                        'Values': [instance_state]
                    },
                    {
                        'Name': 'instance-id',
                        'Values': instance_ids
                    },
                    {
                        "Name": "tag:soca:ClusterId",
                        "Values": [os.environ["SOCA_CONFIGURATION"]]
                    },
                    {
                        "Name": "tag:soca:DCVSupportHibernate",
                        "Values": ["true", "false"]
                    },
                    {
                        "Name": "tag:soca:NodeType",
                        "Values": ["dcv"]
                    }
                ],
                MaxResults=1000,
                NextToken=next_token,
            )
        else:
            # get all stopped dcv instances
            response = client_ec2.describe_instances(
                Filters=[
                    {
                        'Name': "instance-state-name",
                        'Values': ["stopped"]
                    },
                    {
                        "Name": "tag:soca:ClusterId",
                        "Values": [os.environ["SOCA_CONFIGURATION"]]
                    },
                    {
                        "Name": "tag:soca:DCVSupportHibernate",
                        "Values": ["true", "false"]
                    },
                    {
                        "Name": "tag:soca:NodeType",
                        "Values": ["dcv"]
                    }
                ],
                MaxResults=1000,
                NextToken=next_token,
            )

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

        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                session_uuid = False
                current_time = parse(response['ResponseMetadata']['HTTPHeaders']['date'])
                if instance_state == "stopped":
                    stopped_time = parse(re.findall('.*\((.*)\)', instance["StateTransitionReason"])[0])
                else:
                    stopped_time = False
                for instance in reservation['Instances']:
                    hibernate_enabled = False
                    stack_name = False
                    for tag in instance["Tags"]:
                        if tag["Key"] == "Name":
                            stack_name = tag["Value"]
                        if tag["Key"] == "soca:DCVSupportHibernate":
                            if tag["Value"] == "true":
                                hibernate_enabled = True
                        if tag["Key"] == "soca:DCVSessionUUID":
                            session_uuid = tag["Value"]

                    host_info[instance["InstanceId"]] = {"stopped_time": stopped_time,
                                                         "current_time": current_time,
                                                         "hibernate_enabled": hibernate_enabled,
                                                         "session_uuid": session_uuid,
                                                         "stack_name": stack_name if stack_name is not False else ""}

    return host_info