Files
be.ems/src/modules/network_element/service/ne_config_backup.impl.go

156 lines
5.3 KiB
Go

package service
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"be.ems/src/framework/utils/date"
"be.ems/src/framework/utils/file"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
)
// NewNeConfigBackupImpl 网元配置文件备份记录 实例化服务层
var NewNeConfigBackupImpl = &NeConfigBackupImpl{
neConfigBackupRepository: repository.NewNeConfigBackupImpl,
}
// NeConfigBackupImpl 网元配置文件备份记录 服务层处理
type NeConfigBackupImpl struct {
// 网元配置文件备份记录
neConfigBackupRepository repository.INeConfigBackup
}
// SelectNeHostPage 分页查询列表数据
func (r *NeConfigBackupImpl) SelectPage(query map[string]any) map[string]any {
return r.neConfigBackupRepository.SelectPage(query)
}
// SelectConfigList 查询列表
func (r *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup {
return r.neConfigBackupRepository.SelectList(item)
}
// SelectByIds 通过ID查询
func (r *NeConfigBackupImpl) SelectById(id string) model.NeConfigBackup {
if id == "" {
return model.NeConfigBackup{}
}
arr := r.neConfigBackupRepository.SelectByIds([]string{id})
if len(arr) > 0 {
return arr[0]
}
return model.NeConfigBackup{}
}
// Insert 新增信息
func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string {
return r.neConfigBackupRepository.Insert(item)
}
// Update 修改信息
func (r *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 {
return r.neConfigBackupRepository.Update(item)
}
// DeleteByIds 批量删除信息
func (r *NeConfigBackupImpl) DeleteByIds(ids []string) (int64, error) {
// 检查是否存在
data := r.neConfigBackupRepository.SelectByIds(ids)
if len(data) <= 0 {
return 0, fmt.Errorf("neConfigBackup.noData")
}
if len(data) == len(ids) {
rows := r.neConfigBackupRepository.DeleteByIds(ids)
return rows, nil
}
// 删除信息失败!
return 0, fmt.Errorf("delete fail")
}
// NeConfigLocalToNe 网元配置文件复制到网元端覆盖
func (r *NeConfigBackupImpl) NeConfigLocalToNe(neInfo model.NeInfo, localFile string) error {
neTypeLower := strings.ToLower(neInfo.NeType)
// 网管本地路径
omcPath := "/usr/local/etc/omc/ne_config"
if runtime.GOOS == "windows" {
omcPath = fmt.Sprintf("C:%s", omcPath)
}
localDirPath := fmt.Sprintf("%s/%s/%s/backup/tmp_import", omcPath, neTypeLower, neInfo.NeId)
if err := file.UnZip(localFile, localDirPath); err != nil {
return fmt.Errorf("unzip err")
}
// 网元主机的SSH客户端
sshClient, err := NewNeInfoImpl.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return fmt.Errorf("ne info ssh client err")
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
return fmt.Errorf("ne info sftp client err")
}
defer sftpClient.Close()
// 网元配置端上的临时目录
neDirTemp := fmt.Sprintf("/tmp/omc/ne_config/%s/%s", neTypeLower, neInfo.NeId)
sshClient.RunCMD(fmt.Sprintf("sudo rm -rf %s", neDirTemp))
// 复制到网元端
if err = sftpClient.CopyDirLocalToRemote(localDirPath, neDirTemp); err != nil {
return fmt.Errorf("copy config to ne err")
}
neEtcPath := fmt.Sprintf("/usr/local/etc/%s", neTypeLower)
sshClient.RunCMD(fmt.Sprintf("sudo cp -rf %s/* %s && sudo chmod 775 %s/*.yaml", neDirTemp, neEtcPath, neEtcPath))
_ = os.RemoveAll(localDirPath) // 删除本地临时目录
sshClient.RunCMD(fmt.Sprintf("sudo rm -rf %s", neDirTemp)) // 删除临时目录
return nil
}
// NeConfigNeToLocal 网元备份文件网元端复制到本地
func (r *NeConfigBackupImpl) NeConfigNeToLocal(neInfo model.NeInfo) (string, error) {
// 网元主机的SSH客户端
sshClient, err := NewNeInfoImpl.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", fmt.Errorf("ne info ssh client err")
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
return "", fmt.Errorf("ne info sftp client err")
}
defer sftpClient.Close()
neTypeLower := strings.ToLower(neInfo.NeType)
// 网管本地路径
omcPath := "/usr/local/etc/omc/ne_config"
if runtime.GOOS == "windows" {
omcPath = fmt.Sprintf("C:%s", omcPath)
}
localDirPath := fmt.Sprintf("%s/%s/%s/backup/tmp_export", omcPath, neTypeLower, neInfo.NeId)
// 网元配置文件先复制到临时目录
nePath := fmt.Sprintf("/usr/local/etc/%s/*.yaml", neTypeLower)
neDirTemp := fmt.Sprintf("/tmp/omc/ne_config/%s/%s", neTypeLower, neInfo.NeId)
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo cp -rf %s %s", neDirTemp, nePath, neDirTemp))
// 复制到本地
if err = sftpClient.CopyDirRemoteToLocal(neDirTemp, localDirPath); err != nil {
return "", fmt.Errorf("copy config err")
}
// 压缩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)
if err := file.CompressZipByDir(zipFilePath, localDirPath); err != nil {
return "", fmt.Errorf("compress zip err")
}
_ = os.RemoveAll(localDirPath) // 删除本地临时目录
sshClient.RunCMD(fmt.Sprintf("sudo rm -rf %s", neDirTemp)) // 删除临时目录
return zipFilePath, nil
}