fix: 移除网元备份文件ftp改用统一ftp配置

This commit is contained in:
TsMask
2025-04-25 15:33:16 +08:00
parent 081ef5c9cc
commit 81e1112166
3 changed files with 2 additions and 150 deletions

View File

@@ -1,7 +1,6 @@
package controller
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
@@ -10,7 +9,6 @@ import (
"be.ems/src/framework/i18n"
"be.ems/src/framework/reqctx"
"be.ems/src/framework/resp"
"be.ems/src/framework/ssh"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/parse"
"be.ems/src/modules/network_element/model"
@@ -209,133 +207,3 @@ func (s NeConfigBackupController) Export(c *gin.Context) {
s.neConfigBackupService.Insert(item)
c.FileAttachment(item.Path, item.Name)
}
// 网元配置文件备份-设置FTP配置
//
// POST /ftp
func (s NeConfigBackupController) SetFTP(c *gin.Context) {
var body struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 获取配置
cfg := s.sysConfigService.FindByKey("ne.neConfigBackupFTP")
if cfg.ConfigId > 0 {
byteData, err := json.Marshal(body)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
cfg.ConfigValue = string(byteData)
cfg.UpdateBy = reqctx.LoginUserToUserName(c)
systemService.NewSysConfig.UpdateEncryptValue(cfg)
}
c.JSON(200, resp.Ok(nil))
}
// 网元配置文件备份-获取FTP配置
//
// GET /ftp
func (s NeConfigBackupController) GetFTP(c *gin.Context) {
// 获取配置
cfg := s.sysConfigService.FindByKeyDecryptValue("ne.neConfigBackupFTP")
if cfg.ConfigId > 0 {
var body struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := json.Unmarshal([]byte(cfg.ConfigValue), &body); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
c.JSON(200, resp.OkData(body))
return
}
c.JSON(200, resp.Ok(nil))
}
// 网元配置文件备份-文件FTP发送
//
// PUT /ftp
func (s NeConfigBackupController) PutFTP(c *gin.Context) {
var body struct {
FilePath string `json:"path" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 判断文件是否存在
if _, err := os.Stat(body.FilePath); os.IsNotExist(err) {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
// 获取配置
var cfgData struct {
Password string `json:"password" `
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := s.sysConfigService.FindByKeyDecryptValue("ne.neConfigBackupFTP")
if cfg.ConfigId > 0 {
if err := json.Unmarshal([]byte(cfg.ConfigValue), &cfgData); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
}
if !cfgData.Enable {
c.JSON(200, resp.ErrMsg("Setting Remote Backup is disabled"))
return
}
connSSH := ssh.ConnSSH{
User: cfgData.Username,
Password: cfgData.Password,
Addr: cfgData.ToIp,
Port: cfgData.ToPort,
AuthMode: "0",
}
sshClient, err := connSSH.NewClient()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, "/ne_config", filepath.Base(body.FilePath))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(body.FilePath, remotePath); err != nil {
c.JSON(200, resp.ErrMsg("error uploading file"))
return
}
c.JSON(200, resp.Ok(nil))
}

View File

@@ -351,20 +351,6 @@ func Setup(router *gin.Engine) {
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_EXPORT)),
controller.NewNeConfigBackup.Export,
)
neConfigBackupGroup.GET("/ftp",
middleware.PreAuthorize(nil),
controller.NewNeConfigBackup.GetFTP,
)
neConfigBackupGroup.POST("/ftp",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewNeConfigBackup.SetFTP,
)
neConfigBackupGroup.PUT("/ftp",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewNeConfigBackup.PutFTP,
)
}
}

View File

@@ -153,7 +153,7 @@ func (s NeConfigBackup) FileNeToLocal(neInfo model.NeInfo) (string, error) {
if runtime.GOOS == "windows" {
omcPath = fmt.Sprintf("C:%s", omcPath)
}
localDirPath := fmt.Sprintf("%s/%s/%s/backup/tmp_export", omcPath, neTypeLower, neInfo.NeId)
localDirPath := fmt.Sprintf("%s/%s/%s/from_ne_tmp", omcPath, neTypeLower, neInfo.NeId)
// 网元配置文件先复制到临时目录
sshClient.RunCMD("mkdir -p /tmp/omc && sudo chmod 777 -R /tmp/omc")
@@ -171,8 +171,6 @@ func (s NeConfigBackup) FileNeToLocal(neInfo model.NeInfo) (string, error) {
sshClient.RunCMD(fmt.Sprintf("mkdir -p %s/rtproxy && sudo cp -rf /usr/local/etc/rtproxy/rtproxy.conf %s/rtproxy", neDirTemp, neDirTemp))
// iwf目录
sshClient.RunCMD(fmt.Sprintf("mkdir -p %s/iwf && sudo cp -rf /usr/local/etc/iwf/*.yaml %s/iwf", neDirTemp, neDirTemp))
} else if neTypeLower == "omc" {
sshClient.RunCMD(fmt.Sprintf("mkdir -p %s && sudo cp -rf /usr/local/omc/etc/*.{yaml,conf} %s", neDirTemp, neDirTemp))
} else if neTypeLower == "smsc" {
sshClient.RunCMD(fmt.Sprintf("mkdir -p %s && sudo cp -rf /usr/local/etc/smsc/{*.yaml,*.conf,*conf.txt} %s", neDirTemp, neDirTemp))
sshClient.RunCMD(fmt.Sprintf("sudo cp -rf /usr/local/etc/smsc/conf %s/conf", neDirTemp))
@@ -191,7 +189,7 @@ func (s NeConfigBackup) FileNeToLocal(neInfo model.NeInfo) (string, error) {
// 压缩zip文件名
zipFileName := fmt.Sprintf("%s-%s-etc-%s.zip", neTypeLower, neInfo.NeId, date.ParseDateToStr(time.Now(), date.YYYYMMDDHHMMSS))
zipFilePath := fmt.Sprintf("%s/%s/%s/backup/%s", omcPath, neTypeLower, neInfo.NeId, zipFileName)
zipFilePath := fmt.Sprintf("%s/%s/%s/%s", omcPath, neTypeLower, neInfo.NeId, zipFileName)
if err := file.CompressZipByDir(zipFilePath, localDirPath); err != nil {
return "", fmt.Errorf("compress zip err")
}