feat: 网元行为接口新增获取目录压缩为ZIP/文件内容查看cat

This commit is contained in:
TsMask
2024-10-15 11:47:25 +08:00
parent 065895e1d2
commit 0a24e50e9d
2 changed files with 129 additions and 13 deletions

View File

@@ -31,7 +31,7 @@ type NeActionController struct {
neInfoService *neService.NeInfo
}
// 发送文件从本地到网元
// 从本地到网元发送文件
//
// POST /pushFile
func (s *NeActionController) PushFile(c *gin.Context) {
@@ -88,7 +88,7 @@ func (s *NeActionController) PushFile(c *gin.Context) {
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
}
// 获取文件从网元到本地
// 从网元到本地获取文件
//
// GET /pullFile
func (s *NeActionController) PullFile(c *gin.Context) {
@@ -147,6 +147,116 @@ func (s *NeActionController) PullFile(c *gin.Context) {
c.FileAttachment(localFilePath, fileName)
}
// 从网元到本地获取目录压缩为ZIP
//
// GET /pullDirZip
func (s *NeActionController) PullDirZip(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
Path string `form:"path" binding:"required"`
DelTemp bool `form:"delTemp"` // 删除本地临时文件
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
nePath := querys.Path
dirName := generate.Code(6)
localFilePath := filepath.Join("/tmp/omc/pull/", dirName)
if runtime.GOOS == "windows" {
localFilePath = fmt.Sprintf("C:%s", localFilePath)
}
// 复制到本地
localDirFilePath := filepath.Join(localFilePath, "zip")
if err = sftpClient.CopyDirRemoteToLocal(nePath, localDirFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 压缩zip文件名
zipFileName := fmt.Sprintf("%s-%s-%s.zip", neInfo.NeType, neInfo.NeId, dirName)
zipFilePath := filepath.Join(localFilePath, zipFileName)
if err := file.CompressZipByDir(zipFilePath, localDirFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer func() {
if querys.DelTemp {
_ = os.RemoveAll(localFilePath)
}
}()
c.FileAttachment(zipFilePath, zipFileName)
}
// 查看网元端文件内容
//
// GET /viewFile
func (s *NeActionController) ViewFile(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
Path string `form:"path" binding:"required"`
FileName string `form:"fileName" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元端文件
nePath := filepath.ToSlash(filepath.Join(querys.Path, querys.FileName))
// 网元端临时目录
output, err := sshClient.RunCMD(fmt.Sprintf("cat %s", nePath))
output = strings.TrimSpace(output)
if err != nil || strings.HasPrefix(output, "ls: ") {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "file view cat error")))
return
}
c.JSON(200, result.OkData(output))
}
// 网元端文件列表
//
// GET /files

View File

@@ -17,10 +17,8 @@ func Setup(router *gin.Engine) {
// 启动时需要的初始参数
InitLoad()
neGroup := router.Group("/ne")
// 网元操作处理
neActionGroup := neGroup.Group("/action")
neActionGroup := router.Group("/ne/action")
{
neActionGroup.GET("/files",
middleware.PreAuthorize(nil),
@@ -35,6 +33,14 @@ func Setup(router *gin.Engine) {
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neAction", collectlogs.BUSINESS_TYPE_IMPORT)),
controller.NewNeAction.PushFile,
)
neActionGroup.GET("/pullDirZip",
middleware.PreAuthorize(nil),
controller.NewNeAction.PullDirZip,
)
neActionGroup.GET("/viewFile",
middleware.PreAuthorize(nil),
controller.NewNeAction.ViewFile,
)
neActionGroup.PUT("/service",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neAction", collectlogs.BUSINESS_TYPE_OTHER)),
@@ -43,7 +49,7 @@ func Setup(router *gin.Engine) {
}
// 网元信息
neInfoGroup := neGroup.Group("/info")
neInfoGroup := router.Group("/ne/info")
{
neInfoGroup.GET("/state",
middleware.PreAuthorize(nil),
@@ -104,7 +110,7 @@ func Setup(router *gin.Engine) {
}
// 网元主机
neHostGroup := neGroup.Group("/host")
neHostGroup := router.Group("/ne/host")
{
neHostGroup.GET("/list",
middleware.PreAuthorize(nil),
@@ -153,7 +159,7 @@ func Setup(router *gin.Engine) {
}
// 网元主机命令
neHostCmdGroup := neGroup.Group("/hostCmd")
neHostCmdGroup := router.Group("/ne/hostCmd")
{
neHostCmdGroup.GET("/list",
middleware.PreAuthorize(nil),
@@ -181,7 +187,7 @@ func Setup(router *gin.Engine) {
}
// 网元版本信息
neVersionGroup := neGroup.Group("/version")
neVersionGroup := router.Group("/ne/version")
{
neVersionGroup.GET("/list",
middleware.PreAuthorize(nil),
@@ -199,7 +205,7 @@ func Setup(router *gin.Engine) {
}
// 网元软件包信息
neSoftwareGroup := neGroup.Group("/software")
neSoftwareGroup := router.Group("/ne/software")
{
neSoftwareGroup.GET("/list",
middleware.PreAuthorize(nil),
@@ -232,7 +238,7 @@ func Setup(router *gin.Engine) {
}
// 网元授权激活信息
neLicenseGroup := neGroup.Group("/license")
neLicenseGroup := router.Group("/ne/license")
{
neLicenseGroup.GET("/list",
middleware.PreAuthorize(nil),
@@ -262,7 +268,7 @@ func Setup(router *gin.Engine) {
}
// 网元参数配置
neConfigGroup := neGroup.Group("/config")
neConfigGroup := router.Group("/ne/config")
{
// 网元参数配置可用属性值
neConfigGroup.GET("/list",
@@ -315,7 +321,7 @@ func Setup(router *gin.Engine) {
}
// 网元配置文件备份记录
neConfigBackupGroup := neGroup.Group("/config/backup")
neConfigBackupGroup := router.Group("/ne/config/backup")
{
neConfigBackupGroup.GET("/list",
middleware.PreAuthorize(nil),