feat: 网元软件包信息
This commit is contained in:
@@ -2,7 +2,12 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -66,24 +71,12 @@ func (r *NeSoftwareImpl) DeleteByIds(ids []string) (int64, error) {
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
|
||||
// SelectByVersionAndPath 通过文件版本和路径查询
|
||||
func (r *NeSoftwareImpl) SelectByVersionAndPath(version, path string) model.NeSoftware {
|
||||
neSoftwares := r.neSoftwareRepository.SelectList(model.NeSoftware{
|
||||
// CheckUniqueTypeAndNameAndVersion 校验网元类型和文件名版本是否唯一
|
||||
func (r *NeSoftwareImpl) CheckUniqueTypeAndNameAndVersion(neType, name, version, id string) bool {
|
||||
uniqueId := r.neSoftwareRepository.CheckUniqueTypeAndNameAndVersion(model.NeSoftware{
|
||||
NeType: neType,
|
||||
Name: name,
|
||||
Version: version,
|
||||
Path: path,
|
||||
})
|
||||
if len(neSoftwares) > 0 {
|
||||
return neSoftwares[0]
|
||||
}
|
||||
return model.NeSoftware{}
|
||||
}
|
||||
|
||||
// CheckUniqueTypeAndFileNameAndVersion 校验网元类型和文件名版本是否唯一
|
||||
func (r *NeSoftwareImpl) CheckUniqueTypeAndFileNameAndVersion(neType, fileName, version, id string) bool {
|
||||
uniqueId := r.neSoftwareRepository.CheckUniqueTypeAndFileNameAndVersion(model.NeSoftware{
|
||||
NeType: neType,
|
||||
FileName: fileName,
|
||||
Version: version,
|
||||
})
|
||||
if uniqueId == id {
|
||||
return true
|
||||
@@ -91,7 +84,54 @@ func (r *NeSoftwareImpl) CheckUniqueTypeAndFileNameAndVersion(neType, fileName,
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// Install 安装软件包
|
||||
func (r *NeSoftwareImpl) Install(neSoftware model.NeSoftware) (string, error) {
|
||||
return "", nil
|
||||
// UploadToNeHost 安装包上传到网元主机
|
||||
// 返回执行命令步骤
|
||||
func (r *NeSoftwareImpl) UploadToNeHost(neSoftware model.NeSoftware) ([]string, error) {
|
||||
cmdStrArr := []string{}
|
||||
// 检查文件是否存在
|
||||
filePath := file.ParseUploadFilePath(neSoftware.Path)
|
||||
if _, err := os.Stat(filePath); err != nil {
|
||||
return cmdStrArr, fmt.Errorf("file read failure")
|
||||
}
|
||||
fileName := filepath.Base(neSoftware.Path)
|
||||
if strings.Contains(fileName, "*") {
|
||||
fileName = strings.ReplaceAll(fileName, "*", "_")
|
||||
}
|
||||
nePath := "/tmp"
|
||||
neFilePath := fmt.Sprintf("%s/%s", nePath, fileName)
|
||||
|
||||
// 检查网元主机
|
||||
neHostInfo := NewNeHostImpl.SelectById(neSoftware.HostId)
|
||||
if neHostInfo.HostType != "ssh" || neHostInfo.HostID != neSoftware.HostId {
|
||||
return cmdStrArr, fmt.Errorf("no found host info")
|
||||
}
|
||||
|
||||
// 上传软件包到 /tmp
|
||||
if err := ssh.FileSCPLocalToNe(neHostInfo.Addr, filePath, neFilePath); err != nil {
|
||||
return cmdStrArr, fmt.Errorf("error uploading package")
|
||||
}
|
||||
|
||||
// 安装软件包
|
||||
cmdStrArr = append(cmdStrArr, fmt.Sprintf("sudo dpkg -i %s", neFilePath))
|
||||
|
||||
if neSoftware.NeType == "IMS" {
|
||||
// 公网 PLMN地址
|
||||
cmdStrArr = append(cmdStrArr, "sudo /usr/local/etc/ims/default/tools/modipplmn.sh {PUBIP} {MCC} {MNC}")
|
||||
// 内网 服务地址
|
||||
cmdStrArr = append(cmdStrArr, "sudo /usr/local/etc/ims/default/tools/modintraip.sh {PRIIP}")
|
||||
// 10s后停止服务
|
||||
cmdStrArr = append(cmdStrArr, "sudo ims-start")
|
||||
cmdStrArr = append(cmdStrArr, `nohup sh -c "sleep 10s && sudo ims-stop" > /dev/null 2>&1 &`)
|
||||
} else {
|
||||
// 10s后停止服务
|
||||
cmdStrArr = append(cmdStrArr, fmt.Sprintf("sudo service %s restart", strings.ToLower(neSoftware.NeType)))
|
||||
cmdStrArr = append(cmdStrArr, fmt.Sprintf(`nohup sh -c "sleep 10s && sudo service %s stop" > /dev/null 2>&1 &`, strings.ToLower(neSoftware.NeType)))
|
||||
}
|
||||
|
||||
// 删除软件包
|
||||
cmdStrArr = append(cmdStrArr, fmt.Sprintf("sudo rm %s", neFilePath))
|
||||
|
||||
// 结束
|
||||
cmdStrArr = append(cmdStrArr, fmt.Sprintf("echo '%s software install successful!'", neSoftware.NeType))
|
||||
return cmdStrArr, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user