fix: run dpkg command with timeout

This commit is contained in:
2024-01-11 16:55:35 +08:00
parent 9b7f3742d0
commit 5aeca6ecb4

View File

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