feat: 网元配置文件复制到网元端覆盖函数功能实现

This commit is contained in:
TsMask
2024-07-24 10:26:45 +08:00
parent 68bbe3c750
commit 7b39972f2c
2 changed files with 49 additions and 6 deletions

View File

@@ -22,6 +22,9 @@ type INeConfigBackup interface {
// DeleteByIds 批量删除信息
DeleteByIds(ids []string) (int64, error)
// NeConfigFileToLocal 网元备份文件到本地
NeConfigFileToLocal(neInfo model.NeInfo) (string, error)
// NeConfigLocalToNe 网元配置文件复制到网元端覆盖
NeConfigLocalToNe(neInfo model.NeInfo, localFile string) error
// NeConfigNeToLocal 网元备份文件网元端复制到本地
NeConfigNeToLocal(neInfo model.NeInfo) (string, error)
}

View File

@@ -72,8 +72,48 @@ func (r *NeConfigBackupImpl) DeleteByIds(ids []string) (int64, error) {
return 0, fmt.Errorf("delete fail")
}
// NeConfigFileToLocal 网元备份文件到本地
func (r *NeConfigBackupImpl) NeConfigFileToLocal(neInfo model.NeInfo) (string, error) {
// 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 {
@@ -93,8 +133,8 @@ func (r *NeConfigBackupImpl) NeConfigFileToLocal(neInfo model.NeInfo) (string, e
if runtime.GOOS == "windows" {
omcPath = fmt.Sprintf("C:%s", omcPath)
}
localDirPath := fmt.Sprintf("%s/%s/%s/backup/tmp", omcPath, neTypeLower, neInfo.NeId)
// 网元配置文件先复制到临时目录目录
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))