示例#1
0
def batch_scripts_func(*args, **kwargs):

    current_task.update_state(state='PROGRESS')
    jr_obj = JobsResults.objects.get(id=args[0])
    ts_obj = TaskSchedule.objects.get(task_id=batch_scripts_func.request.id)

    jr_obj.task_schedule.add(ts_obj)

    script_obj = ScriptsManage.objects.get(id=args[1])
    timeout = args[2]
    input_args = args[3]
    asset_obj = Asset.objects.get(id=args[4])
    ts_obj.asset = asset_obj
    ts_obj.save()

    script_dir, script_name = generate_scripts(script_obj.id,
                                               script_obj.content)
    script_type = script_obj.get_type_display()

    if script_type == "shell" or script_type == "bat":
        script_env = "/bin/bash"
    else:
        script_env = "/usr/bin/python"

    user = jr_obj.key.user
    keypass = jr_obj.key.password
    keyfile = generate_keyfile(jr_obj.key.id, jr_obj.key.private)

    localfile = "{}/{}".format(script_dir, script_name)
    remotefile = "/tmp/{}".format(script_name)

    batch_script = Connection(ip=asset_obj.ip,
                              user=user,
                              port=asset_obj.port,
                              key=keyfile,
                              remote_file=remotefile,
                              password=keypass,
                              keypass=jr_obj.key.password,
                              timeout=timeout)
    batch_script.upload(localfile)

    if len(input_args) > 0:
        cmd = "{} {} {}".format(script_env, remotefile, ' '.join(input_args))
    else:
        cmd = "{} {}".format(script_env, remotefile)

    ret = batch_script.linux_cmd(cmd)
    if ret["result"]:
        ts_obj.log = "on {} server execution {} successed".format(
            asset_obj.hostname, script_obj.name)
    else:
        ts_obj.log = "on {} server execution {} failed".format(
            asset_obj.hostname, script_obj.name)

    ts_obj.save()
    return json.dumps(ret["message"], ensure_ascii=False, cls=OpenSaEncoder)
示例#2
0
def batch_task_func(*args, **kwargs):
    task_dict, message_dict = {}, {}
    message_list = []
    current_task.update_state(state='PROGRESS')
    jr_obj = JobsResults.objects.get(id=args[0])
    ts_obj = TaskSchedule.objects.get(task_id=batch_task_func.request.id)

    jr_obj.task_schedule.add(ts_obj)

    task_obj = TaskScheduling.objects.get(id=args[1])
    timeout = args[2]
    asset_obj = Asset.objects.get(id=args[3])
    ts_obj.asset = asset_obj
    ts_obj.save()
    user = jr_obj.key.user
    keypass = jr_obj.key.password
    keyfile = generate_keyfile(jr_obj.key.id, jr_obj.key.private)

    for i in task_obj.task_scripts.all().order_by('level'):
        script_obj = ScriptsManage.objects.get(id=i.scripts.id)
        script_dir, script_name = generate_scripts(script_obj.id,
                                                   script_obj.content)
        script_type = script_obj.get_type_display()
        try:
            if script_type == "shell" or script_type == "bat":
                script_env = "/bin/bash"
            else:
                script_env = "/usr/bin/python"

            localfile = "{}/{}".format(script_dir, script_name)
            remotefile = "/tmp/{}".format(script_name)

            batch_script = Connection(ip=asset_obj.ip,
                                      user=user,
                                      port=asset_obj.port,
                                      key=keyfile,
                                      remote_file=remotefile,
                                      keypass=keypass,
                                      timeout=timeout)

            batch_script.upload(localfile)
            cmd = "{} {}".format(script_env, remotefile)

            time.sleep(i.delaytime)
            ret = batch_script.task_cmd(cmd)

            message = "script name:{} level:{} server:{}\\nresult:\\n{}\\n".format(
                script_obj.name, i.level, asset_obj.ip,
                ret["message"].decode())
            message_list.append(message)

            if ret["result"] and ret["status"]:

                task_dict['{}_{}'.format(script_obj.name,
                                         i.level)] = _("execution successed")

            else:
                if ret["result"]:
                    log = _("execution success,exception exit")
                else:
                    log = _("execution success,error exit")

                if i.status == 1:
                    task_dict['{}_{}'.format(script_obj.name, i.level)] = log

                else:
                    task_dict['{}_{}'.format(script_obj.name, i.level)] = log
                    break

        except Exception as e:
            task_dict['{}_{}'.format(script_obj.name,
                                     i.level)] = _("execution failed")
            message = "script name:{}\\nlevel:{}\\nresult:\\n{}\\n".format(
                script_obj.name, i.level, e)
            message_list.append(message)
    ts_obj.log = task_dict
    ts_obj.save()
    return json.dumps(message_list, ensure_ascii=False, cls=OpenSaEncoder)