ref: 网元信息RunSSH客户端实例复用连接
This commit is contained in:
@@ -46,13 +46,15 @@ type INeInfo interface {
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
CheckUniqueNeTypeAndNeId(neType, neId, infoId string) bool
|
||||
|
||||
// NeRunCMD 向网元发送cmd命令
|
||||
NeRunCMD(neType, neId, cmd string) (string, error)
|
||||
|
||||
// NeRunSSHclient 网元主机的SSH客户端-为创建相关连接
|
||||
NeRunSSHclient(neType, neId string) (*ssh.ConnSSH, error)
|
||||
|
||||
// NeRunCMD 向网元发送cmd命令
|
||||
NeRunCMD(neType, neId, cmd string) (string, error)
|
||||
|
||||
// NeConfigFileRead 网元配置文件读取 网元配置yaml文件复制到本地后通过filePath读取
|
||||
//
|
||||
// 返回 string string[] map[string]any
|
||||
NeConfigFileRead(neInfo model.NeInfo, filePath, fileType string) any
|
||||
|
||||
// NeConfigFileWirte 网元配置文件写入 content内容 sync同步到网元端
|
||||
|
||||
@@ -307,23 +307,6 @@ func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neType, neId, infoId string) bool
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// NeRunCMD 向网元发送cmd命令
|
||||
func (r *NeInfoImpl) NeRunCMD(neType, neId, cmd string) (string, error) {
|
||||
sshClient, err := r.NeRunSSHclient(neType, neId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer sshClient.Close()
|
||||
|
||||
// 执行命令
|
||||
output, err := sshClient.RunCMD(cmd)
|
||||
if err != nil {
|
||||
logger.Errorf("NeRunCMD RunCMD %s err => %s", output, err.Error())
|
||||
return "", fmt.Errorf("neinfo ssh run cmd err")
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// NeRunSSHclient 网元主机的SSH客户端-为创建相关连接
|
||||
func (r *NeInfoImpl) NeRunSSHclient(neType, neId string) (*ssh.ConnSSH, error) {
|
||||
neInfo := r.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||
@@ -357,7 +340,26 @@ func (r *NeInfoImpl) NeRunSSHclient(neType, neId string) (*ssh.ConnSSH, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// NeRunCMD 向网元发送cmd命令
|
||||
func (r *NeInfoImpl) NeRunCMD(neType, neId, cmd string) (string, error) {
|
||||
sshClient, err := r.NeRunSSHclient(neType, neId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer sshClient.Close()
|
||||
|
||||
// 执行命令
|
||||
output, err := sshClient.RunCMD(cmd)
|
||||
if err != nil {
|
||||
logger.Errorf("NeRunCMD RunCMD %s err => %s", output, err.Error())
|
||||
return "", fmt.Errorf("neinfo ssh run cmd err")
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// NeConfigFileRead 网元配置文件读取 网元配置yaml文件复制到本地后通过filePath读取
|
||||
//
|
||||
// 返回 string string[] map[string]any
|
||||
func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath, fileType string) any {
|
||||
neTypeLower := strings.ToLower(neInfo.NeType)
|
||||
|
||||
@@ -401,27 +403,35 @@ func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath, fileType st
|
||||
nePath := "/usr/local/etc"
|
||||
nePath = fmt.Sprintf("%s/%s", nePath, neTypeLower)
|
||||
|
||||
// 网元主机的SSH客户端
|
||||
sshclient, err := r.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
defer sshclient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshclient.NewClientSFTP()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 各个网元与网管间约定配置文件
|
||||
err := ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/oam_manager.yaml", omcPath+"/oam_manager.yaml")
|
||||
if err == nil {
|
||||
if err = sftpClient.CopyFileRemoteToLocal(nePath+"/oam_manager.yaml", omcPath+"/oam_manager.yaml"); err == nil {
|
||||
files = append(files, "oam_manager.yaml")
|
||||
}
|
||||
|
||||
// 根据情况复制网元特殊配置
|
||||
switch neTypeLower {
|
||||
case "ausf":
|
||||
err = ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/ausfcfg.yaml", omcPath+"/ausfcfg.yaml")
|
||||
if err == nil {
|
||||
files = append(files, "ausfcfg.yaml")
|
||||
cfgFile := "ausfcfg.yaml"
|
||||
if err = sftpClient.CopyFileRemoteToLocal(fmt.Sprintf("%s/%s", nePath, cfgFile), fmt.Sprintf("%s/%s", omcPath, cfgFile)); err == nil {
|
||||
files = append(files, cfgFile)
|
||||
}
|
||||
case "smf":
|
||||
ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/smf_conf.yaml", omcPath+"/smf_conf.yaml")
|
||||
if err == nil {
|
||||
files = append(files, "smf_conf.yaml")
|
||||
}
|
||||
ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/smf_policy.yaml", omcPath+"/smf_policy.yaml")
|
||||
if err == nil {
|
||||
files = append(files, "smf_policy.yaml")
|
||||
cfgFile := "smf_conf.yaml"
|
||||
if err = sftpClient.CopyFileRemoteToLocal(fmt.Sprintf("%s/%s", nePath, cfgFile), fmt.Sprintf("%s/%s", omcPath, cfgFile)); err == nil {
|
||||
files = append(files, cfgFile)
|
||||
}
|
||||
case "ims":
|
||||
}
|
||||
@@ -448,20 +458,30 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s
|
||||
err = parse.ConvertConfigToFile(fileType, localFilePath, content)
|
||||
}
|
||||
if err != nil {
|
||||
logger.Warnf("NeConfigFile WriteFile => %s", err.Error())
|
||||
return fmt.Errorf("please check if the file exists or write permissions")
|
||||
}
|
||||
|
||||
// 同步到网元端
|
||||
if sync {
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := NewNeInfoImpl.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 网元端配置路径
|
||||
neFilePath := fmt.Sprintf("/usr/local/etc/%s/%s", neTypeLower, filePath)
|
||||
// 修改网元文件权限
|
||||
r.NeRunCMD(neInfo.NeType, neInfo.NeId, fmt.Sprintf("sudo chmod o+w %s", neFilePath))
|
||||
sshClient.RunCMD(fmt.Sprintf("sudo chmod o+w %s", neFilePath))
|
||||
// 复制到网元进行覆盖
|
||||
err = ssh.FileSCPLocalToNe(neInfo.IP, localFilePath, neFilePath)
|
||||
if err != nil {
|
||||
logger.Warnf("NeConfigFile SyncFile => %s", err.Error())
|
||||
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
|
||||
return fmt.Errorf("please check if scp remote copy is allowed")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user