def verify_target_account_servers()

in source/Tools Integration/MGN-Integration/Lambdas/lambda_mgn.py [0:0]


def verify_target_account_servers(serverlist):
    # Check each AWS account and region one by one
    verified_servers = serverlist
    try:
        # Enable multithreading
        processes = []
        manager = multiprocessing.Manager()
        return_dict_id = manager.dict()
        for account in verified_servers:
            source_server_ids = []
            target_account_creds = assume_role(str(account['aws_accountid']))
            target_account_session = get_session(target_account_creds, account['aws_region'])
            mgn_client_base = target_account_session.client("mgn", region_name=account['aws_region'], config=boto_config)
            mgn_sourceservers = mgn_client_base.describe_source_servers(filters={})
            log.info("Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'])
            print(mgn_sourceservers)
            for factoryserver in account['servers']:
                if 'server_fqdn' not in factoryserver:
                    msg = "ERROR: server_fqdn does not exist for server: " + factoryserver['server_name']
                    log.error(msg)
                    return msg
                else:
                    isServerExist = False
                    for sourceserver in mgn_sourceservers['items']:
                        # Check if the factory server exist in Application Migration Service
                        if factoryserver['server_name'].lower().strip() == sourceserver['sourceProperties']['identificationHints']['hostname'].lower().strip():
                            isServerExist = True
                        elif factoryserver['server_name'].lower().strip() == sourceserver['sourceProperties']['identificationHints']['fqdn'].lower().strip():
                            isServerExist = True
                        elif factoryserver['server_fqdn'].lower().strip() == sourceserver['sourceProperties']['identificationHints']['hostname'].lower().strip():
                            isServerExist = True
                        elif factoryserver['server_fqdn'].lower().strip() == sourceserver['sourceProperties']['identificationHints']['fqdn'].lower().strip():
                            isServerExist = True
                        else:
                            continue
                        # Get EC2 launch template Id for the source server in Application Migration Service
                        if isServerExist == True:
                            if sourceserver['isArchived'] == False:
                                factoryserver['source_server_id'] = sourceserver['sourceServerID']
                                source_server_ids.append(factoryserver['source_server_id'])
                                if sourceserver['dataReplicationInfo']['dataReplicationState'].lower() != 'disconnected':
                                    p = multiprocessing.Process(target=multiprocessing_launch_template_id, args=(target_account_creds, account['aws_region'], factoryserver, return_dict_id))
                                    processes.append(p)
                                    p.start()
                                    break
                            else:
                                # Check if there is another server with the same name registered in MGN that is not archived, this can occur if someone archives a server and then redploys the agent to the same server, it then gets a new MGN id.
                                isSecondServerExist = False
                                for sourceserver1 in mgn_sourceservers['items']:
                                    if sourceserver1['isArchived'] == False and sourceserver['sourceServerID'] != sourceserver1['sourceServerID']:
                                        if factoryserver['server_name'].lower().strip() == sourceserver1['sourceProperties']['identificationHints']['hostname'].lower().strip():
                                            isSecondServerExist = True
                                        elif factoryserver['server_name'].lower().strip() == sourceserver1['sourceProperties']['identificationHints']['fqdn'].lower().strip():
                                            isSecondServerExist = True
                                        elif factoryserver['server_fqdn'].lower().strip() == sourceserver1['sourceProperties']['identificationHints']['hostname'].lower().strip():
                                            isSecondServerExist = True
                                        elif factoryserver['server_fqdn'].lower().strip() == sourceserver1['sourceProperties']['identificationHints']['fqdn'].lower().strip():
                                            isSecondServerExist = True
                                        else:
                                            continue
                                if isSecondServerExist:
                                    continue
                                else:
                                    msg = "ERROR: Server: " + factoryserver['server_name'] + " is archived in Application Migration Service (Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'] + "), Please reinstall the agent"
                                    log.error(msg)
                                    return msg
                    if isServerExist == False:
                        msg = "ERROR: Server: " + factoryserver['server_name'] + " does not exist in Application Migration Service (Account: " + account['aws_accountid'] + ", Region: " + account['aws_region'] + "), Please reinstall the agent."
                        log.error(msg)
                        return msg
            account['source_server_ids'] = source_server_ids
        # Waiting for all processes to finish
        for process in processes:
            process.join()
        # Get ec2LaunchTemplateID from the dictionary
        for account in verified_servers:
            for factoryserver in account['servers']:
                if factoryserver['server_name'] in return_dict_id:
                    factoryserver['launch_template_id'] = return_dict_id[factoryserver['server_name']]
        return verified_servers
    except botocore.exceptions.ClientError as error:
        if ":" in str(error):
            log.error("ERROR: " + str(error))
            err = ''
            msgs = str(error).split(":")[1:]
            for msg in msgs:
                err = err + msg
            msg = "ERROR: " + err
            return msg
        else:
            msg = "ERROR: " + str(error)
            log.error(msg)
            return msg