def cmd_run(request): if request.method=='GET': form=CmdInputForm() return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form})) elif request.method=='POST': form=CmdInputForm(request.POST) if form.is_valid(): target=form.cleaned_data['target'].replace(' ','') mapping=form.cleaned_data['mapping'].replace(' ','') command=form.cleaned_data['cmdline'].lstrip(' ').rstrip(' ') result,info=parse_target_params(target,mapping) if result is None: return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form,'error':info})) if juge_danger_cmd(command): error='Danger Command !!!!' return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form,'cmd_error':error})) if not request.user.is_superuser: if not command.startswith('ls'): error='Permission Denied ! ' return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form,'cmd_error':error})) target=info client=SaltByLocalApi('/etc/salt/master') output=client.client.cmd(target,'cmd.run',[command],expr_form=mapping) if output is None or output == {}: error='Bad Target Host !!!' return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form,'error':error})) result='' hosts=[] for k,v in output.iteritems(): hosts.append(k) result=result+'\n\n------'+k+'-----\n\n'+v f=open('/tmp/.cmd_run.out','w') f.write(result) f.close() code,result=commands.getstatusoutput('cat /tmp/.cmd_run.out') if target == '*': cmd_info=CmdRunLogModel(user=request.user, time=datetime.now(), target=target, mapping=mapping, cmd=command, hosts='all hosts', total=len(hosts)) else: cmd_info=CmdRunLogModel(user=request.user, time=datetime.now(), target=target, mapping=mapping, cmd=command, hosts=','.join(hosts), total=len(hosts)) try: cmd_info.save() except: pass return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form,'result':result})) else: return render_to_response('salt_cmd_run.html',RequestContext(request,{'form':form})) else: return HttpResponseNotAllowed(request)
def cmd_run(request): if request.method == 'GET': form = CmdInputForm() return render_to_response('salt_cmd_run.html', RequestContext(request, {'form': form})) elif request.method == 'POST': form = CmdInputForm(request.POST) if form.is_valid(): target = form.cleaned_data['target'].replace(' ', '') mapping = form.cleaned_data['mapping'].replace(' ', '') command = form.cleaned_data['cmdline'].lstrip(' ').rstrip(' ') result, info = parse_target_params(target, mapping) if result is None: return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'error': info })) if juge_danger_cmd(command): error = 'Danger Command !!!!' return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'cmd_error': error })) if not request.user.is_superuser: flag = True if command.startswith('ls'): flag = False if command.startswith('/root/deploy/deploy_test.sh'): num, output = commands.getstatusoutput(command) if num != 0: error = 'please checkout your command format !' return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'cmd_error': error })) cmd_info = CmdRunLogModel(user=request.user, time=datetime.now(), target=target, mapping=mapping, cmd=command, hosts='self', total=1) cmd_info.save() return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'result': output })) if ';' in command or r'&' in command or r'|' in command: flag = True if flag: error = 'Permission Denied ! ' return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'cmd_error': error })) target = info client = SaltByLocalApi('/etc/salt/master') output = client.client.cmd(target, 'cmd.run', [command], expr_form=mapping) if output is None or output == {}: error = 'Bad Target Host !!!' return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'error': error })) result = '' hosts = [] for k, v in output.iteritems(): hosts.append(k) result = result + '\n\n------' + k + '-----\n\n' + v f = open('/tmp/.cmd_run.out', 'w') f.write(result) f.close() code, result = commands.getstatusoutput('cat /tmp/.cmd_run.out') if target == '*': cmd_info = CmdRunLogModel(user=request.user, time=datetime.now(), target=target, mapping=mapping, cmd=command, hosts='all hosts', total=len(hosts)) else: cmd_info = CmdRunLogModel(user=request.user, time=datetime.now(), target=target, mapping=mapping, cmd=command, hosts=','.join(hosts), total=len(hosts)) try: cmd_info.save() except: pass return render_to_response( 'salt_cmd_run.html', RequestContext(request, { 'form': form, 'result': result })) else: return render_to_response('salt_cmd_run.html', RequestContext(request, {'form': form})) else: return HttpResponseNotAllowed(request)