From 81e11121665469fe79e9dccf0e18a93bfa46fc4e Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 25 Apr 2025 15:33:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E7=BD=91=E5=85=83?= =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6ftp=E6=94=B9=E7=94=A8?= =?UTF-8?q?=E7=BB=9F=E4=B8=80ftp=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ne_config_backup.go | 132 ------------------ .../network_element/network_element.go | 14 -- .../service/ne_config_backup.go | 6 +- 3 files changed, 2 insertions(+), 150 deletions(-) diff --git a/src/modules/network_element/controller/ne_config_backup.go b/src/modules/network_element/controller/ne_config_backup.go index fa42f7be..bec38ca2 100644 --- a/src/modules/network_element/controller/ne_config_backup.go +++ b/src/modules/network_element/controller/ne_config_backup.go @@ -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)) -} diff --git a/src/modules/network_element/network_element.go b/src/modules/network_element/network_element.go index b2b5249e..78a9787a 100644 --- a/src/modules/network_element/network_element.go +++ b/src/modules/network_element/network_element.go @@ -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, - ) } } diff --git a/src/modules/network_element/service/ne_config_backup.go b/src/modules/network_element/service/ne_config_backup.go index 388bd26b..4937862d 100644 --- a/src/modules/network_element/service/ne_config_backup.go +++ b/src/modules/network_element/service/ne_config_backup.go @@ -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") }