def create(self, validated_data): name = validated_data["name"] job = validated_data["job"] command = RawCommand(job) parsed_command = command.parse() obj, created = JobTemplate.objects.update_or_create( name=name, defaults={"job": json.dumps(parsed_command[0])}) return obj
def test_simple_command(): command = RawCommand("salt '*' cmd.run 'echo foobar'") parsed = command.parse() assert parsed[0]["tgt_type"] == "glob" assert parsed[0]["tgt"] == "*" assert parsed[0]["fun"] == "cmd.run" assert parsed[0]["client"] == "local" assert parsed[0]["arg"] == ["echo foobar"]
def test_complex_command(): command = RawCommand("salt -G 'os:Fedora' cmd.run 'echo foobar' test=True") parsed = command.parse() assert parsed[0]["tgt_type"] == "grain" assert parsed[0]["tgt"] == "os:Fedora" assert parsed[0]["fun"] == "cmd.run" assert parsed[0]["client"] == "local" assert parsed[0]["arg"] == ["echo foobar", "test=True"]
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)
def test_client_kwarg(): command = RawCommand( "salt --client=runner pillar.show_top kwarg='{foo: bar}'") parsed = command.parse() assert parsed[0]["fun"] == "pillar.show_top" assert parsed[0]["client"] == "runner" assert "kwarg" in parsed[0] command = RawCommand("salt --client=wheel key.list_all kwarg='{foo: bar}'") parsed = command.parse() assert parsed[0] == { "fun": "key.list_all", "client": "wheel", "kwarg": "{foo: bar}", }
def test_timeout(): command = RawCommand("salt --client=local '*' state.apply test=True -t 9") parsed = command.parse() assert parsed[0]["timeout"] == 9
def test_batch(): command = RawCommand( "salt --client=local_batch '*' state.apply test=True -b 1") parsed = command.parse() assert parsed[0]["batch"] == "1"
def test_failed_cmd(): command = RawCommand("salt '*'") parsed = command.parse() assert parsed == "Command or target not specified"
def test_client(): command = RawCommand("salt --client=runner pillar.show_top") parsed = command.parse() assert parsed[0]["fun"] == "pillar.show_top" assert parsed[0]["client"] == "runner" command = RawCommand("salt --client=runner pillar.show_top master") parsed = command.parse() assert parsed[0] == { "fun": "pillar.show_top", "client": "runner", "arg": ["master"], } command = RawCommand("salt --client=wheel key.list_all") parsed = command.parse() assert parsed[0] == {"fun": "key.list_all", "client": "wheel"} command = RawCommand("salt --client=wheel key.finger master") parsed = command.parse() assert parsed[0] == { "fun": "key.finger", "client": "wheel", "arg": ["master"] } command = RawCommand("salt --client=foobar key.finger master") parsed = command.parse() assert parsed == "Client not implemented: foobar"