file.go 6.2 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4
package execHelper

import (
	"bufio"
Z
zhaoke 已提交
5
	"context"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
6
	"fmt"
Z
zhaoke 已提交
7 8 9 10
	"io"
	"os"
	"os/exec"
	"strings"
Z
zhaoke 已提交
11
	"time"
Z
zhaoke 已提交
12

aaronchen2k2k's avatar
aaronchen2k2k 已提交
13 14 15 16
	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"
Z
zhaoke 已提交
17
	scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
18 19 20 21 22
	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"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
23
	"github.com/fatih/color"
雨爱无痕 已提交
24
	"github.com/gofrs/uuid"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
25 26 27 28 29 30 31 32 33 34
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/websocket"
)

func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
	ch chan int, wsMsg *websocket.Message) (
	stdOutput string, errOutput string) {

	key := stringUtils.Md5(filePath)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
35 36
	lang := langHelper.GetLangByFile(filePath)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
37
	var cmd *exec.Cmd
Z
zhaoke 已提交
38 39 40 41 42 43
	_, _, _, _, timeout := scriptHelper.GetCaseInfo(filePath)
	if timeout == 0 {
		timeout = 86400 * 7
	}
	ctxt, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
	defer cancel()
雨爱无痕 已提交
44
	uuidString := ""
aaronchen2k2k's avatar
aaronchen2k2k 已提交
45
	if commonUtils.IsWin() {
雨爱无痕 已提交
46
		uuidString = uuid.Must(uuid.NewV4()).String()
aaronchen2k2k's avatar
aaronchen2k2k 已提交
47 48 49 50 51 52
		scriptInterpreter := ""
		if strings.ToLower(lang) != "bat" {
			scriptInterpreter = configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
		}
		if scriptInterpreter != "" {
			if strings.Index(strings.ToLower(scriptInterpreter), "autoit") > -1 {
雨爱无痕 已提交
53
				cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "|", "more")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
54
			} else {
Z
zhaoke 已提交
55
				if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
雨爱无痕 已提交
56
					cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, command, filePath, "-uuid", uuidString)
Z
zhaoke 已提交
57
				} else {
雨爱无痕 已提交
58
					cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "-uuid", uuidString)
Z
zhaoke 已提交
59
				}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
60 61
			}
		} else if strings.ToLower(lang) == "bat" {
雨爱无痕 已提交
62
			cmd = exec.CommandContext(ctxt, "cmd", "/C", filePath, "-uuid", uuidString)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		} else {
			msg := i118Utils.I118Prt.Sprintf("no_interpreter_for_run", lang, filePath)
			if commConsts.ExecFrom != commConsts.FromCmd {
				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.FromCmd {
				websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
			}
			logUtils.ExecConsolef(-1, msg)
			logUtils.ExecFilef(msg)
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
82 83 84 85
		//filePath = "\"" + filePath + "\""
		scriptInterpreter := configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))

		if scriptInterpreter != "" {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
86 87 88 89 90 91 92 93
			msg := fmt.Sprintf("use interpreter %s", scriptInterpreter)

			if commConsts.ExecFrom != commConsts.FromCmd {
				//websocketHelper.SendOutputMsg(msg, "", iris.Map{"key": key}, wsMsg)
				logUtils.ExecConsolef(-1, msg)
			}
			//logUtils.ExecFilef(msg)

Z
zhaoke 已提交
94
			if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
95
				cmd = exec.CommandContext(ctxt, scriptInterpreter, command, filePath)
Z
zhaoke 已提交
96
			} else {
97
				cmd = exec.CommandContext(ctxt, scriptInterpreter, filePath)
Z
zhaoke 已提交
98
			}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
99
		} else {
Z
zhaoke 已提交
100
			if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
Z
zhaoke 已提交
101
				filePath = fmt.Sprintf("%s %s %s", lang, command, filePath)
Z
zhaoke 已提交
102
			}
Z
zhaoke 已提交
103
			cmd = exec.CommandContext(ctxt, "/bin/bash", "-c", filePath)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
104
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
105 106 107 108 109 110 111 112 113 114 115 116
	}

	if cmd == nil {
		msgStr := i118Utils.Sprintf("cmd_empty")
		if commConsts.ExecFrom != commConsts.FromCmd {
			websocketHelper.SendOutputMsg(msgStr, "", iris.Map{"key": key}, wsMsg)
			logUtils.ExecConsolef(color.FgRed, msgStr)
		}

		logUtils.ExecFilef(msgStr)

		return "", msgStr
Z
zhaoke 已提交
117 118
	} else {
		cmd.Dir = workspacePath
aaronchen2k2k's avatar
aaronchen2k2k 已提交
119 120
	}

Z
zhaoke 已提交
121 122
	cmd.Dir = workspacePath

aaronchen2k2k's avatar
aaronchen2k2k 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	stdout, err1 := cmd.StdoutPipe()
	stderr, err2 := cmd.StderrPipe()

	if err1 != nil {
		if commConsts.ExecFrom != commConsts.FromCmd {
			websocketHelper.SendOutputMsg(err1.Error(), "", iris.Map{"key": key}, wsMsg)
		}
		logUtils.ExecConsolef(color.FgRed, err1.Error())
		logUtils.ExecFilef(err1.Error())

		return "", err1.Error()
	} else if err2 != nil {
		if commConsts.ExecFrom != commConsts.FromCmd {
			websocketHelper.SendOutputMsg(err2.Error(), "", iris.Map{"key": key}, wsMsg)
		}
		logUtils.ExecConsolef(color.FgRed, err2.Error())
		logUtils.ExecFilef(err2.Error())

		return "", err2.Error()
	}

	cmd.Start()
雨爱无痕 已提交
145 146 147 148 149 150 151 152 153
	if commonUtils.IsWin() {
		go func() {
			time.AfterFunc(time.Second*time.Duration(timeout), func() {
				KillProcessByUUID(uuidString)
				stdout.Close()
				stderr.Close()
			})
		}()
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
154 155 156
	isTerminal := false
	reader1 := bufio.NewReader(stdout)
	stdOutputArr := make([]string, 0)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
157

aaronchen2k2k's avatar
aaronchen2k2k 已提交
158 159 160 161 162
	for {
		line, err2 := reader1.ReadString('\n')
		if line != "" {
			if commConsts.ExecFrom != commConsts.FromCmd {
				websocketHelper.SendOutputMsg(line, "", iris.Map{"key": key}, wsMsg)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
163
				logUtils.ExecConsole(-1, line)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
164 165 166 167 168 169 170
			}

			logUtils.ExecFile(line)

			isTerminal = true
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
171 172 173
		if err2 == io.EOF {
			break
		}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
174
		if err2 != nil {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
175
			logUtils.ExecConsole(color.FgRed, err2.Error())
aaronchen2k2k's avatar
aaronchen2k2k 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
			logUtils.ExecFile(err2.Error())
			break
		}

		stdOutputArr = append(stdOutputArr, line)

		select {
		case <-ch:
			msg := i118Utils.Sprintf("exit_exec_curr")

			if commConsts.ExecFrom != commConsts.FromCmd {
				websocketHelper.SendExecMsg(msg, "", commConsts.Run,
					iris.Map{"key": key, "status": "end"}, wsMsg)
			}

			logUtils.ExecConsolef(color.FgCyan, msg)
			logUtils.ExecFilef(msg)

			goto ExitCurrCase
		default:
		}
	}

ExitCurrCase:
	errOutputArr := make([]string, 0)
	if !isTerminal {
		reader2 := bufio.NewReader(stderr)

		for {
			line, err2 := reader2.ReadString('\n')
			if err2 != nil || io.EOF == err2 {
				break
			}
			errOutputArr = append(errOutputArr, line)
		}
	}
Z
zhaoke 已提交
212 213
	if ctxt.Err() != nil {
		errOutputArr = append(errOutputArr, i118Utils.Sprintf("exec_cmd_timeout"))
aaronchen2k2k's avatar
aaronchen2k2k 已提交
214

Z
zhaoke 已提交
215
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
216 217
	stdOutput = strings.Join(stdOutputArr, "")
	errOutput = strings.Join(errOutputArr, "")
Z
zhaoke 已提交
218
	cmd.Wait()
aaronchen2k2k's avatar
aaronchen2k2k 已提交
219 220
	return
}