Example #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)
Example #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)
Example #3
0
    def conformity_detail(self, request, minion_id):
        minion = self.get_object()

        # Get conformity data.
        _, _, custom_conformity = render_conformity(minion.minion_id)
        custom_conformity = (
            custom_conformity[minion.minion_id] if custom_conformity else None
        )
        minion_conformity = minion.conformity()

        # Convert states to html.
        conv = Ansi2HTMLConverter(inline=False, scheme="xterm")
        last_highstate = minion.last_highstate()
        succeeded, unchanged, failed = {}, {}, {}

        if last_highstate:
            last_highstate = last_highstate.loaded_ret()["return"]
            # Sls error
            if isinstance(last_highstate, list):
                failed = {"error": last_highstate[0]}
            else:
                for state in last_highstate:
                    state_name = state.split("_|-")[1]
                    formatted = highstate_output.output(
                        {minion.minion_id: {state: last_highstate[state]}},
                        summary=False,
                    )
                    if last_highstate[state]["result"] is True:
                        succeeded[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
                    elif last_highstate[state]["result"] is None:
                        unchanged[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
                    else:
                        failed[state_name] = conv.convert(
                            formatted, ensure_trailing_newline=True
                        )
        return Response(
            {
                "custom_conformity": custom_conformity,
                "conformity": minion_conformity,
                "succeeded": succeeded,
                "unchanged": unchanged,
                "failed": failed,
            }
        )