Ejemplo n.º 1
0
def run(request):
    if request.POST.get("raw"):
        command = RawCommand(request.POST.get("command"))
        parsed_command = command.parse()
        # Schedules.
        if request.POST.get("schedule_type"):
            schedule_type = request.POST.get("schedule_type")
            schedule_name = request.POST.get(
                "schedule_name",
                datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
            schedule_parsed = [{
                "client":
                "local",
                "batch":
                None,
                "tgt_type":
                parsed_command[0]["tgt_type"],
                "tgt":
                parsed_command[0]["tgt"],
                "fun":
                "schedule.add",
                "arg": [
                    schedule_name,
                    "function={}".format(parsed_command[0]["fun"]),
                    "job_args={}".format(parsed_command[0]["arg"]),
                ],
            }]
            if schedule_type == "once":
                schedule_date = request.POST.get("schedule")
                schedule_parsed[0]["arg"].append(
                    "once={}".format(schedule_date))
                schedule_parsed[0]["arg"].append("once_fmt=%Y-%m-%d %H:%M:%S")
            else:
                cron = request.POST.get("cron")
                schedule_parsed[0]["arg"].append("cron={}".format(cron))
            ret = run_raw(schedule_parsed)
            formatted = nested_output.output(ret)
            conv = Ansi2HTMLConverter(inline=False, scheme="xterm")
            html = conv.convert(formatted, ensure_trailing_newline=True)
            return HttpResponse(html)

        ret = run_raw(parsed_command)
        formatted = "\n"
        if (parsed_command[0]["fun"] in ["state.apply", "state.highstate"]
                and parsed_command[0]["client"] != "local_async"):
            for state, out in ret.items():
                minion_ret = highstate_output.output({state: out})
                formatted += minion_ret + "\n\n"
        else:
            for state, out in ret.items():
                minion_ret = nested_output.output({state: out})
                formatted += minion_ret + "\n\n"
        if request.POST.get("cli"):
            return JsonResponse({"results": formatted})
        conv = Ansi2HTMLConverter(inline=False, scheme="xterm")
        html = conv.convert(formatted, ensure_trailing_newline=True)
        return HttpResponse(html)
Ejemplo n.º 2
0
def job_rendered(request, jid, minion_id):
    # Retrieve job from database.
    job = SaltReturns.objects.get(jid=jid, id=minion_id)

    # Use different output.
    if job.fun in ["state.apply", "state.highstate"]:
        formatted = highstate_output.output({minion_id: job.loaded_ret()["return"]})
    else:
        formatted = nested_output.output({minion_id: job.loaded_ret()["return"]})

    # Convert it to html.
    conv = Ansi2HTMLConverter(inline=False, scheme="xterm")
    html_detail = conv.convert(formatted, ensure_trailing_newline=True)
    return Response(html_detail)