This commit is contained in:
2023-09-16 19:23:11 +08:00
parent 9c8b4d8dc8
commit de9189bd32
6 changed files with 119 additions and 84 deletions

View File

@@ -457,7 +457,6 @@ func ExportCmFromNF(w http.ResponseWriter, r *http.Request) {
//services.ResponseFileWithNameAndMD5(w, http.StatusOK, zipFile, path, md5Sum)
services.ResponseStatusOK204NoContent(w)
return
}
type ImportCMJson struct {
@@ -481,7 +480,7 @@ func ImportCmToNF(w http.ResponseWriter, r *http.Request) {
return
}
neTypeUpper := strings.ToUpper(neType)
neTypeLower := strings.ToLower(neType)
//neTypeLower := strings.ToLower(neType)
neId := services.GetUriParamString(r, "ne_id", ",", false, false)
@@ -534,41 +533,75 @@ func ImportCmToNF(w http.ResponseWriter, r *http.Request) {
log.Error("Faile to XormInsertTableOne:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
} else if has == false {
} else if !has {
err = global.ErrCMInvalidBackupFile
log.Error(err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
isExist, err := global.PathExists(nePath)
if err != nil {
log.Errorf("Failed to stat:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
if isExist {
err = os.RemoveAll(nePath)
if err != nil {
log.Errorf("Failed to remove:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
}
unzipCmd := fmt.Sprintf("unzip -o %s -d %s", filePath, config.GetYamlConfig().OMC.Upload)
// nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
// isExist, err := global.PathExists(nePath)
// if err != nil {
// log.Errorf("Failed to stat:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// if isExist {
// err = os.RemoveAll(nePath)
// if err != nil {
// log.Errorf("Failed to remove:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// }
// unzipCmd := fmt.Sprintf("unzip -o %s -d %s", filePath, config.GetYamlConfig().OMC.Upload)
var scpCmd string
// var scpCmd string
// ipType := global.ParseIPAddr(neInfo.Ip)
// if ipType == global.IsIPv4 {
// scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@%s:%s", config.GetYamlConfig().OMC.Upload,
// neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
// } else {
// scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@[%s]:%s", config.GetYamlConfig().OMC.Upload,
// neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
// }
// err = ExecCmd(fmt.Sprintf("%s && %s", unzipCmd, scpCmd))
// if err != nil {
// log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
// isExist, err := global.PathExists(nePath)
// if err != nil {
// log.Errorf("Failed to stat:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// if isExist {
// err = os.RemoveAll(nePath)
// if err != nil {
// log.Errorf("Failed to remove:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// }
var scpZipCmd string
ipType := global.ParseIPAddr(neInfo.Ip)
if ipType == global.IsIPv4 {
scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@%s:%s", config.GetYamlConfig().OMC.Upload,
neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
scpZipCmd = fmt.Sprintf("scp -r %s -d %s@%s:%s", filePath,
config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
} else {
scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@[%s]:%s", config.GetYamlConfig().OMC.Upload,
neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
scpZipCmd = fmt.Sprintf("scp -r %s -d %s@[%s]:%s", filePath,
config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
}
neFilePath := config.GetYamlConfig().NE.ScpDir + "/" + fileName
unzipCmd := fmt.Sprintf("sudo unzip -o %s -d %s", neFilePath, config.GetYamlConfig().NE.EtcDir)
err = ExecCmd(fmt.Sprintf("%s && %s", unzipCmd, scpCmd))
err = ExecCmd(fmt.Sprintf("%s && %s", scpZipCmd, unzipCmd))
if err != nil {
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
services.ResponseInternalServerError500ProcessError(w, err)
@@ -621,7 +654,6 @@ func DownloadNeBackupFile(w http.ResponseWriter, r *http.Request) {
md5Sum := (*neBackup)[0]["md5_sum"]
services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, path, md5Sum)
return
}
func DeleteNeBackupFile(w http.ResponseWriter, r *http.Request) {
@@ -681,5 +713,4 @@ func DeleteNeBackupFile(w http.ResponseWriter, r *http.Request) {
}
services.ResponseStatusOK204NoContent(w)
return
}