Ejemplo n.º 1
0
def salt_result(request):
    _u = request.user
    line = "################################################################"
    sapi = SaltAPI()
    if request.POST:
        host_list = request.POST.getlist("hosts_name")
        logger.info(host_list)
        salt_fun = request.POST.get('salt_fun')
        salt_arg = request.POST.get('salt_arg').strip()
        cmd = str(salt_fun) + '.' + str(salt_arg)

        host_str = ",".join(host_list)
        logger.info(host_str)
        if salt_arg == '':
            result = sapi.remote_noarg_execution(host_str, salt_fun)
        else:
            result = sapi.remote_execution(host_str, salt_fun, salt_arg)

        cmd_history = Command_history()
        cmd_history.user = _u
        cmd_history.command = cmd
        cmd_history.command_tag = 1
        cmd_history.save()

        minion_count = 'Total: ' + str(len(host_list))
        cmd_succeed = 'Succeed: ' + str(len(result))
        cmd_failure = 'Failure: ' + str(len(host_list) - len(result))
        cmd = 'Command: ' + cmd
        succeed_minion = []
        for i in result:
            succeed_minion.append(i)
        failure_minion = 'Failure_Minion: ' + ','.join(
            list(set(host_list).difference(set(succeed_minion))))

        return render(
            request, 'execute/minions_salt_result.html', {
                'result': result,
                'cmd': cmd,
                'line': line,
                'minion_count': minion_count,
                'cmd_succeed': cmd_succeed,
                'cmd_failure': cmd_failure,
                'failure_minion': failure_minion
            })
Ejemplo n.º 2
0
def salt_result(request):
    _u = request.user
    line = "################################################################"
    sapi = SaltAPI()
    if request.POST:
        host_list = request.POST.getlist("hosts_name")
        logger.info(host_list)
        salt_fun = request.POST.get('salt_fun')
        salt_arg = request.POST.get('salt_arg').strip()
        cmd = str(salt_fun) + '.' + str(salt_arg)

        host_str = ",".join(host_list)
        logger.info(host_str)
        if salt_arg == '':
            result = sapi.remote_noarg_execution_notgt(host_str, salt_fun)
        else:
            result = sapi.remote_execution_notgt(host_str, salt_fun, salt_arg)

        cmd_history = Command_history()
        cmd_history.user = _u
        cmd_history.command = cmd
        cmd_history.command_tag = 1
        cmd_history.save()

        minion_count = 'Total: ' + str(len(host_list))
        cmd_succeed = 'Succeed: ' + str(len(result))
        cmd_failure = 'Failure: ' + str(len(host_list)-len(result))
        cmd = 'Command: ' + cmd
        succeed_minion = []
        for i in result:
            succeed_minion.append(i)
        failure_minion = 'Failure_Minion: ' + ','.join(list(set(host_list).difference(set(succeed_minion))))

        return render(request, 'execute/minions_salt_result.html', {'result': result,
                                                                     'cmd': cmd,
                                                                     'line': line,
                                                                     'minion_count': minion_count,
                                                                     'cmd_succeed': cmd_succeed,
                                                                     'cmd_failure': cmd_failure,
                                                                     'failure_minion': failure_minion
                                                                     })
Ejemplo n.º 3
0
def shell_result(request):
    sapi = SaltAPI()
    _u = request.user
    _user = User.objects.get(username=_u)
    host_list = request.POST.getlist("hosts_name")
    try:
        _userprofile = UserProfiles.objects.get(user=_user)
    except Exception as e:
        return render(request, 'execute/minions_shell_result.html')

    _privileges = _userprofile.privilege.all()

    _deny = []
    _allow = []

    line = "################################################################"
    result = {}
    minion_id_list = []
    
    for _p in _privileges:
        if len(_p.deny) > 0:
            _deny.append(_p.deny)  
        if len(_p.allow) > 0:
            _allow.append(_p.allow)

    if request.POST:
        cmd = request.POST.get("cmd").strip()
        if not _user.is_superuser:
            if _allow:
                _number = 0
                for _a in _allow:
                    _commands_split = _a.split(',')
                    for _cs in _commands_split:
                        _cmd_cs = _cs.strip('\'\"').split()
                        _cmd_cmd = cmd.strip().split()
                        _len_cs = len(''.join(_cmd_cs))
                        _len_cmd = len(''.join(_cmd_cmd))
                        if _len_cs == _len_cmd:
                           _compile = ""
                           for _tmp in _cmd_cs:
                               _compile = _compile + '.*' + str(_tmp)
                           _compile = _compile + '.*'
                           regex = re.compile(r''+_compile+'')
                           if regex.search(cmd) is not None:
                               _number = _number + 1
                if _number  <= 0 :
                    error = "error occurred : Allow Warn! You have no permition run [ " + cmd +" ]"
                    result["result"]=error
                    return render(request, 'execute/minions_shell_result.html', {'result': result, 'cmd': cmd, 'line': line})

            if _deny: 
                _number = 0
                for _a in _deny:
                    _commands_split = _a.split(',')
                    for _cs in _commands_split:
                        _cmd_cs = _cs.strip('\'\"').split()
                        _cmd_cmd = cmd.strip().split()
                        _len_cs = len(''.join(_cmd_cs))
                        _len_cmd = len(''.join(_cmd_cmd))
                        if _len_cs == _len_cmd:
                           _compile = ""
                           for _tmp in _cmd_cs:
                               _compile = _compile + '.*' + str(_tmp)
                           _compile = _compile + '.*'
                           regex = re.compile(r''+_compile+'')
                           if regex.search(cmd) is not None:
                               _number = _number + 1
                if _number >0:
                    error = "error occurred : Deny Warn! You have no permition run [ " + cmd +" ]"
                    result["result"]=error
                    return render(request, 'execute/minions_shell_result.html', {'result': result, 'cmd': cmd, 'line': line})
        else:
            pass

        for _h in host_list:
            try:
                _host = Hosts.objects.get(name=_h)
                minion_id_list.append(_host.minion.minion_id)
            except:
                minion_id_list.append(_h)
                        
        #run cmd now
        host_str = ",".join(minion_id_list)
        # the type of result is dictionary
        result = sapi.shell_remote_execution(host_str, cmd)
        cmd_history = Command_history()
        cmd_history.user = _u
        cmd_history.command = cmd
        cmd_history.save()
        return render(request, 'execute/minions_shell_result.html', {'result': result, 'cmd': cmd, 'line': line})
    return render(request, 'execute/minions_shell_result.html')