feat: 新增网元端公共配置文件读写接口
This commit is contained in:
@@ -59,4 +59,12 @@ type INeInfo interface {
|
||||
|
||||
// NeConfigFileWirte 网元配置文件写入 content内容 sync同步到网元端
|
||||
NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType string, content any, sync bool) error
|
||||
|
||||
// NeConfPara5GRead 网元公共配置文件读取
|
||||
//
|
||||
// 返回 string map[string]any
|
||||
NeConfPara5GRead(fileType string) any
|
||||
|
||||
// NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId
|
||||
NeConfPara5GWirte(fileType string, content any, syncNE []string) error
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
@@ -470,7 +471,7 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s
|
||||
// 同步到网元端
|
||||
if sync {
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := NewNeInfoImpl.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
sshClient, err := r.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -494,3 +495,89 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NeConfPara5GRead 网元公共配置文件读取
|
||||
//
|
||||
// 返回 string map[string]any
|
||||
func (r *NeInfoImpl) NeConfPara5GRead(fileType string) any {
|
||||
// 网管本地路径
|
||||
omcFilePath := "/usr/local/omc/etc/para5G.yaml"
|
||||
if runtime.GOOS == "windows" {
|
||||
omcFilePath = fmt.Sprintf("C:%s", omcFilePath)
|
||||
}
|
||||
// 读取文件内容
|
||||
bytes, err := os.ReadFile(omcFilePath)
|
||||
if err != nil {
|
||||
logger.Warnf("NeConfPara5GRead ReadFile => %s", err.Error())
|
||||
return "read file error"
|
||||
}
|
||||
content := string(bytes)
|
||||
if fileType == "" || fileType == "txt" {
|
||||
return content
|
||||
}
|
||||
// 序列化Map
|
||||
mapData, err := parse.ConvertConfigToMap(fileType, content)
|
||||
if err != nil {
|
||||
logger.Warnf("NeConfPara5GRead ConvertConfigToMap => %s", err.Error())
|
||||
return "content convert type error"
|
||||
}
|
||||
return mapData
|
||||
}
|
||||
|
||||
// NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId
|
||||
func (r *NeInfoImpl) NeConfPara5GWirte(fileType string, content any, syncNE []string) error {
|
||||
// 网管本地路径
|
||||
omcFilePath := "/usr/local/omc/etc/para5G.yaml"
|
||||
if runtime.GOOS == "windows" {
|
||||
omcFilePath = fmt.Sprintf("C:%s", omcFilePath)
|
||||
}
|
||||
|
||||
var err error
|
||||
if fileType == "" || fileType == "txt" {
|
||||
err = parse.ConvertConfigToFile(fileType, omcFilePath, content)
|
||||
}
|
||||
if fileType == "json" || fileType == "yaml" || fileType == "yml" {
|
||||
err = parse.ConvertConfigToFile(fileType, omcFilePath, content)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("please check if the file exists or write permissions")
|
||||
}
|
||||
|
||||
// 同步到网元端
|
||||
if len(syncNE) > 0 {
|
||||
errMsg := []string{}
|
||||
for _, neTI := range syncNE {
|
||||
ti := strings.SplitN(neTI, "@", 2)
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := r.NeRunSSHclient(ti[0], ti[1])
|
||||
if err != nil {
|
||||
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
|
||||
continue
|
||||
}
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
|
||||
continue
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 网元端配置路径
|
||||
neFilePath := "/usr/local/etc/conf/para5G.yaml"
|
||||
neFileDir := filepath.Dir(neFilePath)
|
||||
// 修改网元文件权限
|
||||
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod o+w %s && sudo chmod o+w %s", neFileDir, neFileDir, neFilePath))
|
||||
// 复制到网元进行覆盖
|
||||
if err = sftpClient.CopyFileLocalToRemote(omcFilePath, neFilePath); err != nil {
|
||||
errMsg = append(errMsg, fmt.Sprintf("%s : please check if scp remote copy is allowed", ti))
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(errMsg) > 0 {
|
||||
return fmt.Errorf(strings.Join(errMsg, "\r\n"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user