Exemple #1
0
 def delete_jobs(self, job_list, as_json=False):
     """
     Delete all jobs in the list. They may belong to different services based on their prefix, currently
     either 'njs', 'method', or 'ujs'.
     """
     deletion_status = dict()
     for job_id in job_list:
         app_id = None
         if job_id.startswith('njs:'):
             # delete from njs
             is_deleted = True
             app_id = job_id[4:]
         elif job_id.startswith('method:'):
             # delete from njs_wrapper
             is_deleted = True
             app_id = job_id[7:]
         else:
             # delete from ujs (njs_wrapper?)
             is_deleted = False
         if app_id is not None:
             token = os.environ['KB_AUTH_TOKEN']
             njsClient = NarrativeJobService(URLS.job_service, token=token)
             try:
                 status = njsClient.delete_app(app_id)
                 if (not status == 'success') and ('was marked for deletion'
                                                   not in status):
                     is_deleted = False
             except Exception as e:
                 # just return false until we get some better info from the NJS folks.
                 is_deleted = False
         deletion_status[job_id] = is_deleted
     if as_json:
         import json
         deletion_status = json.dumps(deletion_status)
     return deletion_status
Exemple #2
0
def _app_get_state(workspace, token, URLS, job_manager, app_spec_json,
                   method_specs_json, param_values_json, app_job_id):
    appSpec = json.loads(app_spec_json)
    paramValues = json.loads(correct_method_specs_json(param_values_json))
    methIdToSpec = json.loads(correct_method_specs_json(method_specs_json))
    njsClient = NarrativeJobService(URLS.job_service, token=token)
    wsClient = workspaceService(URLS.workspace, token=token)
    if app_job_id.startswith("njs:"):
        app_job_id = app_job_id[4:]
    appState = njsClient.check_app_state(app_job_id)
    appState['widget_outputs'] = {}
    prevStepReady = True
    for stepSpec in appSpec['steps']:
        stepId = stepSpec['step_id']
        methodId = stepSpec['method_id']
        methodSpec = methIdToSpec[methodId]
        if stepId in appState['step_outputs']:
            rpcOut = appState['step_outputs'][stepId]
            prevStepReady = True
        elif 'output_mapping' in methodSpec['behavior'] and prevStepReady:
            rpcOut = None
            prevStepReady = True
        else:
            prevStepReady = False
            continue
        methodInputValues = extract_param_values(paramValues, stepId)
        appState['widget_outputs'][
            stepId] = app_state_output_into_method_output(
                workspace, token, wsClient, methodSpec, methodInputValues,
                rpcOut)
    appState['job_id'] = "njs:" + appState['job_id']
    return appState
Exemple #3
0
def _method_get_state(workspace, token, URLS, job_manager, method_spec_json,
                      param_values_json, method_job_id):
    methodSpec = json.loads(method_spec_json)
    methodInputValues = json.loads(
        correct_method_specs_json(param_values_json))
    njsClient = NarrativeJobService(URLS.job_service, token=token)
    wsClient = workspaceService(URLS.workspace, token=token)
    if method_job_id.startswith("method:"):
        method_job_id = method_job_id[7:]
        is_async = is_async_method(methodSpec)
        appState = None
        if is_async:
            # It's an SDK method, we use narrative proxy user to deal with sharing
            ujs_proxy = job_manager.proxy_client()
            appState = ujs_proxy.check_app_state(method_job_id)
        else:  # If it's NJS script method then we cannot use narrative proxy user
            appState = njsClient.check_app_state(method_job_id)
        for stepId in appState['step_outputs']:
            rpcOut = appState['step_outputs'][stepId]
            appState['widget_outputs'] = app_state_output_into_method_output(
                workspace, token, wsClient, methodSpec, methodInputValues,
                rpcOut)
        appState['job_id'] = "method:" + appState['job_id']
        return appState
    else:
        input = {}
        rpcArgs = prepare_generic_method_input(token, workspace, methodSpec,
                                               methodInputValues, input)
        output = method_job_id
        methodOut = prepare_generic_method_output(token, workspace, methodSpec,
                                                  input, output)
        return methodOut
Exemple #4
0
def _method_call(meth, method_spec_json, param_values_json):
    """Generic service method calls

    :param method_spec_json: Method descriptor JSON string
    :type method_spec_json: kbtypes.Unicode
    :ui_name method_spec_json: Method descriptor JSON string
    :param param_values_json: Parameter values JSON string
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Parameter values JSON string
    :return: Service method response
    :rtype: kbtypes.Unicode
    """
    token = os.environ['KB_AUTH_TOKEN']
    workspace = os.environ['KB_WORKSPACE_ID']
    methodSpec = json.loads(method_spec_json)
    paramValues = json.loads(param_values_json)
    methodOut = None

    if is_script_method(methodSpec):
        wsClient = workspaceService(service.URLS.workspace, token = token)
        steps = []
        methodId = methodSpec['info']['id']
        app = { 'name' : 'App wrapper for method ' + methodId,'steps' : steps }
        steps.append(create_app_step(workspace, token, wsClient, methodSpec, paramValues, methodId, True))
        njsClient = NarrativeJobService(service.URLS.job_service, token = token)
        appState = njsClient.run_app(app)
        jobId = "method:" + appState["job_id"]
        meth.register_app(jobId)
        methodOut = {'job_id': jobId}
    else:
        input = {}
        rpcArgs = prepare_generic_method_input(token, workspace, methodSpec, paramValues, input);
        behavior = methodSpec['behavior']
        url = behavior['kb_service_url']
        serviceName = behavior['kb_service_name']
        methodName = behavior['kb_service_method']
        if serviceName:
            methodName = serviceName + '.' + methodName
        genericClient = GenericService(url = url, token = token)
        output = genericClient.call_method(methodName, rpcArgs)
        methodOut = prepare_generic_method_output(token, workspace, methodSpec, input, output)
        if 'job_id_output_field' in methodSpec:
            jobIdField = methodSpec['job_id_output_field']
            if jobIdField in methodOut:
                meth.register_job(methodOut[jobIdField])
    return json.dumps(methodOut)
Exemple #5
0
def __init_client(client_name):
    if client_name == 'workspace':
        c = Workspace(URLS.workspace)
    elif client_name == 'job_service':
        c = NarrativeJobService(URLS.job_service)
    elif client_name == 'narrative_method_store':
        c = NarrativeMethodStore(URLS.narrative_method_store)
    elif client_name == 'user_and_job_state':
        c = UserAndJobState(URLS.user_and_job_state)
    elif client_name == 'catalog':
        c = Catalog(URLS.catalog)

    else:
        raise ValueError('Unknown client name "%s"' % client_name)

    __clients[client_name] = c
    return c
Exemple #6
0
def __init_client(client_name, token=None):
    if client_name == 'workspace':
        c = Workspace(URLS.workspace, token=token)
    elif client_name == 'job_service':
        c = NarrativeJobService(URLS.job_service, token=token)
    elif client_name == 'narrative_method_store':
        c = NarrativeMethodStore(URLS.narrative_method_store, token=token)
    elif client_name == 'user_and_job_state':
        c = UserAndJobState(URLS.user_and_job_state, token=token)
    elif client_name == 'catalog':
        c = Catalog(URLS.catalog, token=token)
    elif client_name == 'service' or client_name == 'service_wizard':
        c = ServiceClient(URLS.service_wizard,
                          use_url_lookup=True,
                          token=token)
    elif client_name == 'job_service_mock':
        c = JobServiceMock()
    else:
        raise ValueError('Unknown client name "%s"' % client_name)

    __clients[client_name] = c
    return c
Exemple #7
0
def _app_call(meth, app_spec_json, method_specs_json, param_values_json):
    """Makes a call to the app service

    :param app_spec_json: The App Spec
    :type app_spec_json: kbtypes.Unicode
    :ui_name app_spec_json: The App Spec
    :param method_specs_json: The Method Specs
    :type method_specs_json: kbtypes.Unicode
    :ui_name method_specs_json: The Method Specs
    :param param_values_json: Param values
    :type param_values_json: kbtypes.Unicode
    :ui_name param_values_json: Param values
    :rtype: kbtypes.Unicode
    :return: running job info
    """
    token, workspace = meth.token, meth.workspace_id

    appSpec = json.loads(app_spec_json)
    paramValues = json.loads(correct_method_specs_json(param_values_json))

    methIdToSpec = json.loads(correct_method_specs_json(method_specs_json))

    #raise ValueError("========\nExternal=" + method_specs_json + "\n=======================\nInternal=" + json.dumps(methIdToSpec))

    app = create_app_for_njs(workspace, token, service.URLS,
                             appSpec['info']['id'], appSpec['steps'],
                             methIdToSpec, paramValues)

    #raise ValueError("App sending to NJS: " + json.dumps(app))
    meth.debug(json.dumps(app))
    njsClient = NarrativeJobService(service.URLS.job_service, token=token)
    appState = njsClient.run_app(app)

    # assuming we get a job ID out of this, do the following:
    job_id = "njs:" + appState["job_id"]
    meth.register_app(job_id)

    return json.dumps({'job_id': job_id, 'app': app, 'app_state': appState})
Exemple #8
0
def _method_get_state(workspace, token, URLS, job_manager, method_spec_json,
                      param_values_json, method_job_id):
    methodSpec = json.loads(method_spec_json)
    methodInputValues = json.loads(param_values_json)
    njsClient = NarrativeJobService(URLS.job_service, token=token)
    wsClient = workspaceService(URLS.workspace, token=token)
    if method_job_id.startswith("method:"):
        method_job_id = method_job_id[7:]
        appState = njsClient.check_app_state(method_job_id)
        for stepId in appState['step_outputs']:
            rpcOut = appState['step_outputs'][stepId]
            appState['widget_output'] = app_state_output_into_method_output(
                workspace, token, wsClient, methodSpec, methodInputValues,
                rpcOut)
        appState['job_id'] = "method:" + appState['job_id']
        return appState
    else:
        input = {}
        rpcArgs = prepare_generic_method_input(token, workspace, methodSpec,
                                               methodInputValues, input)
        output = method_job_id
        methodOut = prepare_generic_method_output(token, workspace, methodSpec,
                                                  input, output)
        return methodOut