def run_workflow(request):
    svc = get_service("workflow")
    global start_time
    start_time = time.time()
    class_id = request.cls.__name__
    print_status("WF plugin starting.")
    # Check if we should use the files generated by last run instead of starting a new one.
    last_run_key = "wf_plugin/latest_result_dir/{}".format(class_id)
    use_last_run = request.config.option.use_last_run
    if use_last_run:
        print_status(
            "--use-last-run flag set. Using output files from the previous run."
        )
        prev_dir = request.config.cache.get(last_run_key, None)
        if prev_dir:
            print_status("Previous file dir found: %s." % prev_dir)
            # use the previous directory if it exists
            if os.path.exists(prev_dir):
                request.cls.result_dir = prev_dir
                return
            # previous directory doesn't exists.  Remove the key and continue with the run.
            print_status(
                "WARN: '%s' doesn't exist anymore.  Rerunning workflow." %
                prev_dir)
            request.config.cache.set(last_run_key, None)
        else:
            print_status("Previous file dir not found. Starting a new run.")

    base_result_dir = request.config.getini("base_upload_bucket")
    project_name = request.config.option.project
    run_mode = request.config.option.run_mode
    run_id = request.config.option.run_id
    storage_type = request.config.option.storage_type
    # Use the profile specified by the TestCase if it exists. Use the default profile if none is provided.
    profile = getattr(request.cls, "profile", request.config.option.profile)
    revision = None

    if run_mode == "repository":
        build_context = request.config.option.repository
        revision = request.config.option.revision
        build_source = "git"
        if not build_context:
            print(
                "ERROR: Your must specify a --repository when --runmode = repository"
            )
            sys.exit(-1)

    else:
        path = os.path.abspath(
            os.path.expanduser(request.config.option.local_dir))
        p = package_and_upload(svc, "local_workflow", path)
        build_source = "url"
        build_context = p

    # Add the parameters if the TestCase defines them
    params = getattr(request.cls, "params", {})

    # add result_dir
    result_dir = "{prefix}/{run}/{instance}/".format(prefix=base_result_dir,
                                                     run=run_id,
                                                     instance=class_id)
    params["result_dir"] = result_dir
    print_status(
        "Workflow run parameters:\n\tWorkflow repository: %s:%s\n\tProject: %s\n\tProfile: %s\n\tParameters: %s"
        % (
            build_context,
            revision,
            project_name,
            profile,
            pprint.pformat(params, width=-1),
        ))

    job = svc.post_job(None,
                       project_name,
                       params,
                       build_context,
                       revision,
                       build_source=build_source,
                       build_context=build_context,
                       profile=profile,
                       storage_type=storage_type)
    last_status = ""
    while job.running:
        time.sleep(5)
        job.refresh()
        if last_status == job.status:
            print_status(".", newline=False)
        else:
            print_status("\nJob %s status = %s" % (job.job_id, job.status),
                         newline=False)
        last_status = job.status

    print_status("\nJob %s is no longer running. Status = %s" %
                 (job.job_id, job.status))

    if not job.status == "COMPLETED":
        print_status(job.logs("pod", ""))
        raise Exception("Job did not complete successfully")

    temp_dir = tempfile.mkdtemp()

    _download_all(result_dir, temp_dir)

    request.cls.result_dir = temp_dir
    # Store the latest result dir in cache
    request.config.cache.set(last_run_key, temp_dir)
    print_status("WF plugin run finished.")
Ejemplo n.º 2
0
def get_service():
    svc = nextcode.get_service("query")
    return svc
Ejemplo n.º 3
0
def get_queryserver():
    """
    Helper method to get a query server instance
    """
    svc = nextcode.get_service("queryserver")
    return svc