Esempio n. 1
0
def execute_remote (workflow="mq2.ros", host="localhost", port=8080, args={}, library_path=["."]):
    """ Execute the workflow remotely via a web API. """
    workflow_spec = Workflow.get_workflow (workflow, library_path)
    return requests.post (
        url = f"{host}:{port}/api/executeWorkflow",
        json = {
            "workflow" : workflow_spec,
            "args"     : args
        })
Esempio n. 2
0
def main ():
    arg_parser = argparse.ArgumentParser(
        description='Ros Workflow CLI',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=60))
    arg_parser.add_argument('-a', '--api', help="Execute via API instead of locally.", action="store_true")
    arg_parser.add_argument('-w', '--workflow', help="Workflow to execute.", default="mq2.ros")
    arg_parser.add_argument('-s', '--server', help="Hostname of api server", default="localhost")
    arg_parser.add_argument('-p', '--port', help="Port of the server", default="80")
    arg_parser.add_argument('-i', '--arg', help="Add an argument expressed as key=val", action='append', default=[])
    arg_parser.add_argument('-o', '--out', help="Output the workflow result graph to a file. Use 'stdout' to print to terminal.")
    arg_parser.add_argument('-l', '--lib_path', help="A directory containing workflow modules.", action='append', default=["."])
    arg_parser.add_argument('-n', '--ndex_id', help="Publish the graph to NDEx")
    arg_parser.add_argument('--validate', help="Validate inputs and outputs", action="store_true")
    args = arg_parser.parse_args ()

    setup_logging ()

    #start_task_queue ()
    
    """ Parse input arguments. """
    wf_args = { k : v for k, v in [ arg.split("=") for arg in args.arg ] }
    response = None
    if args.api:
        """ Invoke a remote API endpoint. """
        response = execute_rmote (workflow=args.workflow,
                                  host=args.server,
                                  port=args.port,
                                  args=wf_args)
    else:
        """ Execute the workflow in process. """
        executor = CeleryDAGExecutor (
            spec=Workflow.get_workflow (workflow=args.workflow,
                                        inputs=wf_args,
                                        library_path=args.lib_path))
        response = executor.execute ()
        if args.ndex_id:
            jsonpath_query = parse ("$.[*].result_list.[*].[*].result_graph")
            graph = [ match.value for match in jsonpath_query.find (response) ]
            logger.debug (f"{args.ndex_id} => {json.dumps(graph, indent=2)}")
            ndex = NDEx ()
            ndex.publish (args.ndex_id, graph)
    if args.out:
        if args.out == "stdout":
            logger.debug (f"{graph_text}")
        else:
            with open(args.out, "w") as stream:
                stream.write (json.dumps(response, indent=2))
Esempio n. 3
0
def main():
    arg_parser = argparse.ArgumentParser(
        description='Ros Workflow CLI',
        formatter_class=lambda prog: argparse.ArgumentDefaultsHelpFormatter(
            prog, max_help_position=60))
    arg_parser.add_argument('-a',
                            '--api',
                            help="URL of the remote Ros server to use.",
                            action="store_true")
    arg_parser.add_argument('-w',
                            '--workflow',
                            help="Workflow to execute.",
                            default="workflow_one.ros")
    arg_parser.add_argument('-s',
                            '--server',
                            help="Hostname of api server",
                            default="http://localhost:5002")
    arg_parser.add_argument('-i',
                            '--arg',
                            help="Add an argument expressed as key=val",
                            action='append',
                            default=[])
    arg_parser.add_argument(
        '-o',
        '--out',
        help=
        "Output the workflow result graph to a file. Use 'stdout' to print to terminal."
    )
    arg_parser.add_argument('-l',
                            '--libpath',
                            help="A directory containing workflow modules.",
                            action='append',
                            default=["."])
    arg_parser.add_argument(
        '-n',
        '--ndex',
        help=
        "Name of the graph to publish to NDEx. Requires valid ~/.ndex credential file."
    )
    arg_parser.add_argument('--validate',
                            help="Validate inputs and outputs",
                            action="store_true")
    args = arg_parser.parse_args()

    LoggingUtil.setup_logging()
    """ Parse input arguments. """
    wf_args = {k: v for k, v in [arg.split("=") for arg in args.arg]}
    response = None
    if args.api:
        """ Use the Ros client to run a workflow remotely. """
        client = Client(url=args.server)
        ros_result = client.run(workflow=args.workflow,
                                args=wf_args,
                                library_path=args.libpath)
        response = ros_result.result

    else:
        """ Execute locally via python async. """
        executor = AsyncioExecutor(workflow=Workflow.get_workflow(
            workflow=args.workflow, inputs=wf_args, library_path=args.libpath))
        tasks = [asyncio.ensure_future(executor.execute())]
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.wait(tasks))

        response = tasks[0].result()

    if args.ndex:
        """ Output to NDEx. """
        jsonkit = JSONKit()
        graph = jsonkit.select("$.[*][*].result_list.[*][*].result_graph",
                               response)
        logger.debug(
            f"Publishing result as NDEx graph({args.ndex})=> {json.dumps(graph, indent=2)}"
        )
        NDEx()._publish(args.ndex, graph)

    if args.out:
        """ Write to a file, possibly standard ouput. """
        if args.out == "stdout":
            print(f"{json.dumps(response, indent=2)}")
        else:
            with open(args.out, "w") as stream:
                json.dump(response, stream, indent=2)