fix: run cmd mode

This commit is contained in:
2024-01-11 19:22:15 +08:00
parent 61b25fbe61
commit 3d77ade719
3 changed files with 74 additions and 11 deletions

View File

@@ -5,7 +5,9 @@ package cm
import (
"bytes"
"context"
"os/exec"
"time"
"ems.agt/lib/log"
)
@@ -64,3 +66,30 @@ func ExecOsCmd(command, os string) error {
}
return nil
}
func StartSSHCmdWithTimeout(duration int, sshHost, cmdStr string) error {
timeout := time.Duration(duration) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
defer cancel()
cmd := exec.CommandContext(ctx, "ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func RunSSHCmd(sshHost, cmdStr string) error {
cmd := exec.Command("ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}

View File

@@ -4,7 +4,10 @@
package cm
import (
"bytes"
"context"
"os/exec"
"time"
"ems.agt/lib/log"
)
@@ -51,3 +54,30 @@ func ExecOsCmd(command, os string) error {
}
return nil
}
func StartSSHCmdWithTimeout(duration int, sshHost, cmdStr string) error {
timeout := time.Duration(duration) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
defer cancel()
cmd := exec.CommandContext(ctx, "ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func RunSSHCmd(sshHost, cmdStr string) error {
cmd := exec.Command("ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}

View File

@@ -2,7 +2,6 @@ package cm
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
@@ -10,7 +9,6 @@ import (
"os/exec"
"strconv"
"strings"
"time"
"ems.agt/lib/dborm"
"ems.agt/lib/global"
@@ -701,19 +699,25 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
} else if fileType == 2 {
dpkgCmd := fmt.Sprintf("sudo dpkg -i --force-all '%s'", filePath)
timeout := time.Duration(config.GetYamlConfig().OMC.CmdTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
defer cancel()
cmd := exec.CommandContext(ctx, "ssh", sshHost, dpkgCmd)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Start()
err := RunSSHCmd(sshHost, dpkgCmd)
if err != nil {
log.Error("Faile to execute dpkg command: %v, err: %s", err, stderr.String())
log.Error("Faile to execute dpkg command:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
// timeout := time.Duration(config.GetYamlConfig().OMC.CmdTimeout) * time.Second
// ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
// defer cancel()
// cmd := exec.CommandContext(ctx, "ssh", sshHost, dpkgCmd)
// var stdout, stderr bytes.Buffer
// cmd.Stdout = &stdout
// cmd.Stderr = &stderr
// err := cmd.Start()
// if err != nil {
// log.Error("Faile to execute dpkg command: %v, err: %s", err, stderr.String())
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// out, err := cmd.CombinedOutput()
// log.Debugf("Exec output: %v", string(out))
// if err != nil {