ref: 网元授权激活授权code/change/state接口优化

This commit is contained in:
TsMask
2024-04-11 17:11:24 +08:00
parent 5970a8b5f2
commit ebd02cb3e9
4 changed files with 94 additions and 79 deletions

View File

@@ -37,6 +37,14 @@ func (s *NeLicenseController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.neLicenseService.SelectPage(querys)
// 过滤屏蔽授权文件
rows := data["rows"].([]model.NeLicense)
arr := &rows
for i := range *arr {
(*arr)[i].ActivationRequestCode = "-"
(*arr)[i].LicensePath = "-"
}
c.JSON(200, result.Ok(data))
}
@@ -73,24 +81,17 @@ func (s *NeLicenseController) Add(c *gin.Context) {
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 检查属性值唯一
uniqueInfo := s.neLicenseService.CheckUniqueTypeAndID(neInfo.NeType, neInfo.NeId, "")
uniqueInfo := s.neLicenseService.CheckUniqueTypeAndID(body.NeType, body.NeId, "")
if !uniqueInfo {
// 网元授权激活操作【%s】失败网元类型信息已存在
msg := i18n.TTemplate(language, "neLicense.errKeyExists", map[string]any{"name": neInfo.NeType})
msg := i18n.TTemplate(language, "neLicense.errKeyExists", map[string]any{"name": body.NeType})
c.JSON(200, result.ErrMsg(msg))
return
}
// 读取授权码
code, _ := s.neLicenseService.ReadLicenseInfo(neInfo)
code, _ := s.neLicenseService.ReadLicenseInfo(body)
body.ActivationRequestCode = code
body.CreateBy = ctx.LoginUserToUserName(c)
@@ -180,15 +181,8 @@ func (s *NeLicenseController) Code(c *gin.Context) {
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeId)
if neInfo.NeId != querys.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 检查是否存在授权记录
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(neInfo.NeType, neInfo.NeId)
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(querys.NeType, querys.NeId)
if neLicense.NeId != querys.NeId {
// 没有可访问网元授权激活数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
@@ -196,7 +190,7 @@ func (s *NeLicenseController) Code(c *gin.Context) {
}
// 更新授权码
code, licensePath := s.neLicenseService.ReadLicenseInfo(neInfo)
code, licensePath := s.neLicenseService.ReadLicenseInfo(neLicense)
neLicense.ActivationRequestCode = code
if licensePath != "" {
neLicense.LicensePath = licensePath
@@ -218,7 +212,7 @@ func (s *NeLicenseController) Change(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.NeLicense
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.HostId == "" {
if err != nil || body.LicensePath == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -226,23 +220,30 @@ func (s *NeLicenseController) Change(c *gin.Context) {
// 检查是否存在授权记录
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(body.NeType, body.NeId)
if neLicense.NeId != body.NeId {
body.Status = "0"
body.CreateBy = ctx.LoginUserToUserName(c)
body.ID = s.neLicenseService.Insert(body)
} else {
neLicense.LicensePath = body.LicensePath
neLicense.Status = "0"
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
s.neLicenseService.Update(neLicense)
}
// 进行上传替换
err = s.neLicenseService.UploadToNeHost(body)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
// 没有可访问网元授权激活数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
return
}
c.JSON(200, result.Ok(nil))
// 更新授权记录
if body.Remark != "" {
neLicense.Remark = body.Remark
}
neLicense.LicensePath = body.LicensePath
neLicense.Status = "0"
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
upRows := s.neLicenseService.Update(neLicense)
if upRows > 0 {
// 进行上传替换
err = s.neLicenseService.UploadLicense(body)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 网元授权激活状态
@@ -259,35 +260,31 @@ func (s *NeLicenseController) State(c *gin.Context) {
return
}
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeId)
if neInfo.NeId != querys.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 检查是否存在授权记录
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(neInfo.NeType, neInfo.NeId)
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(querys.NeType, querys.NeId)
if neLicense.NeId != querys.NeId {
// 没有可访问网元授权激活数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
return
}
// 查询网元状态
neState, err := neService.NeState(neInfo)
if err != nil {
c.JSON(200, result.ErrMsg("network element service anomaly"))
// 查询网元获取IP获取网元状态
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neLicense.NeType, neLicense.NeId)
if neInfo.NeId != neLicense.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
neLicense.Status = "0"
if neState, err := neService.NeState(neInfo); err == nil {
neLicense.SerialNum = fmt.Sprint(neState["sn"])
neLicense.ExpiryDate = fmt.Sprint(neState["expire"])
neLicense.Status = "1"
}
// 更新授权信息
neLicense.SerialNum = fmt.Sprint(neState["sn"])
neLicense.ExpiryDate = fmt.Sprint(neState["expire"])
code, licensePath := s.neLicenseService.ReadLicenseInfo(neInfo)
code, licensePath := s.neLicenseService.ReadLicenseInfo(neLicense)
neLicense.ActivationRequestCode = code
neLicense.LicensePath = licensePath
neLicense.Status = "1"
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
rows := s.neLicenseService.Update(neLicense)
if rows > 0 {

View File

@@ -9,7 +9,7 @@ type NeLicense struct {
LicensePath string `json:"licensePath" gorm:"license_path"` // 激活授权文件
SerialNum string `json:"serialNum" gorm:"serial_num"` // 序列号
ExpiryDate string `json:"expiryDate" gorm:"expiry_date"` // 许可证到期日期
Status string `json:"status" gorm:"status"` // 状态 ''ACTIVE'',''INACTIVE'',''PENDING''
Status string `json:"status" gorm:"status"` // 状态 0无效 1有效 ''ACTIVE'',''INACTIVE'',''PENDING''
Remark string `json:"remark" gorm:"remark"` // 备注
CreateBy string `json:"createBy" gorm:"create_by"` // 创建者
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
@@ -18,8 +18,7 @@ type NeLicense struct {
// ====== 非数据库字段属性 ======
Reload bool `json:"reload,omitempty" gorm:"-"` // 刷新重启网元
HostId string `json:"hostId,omitempty" gorm:"-"` // 已记录的主机ID
Reload bool `json:"reload,omitempty" gorm:"-"` // 刷新重启网元
}
// TableName 表名称

View File

@@ -29,9 +29,9 @@ type INeLicense interface {
SelectByNeTypeAndNeID(neType, neId string) model.NeLicense
// ReadLicenseInfo 读取授权文件信息
// 激活申请码, 激活文件
ReadLicenseInfo(neInfo model.NeInfo) (string, string)
// 返回激活申请码, 激活文件
ReadLicenseInfo(neLicense model.NeLicense) (string, string)
// UploadToNeHost 授权文件上传到网元主机
UploadToNeHost(neLicense model.NeLicense) error
// UploadLicense 授权文件上传到网元主机
UploadLicense(neLicense model.NeLicense) error
}

View File

@@ -8,7 +8,6 @@ import (
"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"
)
@@ -109,64 +108,84 @@ func (r *NeLicenseImpl) SelectByNeTypeAndNeID(neType, neId string) model.NeLicen
}
// ReadLicenseInfo 读取授权文件信息
// 激活申请码, 激活文件
func (r *NeLicenseImpl) ReadLicenseInfo(neInfo model.NeInfo) (string, string) {
neTypeLower := strings.ToLower(neInfo.NeType)
// 返回激活申请码, 激活文件
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, neInfo.NeId)
omcPath = fmt.Sprintf("%s/%s/%s", omcPath, neTypeLower, neLicense.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")
// 网元主机的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 = ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/system.ini", omcPath+"/system.ini"); err == nil {
if err = sftpClient.CopyFileRemoteToLocal(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 {
// 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")
}
// 检查网元主机
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))
NewNeInfoImpl.NeRunCMD(neLicense.NeType, neLicense.NeId, fmt.Sprintf("sudo chmod o+w %s/system.ini", 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))
// 网元主机的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()
// 上传授权文件去覆盖
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")
err = sftpClient.CopyFileLocalToRemote(omcLicensePath, neLicensePath+"/system.ini")
if err != nil {
return err
}
// 重启服务