feat: 添加网元配置文件备份的FTP配置管理功能

This commit is contained in:
TsMask
2025-04-11 11:04:39 +08:00
parent 8910008e92
commit ad81d04db1
8 changed files with 258 additions and 54 deletions

View File

@@ -1,6 +1,7 @@
package controller
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
@@ -9,10 +10,12 @@ 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"
neService "be.ems/src/modules/network_element/service"
systemService "be.ems/src/modules/system/service"
"github.com/gin-gonic/gin"
)
@@ -21,6 +24,7 @@ import (
var NewNeConfigBackup = &NeConfigBackupController{
neConfigBackupService: neService.NewNeConfigBackup,
neInfoService: neService.NewNeInfo,
sysConfigService: systemService.NewSysConfig,
}
// 网元配置文件备份记录
@@ -29,6 +33,7 @@ var NewNeConfigBackup = &NeConfigBackupController{
type NeConfigBackupController struct {
neConfigBackupService *neService.NeConfigBackup // 网元配置文件备份记录服务
neInfoService *neService.NeInfo // 网元信息服务
sysConfigService *systemService.SysConfig // 参数配置服务
}
// 网元配置文件备份记录列表
@@ -204,3 +209,133 @@ 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,6 +351,20 @@ 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,
)
}
}