Пример #1
0
def cook_a_recipe(user, org, recipe_id):
    """
    Run recipes via the API.
    """
    r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
    if not r:
        raise RequestError(
            'Recipe with id/slug {} does not exist.'.format(recipe_id))

    # setup kwargs for merlynne
    kw = dict(org=org.to_dict(incl_auths=True,
                              auths_as_dict=True,
                              settings_as_dict=True,
                              incl_domains=True,
                              incl_users=True),
              apikey=user.apikey,
              recipe=r.to_dict(),
              recipe_obj=r,
              sous_chef_path=r.sous_chef.runs)

    # cook recipe
    merlynne = Merlynne(**kw)
    try:
        job_id = merlynne.cook_recipe()
    except Exception as e:
        raise RequestError(
            'There was a problem initializing the SousChef: {}'.format(
                e.message))

    # # return job status url
    ret = url_for_job_status(apikey=user.apikey, job_id=job_id, queue='recipe')
    return jsonify(ret, status=202)
Пример #2
0
def cook_a_recipe(user, org, recipe_id):
    """
    Run recipes via the API.
    """
    r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
    if not r:
        raise RequestError('Recipe with id/slug {} does not exist.'
                           .format(recipe_id))

    # setup kwargs for merlynne
    kw = dict(
        org=org.to_dict(
            incl_auths=True,
            auths_as_dict=True,
            settings_as_dict=True,
            incl_domains=True,
            incl_users=True),
        apikey=user.apikey,
        recipe=r.to_dict(),
        recipe_obj=r,
        passthrough=arg_bool('passthrough', default=False),
        sous_chef_path=r.sous_chef.runs
    )

    # initialize merlynne
    merlynne = Merlynne(**kw)
    try:
        resp = merlynne.cook_recipe()
    
    except Exception as e:
        raise RequestError(
            'There was a problem initializing the SousChef: {}'
            .format(e.message))

    # queued job
    if not kw['passthrough']:
        # set its status as 'queued'
        r.status = 'queued'
        db.session.add(r)
        db.session.commit()
        # # return job status url
        ret = url_for_job_status(apikey=user.apikey, job_id=resp, queue='recipe')
        return jsonify(ret, status=202)
    
    # format non-iterables.
    if isinstance(resp, dict):
        resp = [resp]

    # stream results
    def generate():
        for item in resp:
            yield obj_to_json(item) + "\n"

    return Response(stream_with_context(generate()))
Пример #3
0
def cook_a_recipe(user, org, recipe_id):
    """
    Run recipes via the API.
    """
    r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
    if not r:
        raise NotFoundError(
            'Recipe with id/slug {} does not exist.'
            .format(recipe_id))

    if r.status == 'uninitialized':
        raise RequestError(
            'Recipes must be initialized before cooking.')

    # determine passthrough from req method
    passthrough = False
    if request.method == 'POST':
        passthrough = True

    # setup kwargs for merlynne
    kw = dict(
        org=org.to_dict(
            incl_auths=True,
            auths_as_dict=True,
            settings_as_dict=True,
            incl_domains=True,
            incl_users=True),
        apikey=user.apikey,
        recipe=r.to_dict(),
        recipe_obj=r,
        passthrough=arg_bool('passthrough'),
        sous_chef_path=r.sous_chef.runs
    )

    # check for runtme args for passthrough options.
    if kw['passthrough']:

        # parse runtime options from params + body.
        options = {
            k: v for k, v in dict(request.args.items()).items()
            if k not in ['apikey', 'org', 'localize', 'passthrough']
        } 

        # parse the runtime options.
        options.update(request_data())

        # update the recipe
        if len(options.keys()):
            try:
                recipe = recipe_schema.update(
                    r, options, r.sous_chef.to_dict())
            except Exception as e:
                raise RequestError(
                    'Error trying to attempt to pass {} to recipe {} at runtime:\n{}'
                    .format(options, recipes.slug, e.message))
            kw['recipe']['options'].update(recipe.get('options', {}))

    # execute merlynne
    merlynne = Merlynne(**kw)
    resp = merlynne.cook_recipe()

    # queued job
    if not kw['passthrough']:

        # set its status as 'queued'
        r.status = 'queued'
        db.session.add(r)
        db.session.commit()

        # # return job status url
        ret = url_for_job_status(apikey=user.apikey, job_id=resp, queue='recipe')
        return jsonify(ret, status=202)

    # format non-iterables.
    if isinstance(resp, dict):
        resp = [resp]

    # stream results
    def generate():
        for item in resp:
            yield obj_to_json(item) + "\n"

    return Response(stream_with_context(generate()))
Пример #4
0
def cook_a_recipe(user, org, recipe_id):
    """
    Run recipes via the API.
    """
    r = fetch_by_id_or_field(Recipe, 'slug', recipe_id, org_id=org.id)
    if not r:
        raise NotFoundError(
            'Recipe with id/slug {} does not exist.'.format(recipe_id))

    if r.status == 'uninitialized':
        raise RequestError('Recipes must be initialized before cooking.')

    # determine passthrough from req method
    passthrough = False
    if request.method == 'POST':
        passthrough = True

    # setup kwargs for merlynne
    kw = dict(org=org.to_dict(incl_auths=True,
                              auths_as_dict=True,
                              settings_as_dict=True,
                              incl_domains=True,
                              incl_users=True),
              apikey=user.apikey,
              recipe=r.to_dict(),
              recipe_obj=r,
              passthrough=arg_bool('passthrough'),
              sous_chef_path=r.sous_chef.runs)

    # check for runtme args for passthrough options.
    if kw['passthrough']:

        # parse runtime options from params + body.
        options = {
            k: v
            for k, v in dict(request.args.items()).items()
            if k not in ['apikey', 'org', 'localize', 'passthrough']
        }

        # parse the runtime options.
        options.update(request_data())

        # update the recipe
        if len(options.keys()):
            try:
                recipe = recipe_schema.update(r, options,
                                              r.sous_chef.to_dict())
            except Exception as e:
                raise RequestError(
                    'Error trying to attempt to pass {} to recipe {} at runtime:\n{}'
                    .format(options, recipes.slug, e.message))
            kw['recipe']['options'].update(recipe.get('options', {}))

    # execute merlynne
    merlynne = Merlynne(**kw)
    resp = merlynne.cook_recipe()

    # queued job
    if not kw['passthrough']:

        # set its status as 'queued'
        r.status = 'queued'
        db.session.add(r)
        db.session.commit()

        # # return job status url
        ret = url_for_job_status(apikey=user.apikey,
                                 job_id=resp,
                                 queue='recipe')
        return jsonify(ret, status=202)

    # format non-iterables.
    if isinstance(resp, dict):
        resp = [resp]

    # stream results
    def generate():
        for item in resp:
            yield obj_to_json(item) + "\n"

    return Response(stream_with_context(generate()))