Merge remote-tracking branch 'origin/main' into practical-training

This commit is contained in:
TsMask
2024-07-16 10:02:42 +08:00
19 changed files with 87 additions and 93 deletions

View File

@@ -1,7 +1,7 @@
# 项目信息
framework:
name: "CN EMS"
version: "2.2407.1"
version: "2.2407.2"
# 应用服务配置
server:

View File

@@ -16,13 +16,9 @@ import (
// data = append(data, []string{"1", "2", "3"})
// err := file.WriterCSVFile(data, filePath)
func WriterFileCSV(data [][]string, filePath string) error {
// 获取文件所在的目录路径
dirPath := filepath.Dir(filePath)
// 确保文件夹路径存在
err := os.MkdirAll(dirPath, 0775)
if err != nil {
logger.Errorf("MkdirAll dir %v", err)
// 创建本地输出目录
if err := os.MkdirAll(filepath.Dir(filePath), 0775); err != nil {
return err
}
// 创建或打开文件

View File

@@ -59,6 +59,11 @@ func CompressZipByFile(zipFilePath, filePath string) error {
// CompressZipByDir 将目录下文件添加到 ZIP 压缩文件
func CompressZipByDir(zipFilePath, dirPath string) error {
// 创建本地输出目录
if err := os.MkdirAll(filepath.Dir(zipFilePath), 0775); err != nil {
return err
}
// 创建输出文件
zipWriter, err := os.Create(zipFilePath)
if err != nil {

View File

@@ -160,7 +160,7 @@ func Reset() error {
// return fmt.Errorf("not support window")
} else {
// 重置数据库
if _, err := cmd.Execf("sudo /usr/local/omc/bin/setomc.sh -m install"); err != nil {
if _, err := cmd.Execf("/usr/local/omc/bin/setomc.sh -m install"); err != nil {
return err
}
// 重启服务

View File

@@ -39,8 +39,8 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
// 遍历远程文件和子目录并复制到本地
for _, remoteFile := range remoteFiles {
remotePath := filepath.Join(remoteDir, remoteFile.Name())
localPath := filepath.Join(localDir, remoteFile.Name())
remotePath := filepath.ToSlash(filepath.Join(remoteDir, remoteFile.Name()))
localPath := filepath.ToSlash(filepath.Join(localDir, remoteFile.Name()))
if remoteFile.IsDir() {
// 如果是子目录,则递归复制子目录
@@ -51,25 +51,10 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
}
} else {
// 如果是文件,则复制文件内容
remoteFile, err := s.Client.Open(remotePath)
if err != nil {
if err := s.CopyFileRemoteToLocal(remotePath, localPath); err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to opening remote file %s: => %s", remotePath, err.Error())
continue
}
defer remoteFile.Close()
localFile, err := os.Create(localPath)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to creating local file %s: => %s", localPath, err.Error())
continue
}
defer localFile.Close()
_, err = io.Copy(localFile, remoteFile)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to copying file contents from %s to %s: => %s", remotePath, localPath, err.Error())
continue
}
}
}
return nil
@@ -134,6 +119,10 @@ func (s *SSHClientSFTP) CopyDirLocalToRemote(localDir, remoteDir string) error {
// CopyDirRemoteToLocal 复制文件-远程到本地
func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) error {
if err := os.MkdirAll(filepath.Dir(localPath), 0775); err != nil {
return err
}
// 打开远程文件
remoteFile, err := s.Client.Open(remotePath)
if err != nil {
@@ -142,21 +131,6 @@ func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) erro
}
defer remoteFile.Close()
if err := os.MkdirAll(filepath.Dir(localPath), 0775); err != nil {
return err
}
// 如果目标文件已经存在,先将目标文件重命名
// if info, err := os.Stat(localPath); err == nil && !info.IsDir() {
// ext := filepath.Ext(localPath)
// name := localPath[0 : len(localPath)-len(ext)]
// newName := fmt.Sprintf("%s-%s%s", name, time.Now().Format("20060102_150405"), ext)
// err := os.Rename(localPath, newName)
// if err != nil {
// return err
// }
// }
// 创建本地文件
localFile, err := os.Create(localPath)
if err != nil {

View File

@@ -7,7 +7,6 @@ import (
"strings"
"time"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/ssh"
"be.ems/src/modules/network_element/model"
@@ -203,15 +202,15 @@ func (r *NeVersionImpl) operateCommand(action, neType string, neFilePaths []stri
omcStrArr := []string{}
omcStrArr = append(omcStrArr, pkgCmdStr)
if action == "install" {
omcStrArr = append(omcStrArr, "sudo /usr/local/omc/bin/setomc.sh -m install") // 初始化数据库
omcStrArr = append(omcStrArr, "/usr/local/omc/bin/setomc.sh -m install") // 初始化数据库
} else {
omcStrArr = append(omcStrArr, "sudo /usr/local/omc/bin/setomc.sh -m upgrade") // 升级数据库
omcStrArr = append(omcStrArr, "/usr/local/omc/bin/setomc.sh -m upgrade") // 升级数据库
}
omcStrArr = append(omcStrArr, "sudo systemctl restart restagent") // 重启服务
omcStrArr = append(omcStrArr, fmt.Sprintf("sudo rm %s", strings.Join(neFilePaths, " "))) // 删除软件包
// 2s后安装
cmdStrArr = append(cmdStrArr, fmt.Sprintf("nohup sh -c \"sleep 2s && %s\" > /dev/null 2>&1 & \n", strings.Join(omcStrArr, " && ")))
cmdStrArr = append(cmdStrArr, fmt.Sprintf("nohup sh -c \"sleep 2s && %s\" > /tmp/omc_%s.out 2>&1 & \n", strings.Join(omcStrArr, " && "), action))
// 结束
cmdStrArr = append(cmdStrArr, fmt.Sprintf("echo '%s' \n", okFlagStr))
return okFlagStr, cmdStrArr, nil
@@ -516,8 +515,6 @@ func (r *NeVersionImpl) operateRun(sshClient *ssh.ConnSSH, preinput map[string]s
for {
select {
case <-timeoutTicker.C:
logger.Warnf("NeVersion operateRun %s", commandLineText)
logger.Errorf("neinfo ssh client session read timeout")
done <- true
return
case <-msTicker.C: