Ejemplo n.º 1
0
def get_results_folder():
    io_utils = IOUtils()
    http = HttpResponse()
    archive_name = "results"
    header_key = 'Folder-Path'

    folder_path = request.headers.get(f"{header_key}")
    if not folder_path:
        return Response(json.dumps(http.failure(ApiCodeConstants.HTTP_HEADER_NOT_PROVIDED,
                                                ErrorCodes.HTTP_CODE.get(
                                                    ApiCodeConstants.HTTP_HEADER_NOT_PROVIDED) % header_key,
                                                ErrorCodes.HTTP_CODE.get(
                                                    ApiCodeConstants.HTTP_HEADER_NOT_PROVIDED) % header_key,
                                                str(traceback.format_exc()))), 404, mimetype="application/json")

    try:
        io_utils.zip_file(archive_name, folder_path)
    except FileNotFoundError as e:
        result = "Exception({0})".format(e.__str__())
        return Response(json.dumps(http.failure(ApiCodeConstants.GET_FILE_FAILURE,
                                                ErrorCodes.HTTP_CODE.get(
                                                    ApiCodeConstants.GET_FILE_FAILURE),
                                                result,
                                                str(traceback.format_exc()))), 404, mimetype="application/json")
    except:
        result = "Exception({0})".format(sys.exc_info()[0])
        return Response(json.dumps(http.failure(ApiCodeConstants.FOLDER_ZIP_FAILURE,
                                                ErrorCodes.HTTP_CODE.get(
                                                    ApiCodeConstants.FOLDER_ZIP_FAILURE) % folder_path,
                                                result,
                                                str(traceback.format_exc()))), 404, mimetype="application/json")
    return flask.send_file(
        f"/tmp/{archive_name}.zip",
        mimetype='application/zip',
        as_attachment=True), 200
Ejemplo n.º 2
0
def agents_request(text):
    path = text.strip()
    eureka = Eureka(
        EnvStartupSingleton.get_instance().get_config_env_vars().get(
            EnvConstants.EUREKA_SERVER))
    ip_addr_port_header_key = 'IpAddr-Port'  # target specific agent
    home_page_url_header_key = 'HomePageUrl'  # target specific agent
    application = "agent"
    try:
        input_data = request.get_data()
    except:
        input_data = ""

    try:
        request_object = {
            "uri": path.lstrip("/"),
            "method": request.method,
            "headers": request.headers,
            "data": input_data
        }
        app.logger.debug({"msg": f"{request_object}"})
        agent_apps = eureka.get_type_eureka_apps(application)

        if request.headers.get(f"{ip_addr_port_header_key}"):  # not mandatory
            ip_port_list = request.headers.get(
                f"{ip_addr_port_header_key}").split(",")
            agent_apps = [
                app for app in agent_apps
                if f"{app.get('ipAddr')}:{app.get('port')}" in ip_port_list
            ]

        if request.headers.get(f"{home_page_url_header_key}"):  # not mandatory
            home_page_url_list = request.headers.get(
                f"{home_page_url_header_key}").split(",")
            agent_apps = [
                app for app in agent_apps
                if f"{app.get('homePageUrl')}" in home_page_url_list
            ]

        th_utils = ThreadUtils(apps=agent_apps, headers={})
        th_utils.spawn_threads_send_request(request_object)

    except Exception as e:
        raise ApiException(
            ApiCode.DISCOVERY_ERROR.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.DISCOVERY_ERROR.value), e)

    threads_response = th_utils.get_threads_response()

    if th_utils.is_response_zip():
        folder_path = f"{th_utils.download_folder}/{th_utils.get_source_zip_folder()}"
        archive_path = f"{th_utils.download_folder}/{th_utils.get_source_zip_folder()}"
        try:
            IOUtils.zip_file(archive_path, folder_path)
        except Exception as e:
            raise ApiException(
                ApiCode.FOLDER_ZIP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.FOLDER_ZIP_FAILURE.value) %
                folder_path, e)
        IOUtils.delete_file(folder_path)

        return flask.send_file(f"{archive_path}.zip",
                               mimetype='application/zip',
                               as_attachment=True), 200

    return Response(json.dumps(HttpResponse().response(
        ApiCode.SUCCESS.value,
        ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), threads_response)),
                    200,
                    mimetype="application/json")