提交 24cf41b8 编写于 作者: Z zhaoke

Merge branch 'main' of git.zcorp.cc:pangu/zentaoatf

package execHelper
import (
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
websocketHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/websocket"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
"github.com/fatih/color"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/websocket"
)
func PrintErrMsg(key string, err error, wsMsg *websocket.Message) (
stdOutput string, errOutput string) {
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(err.Error(), "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(color.FgRed, err.Error())
logUtils.ExecFilef(err.Error())
return "", err.Error()
}
......@@ -3,20 +3,15 @@ package execHelper
import (
"bufio"
"context"
"fmt"
scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
"io"
"os"
"os/exec"
"strings"
"time"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
configHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/config"
langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
websocketHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/websocket"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
......@@ -34,74 +29,16 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
lang := langHelper.GetLangByFile(filePath)
var cmd *exec.Cmd
uuidString := uuid.Must(uuid.NewV4()).String()
_, _, _, _, timeout := scriptHelper.GetCaseInfo(filePath)
if timeout == 0 {
timeout = 86400 * 7
}
ctxt, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
defer cancel()
uuidString := uuid.Must(uuid.NewV4()).String()
if commonUtils.IsWin() {
scriptInterpreter := ""
if strings.ToLower(lang) != "bat" {
scriptInterpreter = configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
}
if scriptInterpreter != "" {
if strings.Index(strings.ToLower(scriptInterpreter), "autoit") > -1 {
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "|", "more")
} else {
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, command, filePath, "-uuid", uuidString)
} else {
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "-uuid", uuidString)
}
}
} else if strings.ToLower(lang) == "bat" {
cmd = exec.CommandContext(ctxt, "cmd", "/C", filePath, "-uuid", uuidString)
} else {
msg := i118Utils.I118Prt.Sprintf("no_interpreter_for_run", lang, filePath)
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(-1, msg)
logUtils.ExecFilef(msg)
}
} else {
err := os.Chmod(filePath, 0777)
if err != nil {
msg := i118Utils.I118Prt.Sprintf("exec_cmd_fail", filePath, err.Error())
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(-1, msg)
logUtils.ExecFilef(msg)
}
//filePath = "\"" + filePath + "\""
scriptInterpreter := configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
if scriptInterpreter != "" {
msg := fmt.Sprintf("use interpreter %s", scriptInterpreter)
if commConsts.ExecFrom == commConsts.FromClient {
//websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
logUtils.ExecConsolef(-1, msg)
}
//logUtils.ExecFilef(msg)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
defer cancel()
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
cmd = exec.CommandContext(ctxt, scriptInterpreter, command, filePath)
} else {
cmd = exec.CommandContext(ctxt, scriptInterpreter, filePath)
}
} else {
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
filePath = fmt.Sprintf("%s %s %s", lang, command, filePath)
}
cmd = exec.CommandContext(ctxt, "/bin/bash", "-c", fmt.Sprintf("%s -uuid %s", filePath, uuidString))
}
}
cmd := getCommand(filePath, lang, uuidString, conf, ctx, wsMsg)
if cmd == nil {
msgStr := i118Utils.Sprintf("cmd_empty")
......@@ -123,21 +60,9 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
stderr, err2 := cmd.StderrPipe()
if err1 != nil {
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(err1.Error(), "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(color.FgRed, err1.Error())
logUtils.ExecFilef(err1.Error())
return "", err1.Error()
return PrintErrMsg(key, err1, wsMsg)
} else if err2 != nil {
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(err2.Error(), "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(color.FgRed, err2.Error())
logUtils.ExecFilef(err2.Error())
return "", err2.Error()
return PrintErrMsg(key, err2, wsMsg)
}
cmd.Start()
......@@ -226,7 +151,7 @@ ExitCurrCase:
errOutputArr = append(errOutputArr, line)
}
}
if ctxt.Err() != nil {
if ctx.Err() != nil {
errOutputArr = append(errOutputArr, i118Utils.Sprintf("exec_cmd_timeout"))
}
......
......@@ -2,21 +2,115 @@ package execHelper
import (
"bytes"
"os/exec"
"path/filepath"
"regexp"
"strings"
"context"
"fmt"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
configHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/config"
websocketHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/websocket"
"github.com/easysoft/zentaoatf/internal/server/core/dao"
"github.com/easysoft/zentaoatf/internal/server/modules/v1/model"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
shellUtils "github.com/easysoft/zentaoatf/pkg/lib/shell"
stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/websocket"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
func getCommand(filePath, lang, uuidString string, conf commDomain.WorkspaceConf, ctx context.Context, wsMsg *websocket.Message) (
cmd *exec.Cmd) {
if commonUtils.IsWin() {
cmd = setWinScriptInterpreter(filePath, lang, uuidString, conf, ctx, wsMsg)
} else {
cmd = setLinuxScriptInterpreter(filePath, lang, uuidString, conf, ctx, wsMsg)
}
return
}
func setWinScriptInterpreter(filePath, lang, uuidString string, conf commDomain.WorkspaceConf, ctx context.Context, wsMsg *websocket.Message) (
cmd *exec.Cmd) {
key := stringUtils.Md5(filePath)
scriptInterpreter := ""
if strings.ToLower(lang) != "bat" {
scriptInterpreter = configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
}
if scriptInterpreter != "" {
if strings.Index(strings.ToLower(scriptInterpreter), "autoit") > -1 {
cmd = exec.CommandContext(ctx, "cmd", "/C", scriptInterpreter, filePath, "|", "more")
} else {
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
cmd = exec.CommandContext(ctx, "cmd", "/C", scriptInterpreter, command, filePath, "-uuid", uuidString)
} else {
cmd = exec.CommandContext(ctx, "cmd", "/C", scriptInterpreter, filePath, "-uuid", uuidString)
}
}
} else if strings.ToLower(lang) == "bat" {
cmd = exec.CommandContext(ctx, "cmd", "/C", filePath, "-uuid", uuidString)
} else {
msg := i118Utils.I118Prt.Sprintf("no_interpreter_for_run", lang, filePath)
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(-1, msg)
logUtils.ExecFilef(msg)
}
return
}
func setLinuxScriptInterpreter(filePath, lang, uuidString string, conf commDomain.WorkspaceConf, ctx context.Context, wsMsg *websocket.Message) (
cmd *exec.Cmd) {
key := stringUtils.Md5(filePath)
err := os.Chmod(filePath, 0777)
if err != nil {
msg := i118Utils.I118Prt.Sprintf("exec_cmd_fail", filePath, err.Error())
if commConsts.ExecFrom == commConsts.FromClient {
websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
}
logUtils.ExecConsolef(-1, msg)
logUtils.ExecFilef(msg)
}
//filePath = "\"" + filePath + "\""
scriptInterpreter := configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
if scriptInterpreter != "" {
msg := fmt.Sprintf("use interpreter %s", scriptInterpreter)
if commConsts.ExecFrom == commConsts.FromClient {
//websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
logUtils.ExecConsolef(-1, msg)
}
//logUtils.ExecFilef(msg)
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
cmd = exec.CommandContext(ctx, scriptInterpreter, command, filePath)
} else {
cmd = exec.CommandContext(ctx, scriptInterpreter, filePath)
}
} else {
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
filePath = fmt.Sprintf("%s %s %s", lang, command, filePath)
}
cmd = exec.CommandContext(ctx, "/bin/bash", "-c", fmt.Sprintf("%s -uuid %s", filePath, uuidString))
}
return
}
func AddInterpreterIfExist(conf *commDomain.WorkspaceConf, lang string) bool {
if commConsts.ExecFrom != commConsts.FromZentao {
return false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册