diff --git a/features/cm/software.go b/features/cm/software.go index 3d91efb1..07954813 100644 --- a/features/cm/software.go +++ b/features/cm/software.go @@ -595,7 +595,6 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) { } services.ResponseStatusOK204NoContent(w) - return } func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) { @@ -669,12 +668,34 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) { if !config.GetYamlConfig().OMC.TestMode { filePath := (*neVersion)[0]["file_path"] sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip) - rpmCmd := fmt.Sprintf("sudo rpm -Uvh '%s'", filePath) - cmd := exec.Command("ssh", sshHost, rpmCmd) - out, err := cmd.CombinedOutput() - log.Tracef("Exec output: %v", string(out)) + fileType, err := global.JudgeRpmOrDebPackage(filePath) if err != nil { - log.Error("Faile to execute rpm command:", err) + log.Error("Failed to JudgeRpmOrDebPackage:", err) + services.ResponseInternalServerError500ProcessError(w, err) + } + if fileType == 1 { + rpmCmd := fmt.Sprintf("sudo rpm -Uvh '%s'", filePath) + cmd := exec.Command("ssh", sshHost, rpmCmd) + out, err := cmd.CombinedOutput() + log.Tracef("Exec output: %v", string(out)) + if err != nil { + log.Error("Faile to execute rpm command:", err) + services.ResponseInternalServerError500ProcessError(w, err) + return + } + } else if fileType == 2 { + dpkgCmd := fmt.Sprintf("sudo dpkg -i '%s'", filePath) + cmd := exec.Command("ssh", sshHost, dpkgCmd) + out, err := cmd.CombinedOutput() + log.Tracef("Exec output: %v", string(out)) + if err != nil { + log.Error("Faile to execute dpkg command:", err) + services.ResponseInternalServerError500ProcessError(w, err) + return + } + } else { + err := global.ErrCMUnknownSoftwareFormat + log.Error(err) services.ResponseInternalServerError500ProcessError(w, err) return } @@ -759,12 +780,34 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) { if !config.GetYamlConfig().OMC.TestMode { sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip) - rpmCmd := fmt.Sprintf("rpm -Uvh --oldpackage '%s'", filePath) - cmd := exec.Command("ssh", sshHost, rpmCmd) - out, err := cmd.CombinedOutput() - log.Tracef("Exec output: %v", string(out)) + fileType, err := global.JudgeRpmOrDebPackage(filePath) if err != nil { - log.Error("Faile to execute rpm command:", err) + log.Error("Failed to JudgeRpmOrDebPackage:", err) + services.ResponseInternalServerError500ProcessError(w, err) + } + if fileType == 1 { + rpmCmd := fmt.Sprintf("sudo rpm -Uvh --oldpackage '%s'", filePath) + cmd := exec.Command("ssh", sshHost, rpmCmd) + out, err := cmd.CombinedOutput() + log.Tracef("Exec output: %v", string(out)) + if err != nil { + log.Error("Faile to execute rpm command:", err) + services.ResponseInternalServerError500ProcessError(w, err) + return + } + } else if fileType == 2 { + dpkgCmd := fmt.Sprintf("sudo dpkg -i '%s'", filePath) + cmd := exec.Command("ssh", sshHost, dpkgCmd) + out, err := cmd.CombinedOutput() + log.Tracef("Exec output: %v", string(out)) + if err != nil { + log.Error("Faile to execute dpkg command:", err) + services.ResponseInternalServerError500ProcessError(w, err) + return + } + } else { + err := global.ErrCMUnknownSoftwareFormat + log.Error(err) services.ResponseInternalServerError500ProcessError(w, err) return } diff --git a/lib/global/global.go b/lib/global/global.go index 444ca57f..310f662b 100644 --- a/lib/global/global.go +++ b/lib/global/global.go @@ -52,6 +52,7 @@ var ( ErrCMNotFoundRollbackNeVersion = errors.New("not found the rollback NE version") ErrCMNotFoundTargetBackupFile = errors.New("not found the target NE backup") + ErrCMUnknownSoftwareFormat = errors.New("unknown software package format") // 未知软件包格式 // TRACE module error message ErrTraceFailedDistributeToNEs = errors.New("failed to distribute trace task to target NEs") diff --git a/lib/global/kits.go b/lib/global/kits.go index 63f83bbb..671b0fdc 100644 --- a/lib/global/kits.go +++ b/lib/global/kits.go @@ -585,3 +585,31 @@ func ZipDirectoryFile(srcDir, dstZip string) error { return err } + +// 判断软件包是rpm或者deb, 1:rpm, 2:deb, 0:unknown format +func JudgeRpmOrDebPackage(filePath string) (int, error) { + var fileType int = 0 + file, err := os.Open(filePath) + if err != nil { + return fileType, err + } + defer file.Close() + + // Read the first 6 bytes of the file + header := make([]byte, 6) + _, err = file.Read(header) + if err != nil { + return fileType, err + } + + // Check the magic numbers to determine the package format + if string(header) == "!" { + fileType = 1 + } else if string(header) == "!