diff --git a/features/cm/exec_linux.go b/features/cm/exec_linux.go index 3044649a..088fd861 100644 --- a/features/cm/exec_linux.go +++ b/features/cm/exec_linux.go @@ -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 +} diff --git a/features/cm/exec_windows.go b/features/cm/exec_windows.go index fa15045a..0c1d038f 100644 --- a/features/cm/exec_windows.go +++ b/features/cm/exec_windows.go @@ -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 +} diff --git a/features/cm/software.go b/features/cm/software.go index 8b9fe138..269e0d5b 100644 --- a/features/cm/software.go +++ b/features/cm/software.go @@ -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 {