feat: 网元授权激活信息
This commit is contained in:
37
src/modules/network_element/service/ne_license.go
Normal file
37
src/modules/network_element/service/ne_license.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package service
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
|
||||
// INeLicense 网元授权激活信息 服务层接口
|
||||
type INeLicense interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(neLicense model.NeLicense) []model.NeLicense
|
||||
|
||||
// SelectById 通过ID查询
|
||||
SelectById(id string) model.NeLicense
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neLicense model.NeLicense) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neLicense model.NeLicense) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ids []string) (int64, error)
|
||||
|
||||
// CheckUniqueTypeAndID 校验网元类型和网元ID是否唯一
|
||||
CheckUniqueTypeAndID(neType, neId, id string) bool
|
||||
|
||||
// SelectByNeTypeAndNeID 通过ne_type和ne_id查询信息
|
||||
SelectByNeTypeAndNeID(neType, neId string) model.NeLicense
|
||||
|
||||
// ReadLicenseInfo 读取授权文件信息
|
||||
// 激活申请码, 激活文件
|
||||
ReadLicenseInfo(neInfo model.NeInfo) (string, string)
|
||||
|
||||
// UploadToNeHost 授权文件上传到网元主机
|
||||
UploadToNeHost(neLicense model.NeLicense) error
|
||||
}
|
||||
183
src/modules/network_element/service/ne_license.impl.go
Normal file
183
src/modules/network_element/service/ne_license.impl.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/ssh"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
"be.ems/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{}
|
||||
}
|
||||
|
||||
// CheckUniqueTypeAndID 校验网元类型和网元ID是否唯一
|
||||
func (r *NeLicenseImpl) CheckUniqueTypeAndID(neType, neId, id string) bool {
|
||||
uniqueId := r.neLicenseRepository.CheckUniqueTypeAndID(model.NeLicense{
|
||||
NeType: neType,
|
||||
NeId: neId,
|
||||
})
|
||||
if uniqueId == id {
|
||||
return true
|
||||
}
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// 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(neInfo model.NeInfo) (string, string) {
|
||||
neTypeLower := strings.ToLower(neInfo.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, neInfo.NeId)
|
||||
// 网元端授权文件路径
|
||||
nePath := fmt.Sprintf("/usr/local/etc/%s/license", neTypeLower)
|
||||
|
||||
// 复制授权申请码到本地
|
||||
err := ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/Activation_request_code.txt", omcPath+"/Activation_request_code.txt")
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
// 读取文件内容
|
||||
bytes, err := os.ReadFile(omcPath + "/Activation_request_code.txt")
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// 激活文件
|
||||
licensePath := ""
|
||||
if err = ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/system.ini", omcPath+"/system.ini"); err == nil {
|
||||
licensePath = omcPath + "/system.ini"
|
||||
}
|
||||
return strings.TrimSpace(string(bytes)), licensePath
|
||||
}
|
||||
|
||||
// UploadToNeHost 授权文件上传到网元主机
|
||||
func (r *NeLicenseImpl) UploadToNeHost(neLicense model.NeLicense) error {
|
||||
|
||||
// 检查文件是否存在
|
||||
omcLicensePath := file.ParseUploadFilePath(neLicense.LicensePath)
|
||||
if _, err := os.Stat(omcLicensePath); err != nil {
|
||||
return fmt.Errorf("file read failure")
|
||||
}
|
||||
|
||||
// 检查网元主机
|
||||
neHostInfo := NewNeHostImpl.SelectById(neLicense.HostId)
|
||||
if neHostInfo.HostType != "ssh" || neHostInfo.HostID != neLicense.HostId {
|
||||
return fmt.Errorf("no found host info")
|
||||
}
|
||||
|
||||
// 网元端授权文件路径
|
||||
neTypeLower := strings.ToLower(neLicense.NeType)
|
||||
neLicensePath := fmt.Sprintf("/usr/local/etc/%s/license", neTypeLower)
|
||||
// 修改文件夹权限
|
||||
NewNeInfoImpl.NeRunCMD(neLicense.NeType, neLicense.NeId, fmt.Sprintf("sudo chmod o+w %s/", neLicensePath))
|
||||
// 尝试备份授权文件
|
||||
neLicensePathBack := fmt.Sprintf("%s/system_%s.ini", neLicensePath, time.Now().Format("20060102_150405"))
|
||||
NewNeInfoImpl.NeRunCMD(neLicense.NeType, neLicense.NeId, fmt.Sprintf("sudo cp -rf %s/system.ini %s", neLicensePath, neLicensePathBack))
|
||||
// 上传授权文件去覆盖
|
||||
NewNeInfoImpl.NeRunCMD(neLicense.NeType, neLicense.NeId, fmt.Sprintf("sudo chmod o+w %s/system.ini", neLicensePath))
|
||||
if err := ssh.FileSCPLocalToNe(neHostInfo.Addr, omcLicensePath, neLicensePath+"/system.ini"); err != nil {
|
||||
return fmt.Errorf("error uploading license")
|
||||
}
|
||||
|
||||
// 重启服务
|
||||
if neLicense.Reload {
|
||||
cmdStr := fmt.Sprintf("sudo service %s restart", neTypeLower)
|
||||
if neTypeLower == "ims" {
|
||||
cmdStr = "sudo ims-stop && sudo ims-start"
|
||||
} else if neTypeLower == "omc" {
|
||||
cmdStr = "sudo /usr/local/omc/bin/omcsvc.sh restart"
|
||||
}
|
||||
NewNeInfoImpl.NeRunCMD(neLicense.NeType, neLicense.NeId, cmdStr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user