diff --git a/agent-wdd/cmd/Base.go b/agent-wdd/cmd/Base.go index c36acf1..5f5f98d 100644 --- a/agent-wdd/cmd/Base.go +++ b/agent-wdd/cmd/Base.go @@ -146,8 +146,57 @@ func addBaseSubcommands(cmd *cobra.Command) { // 清空路由表 log.Info("清空路由表...") - op.HardCodeCommandExecutor("iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -t raw -F") - op.HardCodeCommandExecutor("ip6tables -F && ip6tables -t nat -F && ip6tables -t mangle -F && ip6tables -t raw -F") + + commandExecutor := [][]string{ + { + "iptables", + "-F", + }, + { + "iptables", + "-t", + "nat", + "-F", + }, + { + "iptables", + "-t", + "mangle", + "-F", + }, + { + "iptables", + "-t", + "raw", + "-F", + }, + { + "ip6tables", + "-F", + }, + { + "ip6tables", + "-t", + "nat", + "-F", + }, + { + "ip6tables", + "-t", + "mangle", + "-F", + }, + { + "ip6tables", + "-t", + "raw", + "-F", + }, + } + + for _, command := range commandExecutor { + op.SingleLineCommandExecutor(command) + } log.Info("Firewall 关闭成功!") }, @@ -272,7 +321,7 @@ func addHarborSubcommands(harborCmd *cobra.Command) { // 运行install.sh op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"}) - ok, log1 := op.SingleLineCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"}) + ok, log1 := op.RealTimeCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"}) if !ok { log.Error("安装harbor失败: %s", log1) return diff --git a/agent-wdd/cmd/Proxy.go b/agent-wdd/cmd/Proxy.go index b50aeb9..2234fd7 100644 --- a/agent-wdd/cmd/Proxy.go +++ b/agent-wdd/cmd/Proxy.go @@ -288,7 +288,7 @@ func installXray() { os.Chmod("/tmp/install-release.sh", 0777) // 执行安装脚本 - ok, resultLog := op.HardCodeCommandExecutor("/bin/bash /tmp/install-release.sh -u root install") + ok, resultLog := op.SingleLineCommandExecutor([]string{"/bin/bash", "/tmp/install-release.sh", "-u", "root", "install"}) if !ok { log.Error("Install Xray failed ! error is %s", resultLog) return diff --git a/agent-wdd/op/Excutor.go b/agent-wdd/op/Excutor.go index f442cb8..7e15830 100644 --- a/agent-wdd/op/Excutor.go +++ b/agent-wdd/op/Excutor.go @@ -5,6 +5,7 @@ import ( "bufio" "bytes" "fmt" + "io" "os/exec" "strings" ) @@ -17,7 +18,7 @@ import ( // []string - 合并后的标准输出和标准错误内容(按行分割) func SingleLineCommandExecutor(singleLineCommand []string) (ok bool, resultLog []string) { - log.Info("[Excutor] - start => %v", singleLineCommand) + log.Info("start => %v", singleLineCommand) if len(singleLineCommand) == 0 { return false, nil @@ -79,13 +80,21 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) (ok bool, resultLog []s return false, nil } + // 转换为 管道命令的字符串格式 + // [][]string{{"ps", "aux"}, {"grep", "nginx"}, {"wc", "-l"}} 转换为 ps aux | grep nginx | wc -l + pipeLineCommandStr := "" + // 预检所有子命令 for _, cmd := range pipeLineCommand { if len(cmd) == 0 { return false, nil } + pipeLineCommandStr += fmt.Sprintf("%v | ", cmd) } + pipeLineCommandStr = strings.TrimSuffix(pipeLineCommandStr, " | ") + log.Info("start => %v", pipeLineCommandStr) + // 创建命令组 cmds := make([]*exec.Cmd, len(pipeLineCommand)) for i, args := range pipeLineCommand { @@ -161,37 +170,44 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) (ok bool, resultLog []s return success, output } -// HardCodeCommandExecutor 执行硬编码命令,返回执行结果和输出内容 -// hardCodeCommand: 硬编码命令,如 "echo hello" +// RealTimeCommandExecutor 执行命令,需要实时打印输出执行命令的日志,并返回执行结果和输出内容 +// realTimeCommand: 实时命令,如 "docker load -i /root/wdd/harbor/harbor-offline-installer-v2.10.1.tgz" // 返回值: // // bool - 命令是否执行成功(true为成功,false为失败) // []string - 合并后的标准输出和标准错误内容(按行分割) -func HardCodeCommandExecutor(hardCodeCommand string) (ok bool, resultLog []string) { - - log.Info("[HardCodeCommandExecutor] - start => %v", hardCodeCommand) - - if hardCodeCommand == "" { +func RealTimeCommandExecutor(realTimeCommand []string) (ok bool, resultLog []string) { + if len(realTimeCommand) == 0 { return false, nil } - // 执行命令 - cmd := exec.Command(hardCodeCommand) - - // 执行命令并获取错误信息 - err := cmd.Run() + cmd := exec.Command(realTimeCommand[0], realTimeCommand[1:]...) + stdout, err := cmd.StdoutPipe() if err != nil { - return false, []string{"HardCodeCommandExecutor " + hardCodeCommand + " 执行命令失败", err.Error()} + return false, []string{err.Error()} } - // 合并输出结果 - stdoutBuf := bytes.Buffer{} - stderrBuf := bytes.Buffer{} - cmd.Stdout = &stdoutBuf - cmd.Stderr = &stderrBuf + stderr, err := cmd.StderrPipe() + if err != nil { + return false, []string{err.Error()} + } - output := mergeOutput(&stdoutBuf, &stderrBuf) + if err := cmd.Start(); err != nil { + return false, []string{err.Error()} + } + + output := make([]string, 0) + scanner := bufio.NewScanner(io.MultiReader(stdout, stderr)) + for scanner.Scan() { + line := scanner.Text() + fmt.Println(line) + output = append(output, line) + } + + if err := cmd.Wait(); err != nil { + return false, append(output, err.Error()) + } return true, output }