merge: 合并OMC分支
This commit is contained in:
191
src/modules/network_element/service/ne_license.impl.go
Normal file
191
src/modules/network_element/service/ne_license.impl.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_cxy/src/framework/utils/file"
|
||||
"nms_cxy/src/modules/network_element/model"
|
||||
"nms_cxy/src/modules/network_element/repository"
|
||||
)
|
||||
|
||||
// 实例化服务层 NeLicenseImpl 结构体
|
||||
var NewNeLicenseImpl = &NeLicenseImpl{
|
||||
neLicenseRepository: repository.NewNeLicenseImpl,
|
||||
}
|
||||
|
||||
// NeLicenseImpl 网元授权激活信息 服务层处理
|
||||
type NeLicenseImpl struct {
|
||||
// 网元授权激活信息表
|
||||
neLicenseRepository repository.INeLicense
|
||||
}
|
||||
|
||||
// SelectNeHostPage 分页查询列表数据
|
||||
func (r *NeLicenseImpl) SelectPage(query map[string]any) map[string]any {
|
||||
return r.neLicenseRepository.SelectPage(query)
|
||||
}
|
||||
|
||||
// SelectConfigList 查询列表
|
||||
func (r *NeLicenseImpl) SelectList(neLicense model.NeLicense) []model.NeLicense {
|
||||
return r.neLicenseRepository.SelectList(neLicense)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeLicenseImpl) SelectById(id string) model.NeLicense {
|
||||
if id == "" {
|
||||
return model.NeLicense{}
|
||||
}
|
||||
neLicenses := r.neLicenseRepository.SelectByIds([]string{id})
|
||||
if len(neLicenses) > 0 {
|
||||
return neLicenses[0]
|
||||
}
|
||||
return model.NeLicense{}
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeLicenseImpl) Insert(neLicense model.NeLicense) string {
|
||||
return r.neLicenseRepository.Insert(neLicense)
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeLicenseImpl) Update(neLicense model.NeLicense) int64 {
|
||||
return r.neLicenseRepository.Update(neLicense)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeLicenseImpl) DeleteByIds(ids []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
rowIds := r.neLicenseRepository.SelectByIds(ids)
|
||||
if len(rowIds) <= 0 {
|
||||
return 0, fmt.Errorf("neLicense.noData")
|
||||
}
|
||||
|
||||
if len(rowIds) == len(ids) {
|
||||
rows := r.neLicenseRepository.DeleteByIds(ids)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
|
||||
// SelectByTypeAndID 通过网元类型和网元ID查询
|
||||
func (r *NeLicenseImpl) SelectByTypeAndID(neType, neId string) model.NeLicense {
|
||||
neLicenses := r.neLicenseRepository.SelectList(model.NeLicense{
|
||||
NeType: neType,
|
||||
NeId: neId,
|
||||
})
|
||||
if len(neLicenses) > 0 {
|
||||
return neLicenses[0]
|
||||
}
|
||||
return model.NeLicense{}
|
||||
}
|
||||
|
||||
// SelectByNeTypeAndNeID 通过ne_type和ne_id查询信息
|
||||
func (r *NeLicenseImpl) SelectByNeTypeAndNeID(neType, neId string) model.NeLicense {
|
||||
neLicenses := r.neLicenseRepository.SelectList(model.NeLicense{
|
||||
NeType: neType,
|
||||
NeId: neId,
|
||||
})
|
||||
if len(neLicenses) > 0 {
|
||||
return neLicenses[0]
|
||||
}
|
||||
return model.NeLicense{}
|
||||
}
|
||||
|
||||
// ReadLicenseInfo 读取授权文件信息
|
||||
// 返回激活申请码, 激活文件
|
||||
func (r *NeLicenseImpl) ReadLicenseInfo(neLicense model.NeLicense) (string, string) {
|
||||
neTypeLower := strings.ToLower(neLicense.NeType)
|
||||
// 网管本地路径
|
||||
omcPath := "/usr/local/etc/omc/ne_license"
|
||||
if runtime.GOOS == "windows" {
|
||||
omcPath = fmt.Sprintf("C:%s", omcPath)
|
||||
}
|
||||
omcPath = fmt.Sprintf("%s/%s/%s", omcPath, neTypeLower, neLicense.NeId)
|
||||
// 网元端授权文件路径
|
||||
nePath := fmt.Sprintf("/usr/local/etc/%s/license", neTypeLower)
|
||||
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := NewNeInfoImpl.NeRunSSHClient(neLicense.NeType, neLicense.NeId)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 复制授权申请码到本地
|
||||
if err = sftpClient.CopyFileRemoteToLocal(nePath+"/Activation_request_code.txt", omcPath+"/Activation_request_code.txt"); err != nil {
|
||||
return "", ""
|
||||
}
|
||||
// 读取文件内容
|
||||
bytes, err := os.ReadFile(omcPath + "/Activation_request_code.txt")
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// 复制激活文件到本地
|
||||
licensePath := ""
|
||||
if err = sftpClient.CopyFileRemoteToLocal(nePath+"/system.ini", omcPath+"/system.ini"); err == nil {
|
||||
licensePath = omcPath + "/system.ini"
|
||||
}
|
||||
return strings.TrimSpace(string(bytes)), licensePath
|
||||
}
|
||||
|
||||
// UploadLicense 授权文件上传到网元主机
|
||||
func (r *NeLicenseImpl) UploadLicense(neLicense model.NeLicense) error {
|
||||
// 检查文件是否存在
|
||||
omcLicensePath := file.ParseUploadFilePath(neLicense.LicensePath)
|
||||
if _, err := os.Stat(omcLicensePath); err != nil {
|
||||
return fmt.Errorf("file read failure")
|
||||
}
|
||||
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := NewNeInfoImpl.NeRunSSHClient(neLicense.NeType, neLicense.NeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 网元端授权文件路径
|
||||
neTypeLower := strings.ToLower(neLicense.NeType)
|
||||
neLicensePath := fmt.Sprintf("/usr/local/etc/%s/license/system.ini", neTypeLower)
|
||||
neLicenseDir := filepath.ToSlash(filepath.Dir(neLicensePath))
|
||||
// 修改网元文件权限
|
||||
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod 775 %s && sudo touch %s && sudo chmod o+rw %s", neLicenseDir, neLicenseDir, neLicensePath, neLicensePath))
|
||||
|
||||
// 尝试备份授权文件
|
||||
neLicensePathBack := fmt.Sprintf("%s/system_%s.ini", neLicensePath, time.Now().Format("20060102_150405"))
|
||||
sshClient.RunCMD(fmt.Sprintf("sudo cp -rf %s/system.ini %s", neLicensePath, neLicensePathBack))
|
||||
|
||||
// 上传授权文件去覆盖
|
||||
if err := sftpClient.CopyFileLocalToRemote(omcLicensePath, neLicensePath); err != nil {
|
||||
return fmt.Errorf("please check if scp remote copy is allowed")
|
||||
}
|
||||
|
||||
// 重启服务
|
||||
if neLicense.Reload {
|
||||
cmdStr := fmt.Sprintf("sudo service %s restart", neTypeLower)
|
||||
if neTypeLower == "ims" {
|
||||
cmdStr = "ims-stop || true && ims-start"
|
||||
} else if neTypeLower == "omc" {
|
||||
cmdStr = "sudo systemctl restart restagent"
|
||||
}
|
||||
sshClient.RunCMD(cmdStr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user