You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.1 KiB
117 lines
3.1 KiB
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net/rpc"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"scagent/config"
|
|
"scagent/util"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// 压缩文件
|
|
type ZipBlock struct {
|
|
Basepath string
|
|
Curpath string // 当前路径
|
|
SelFile []string // 选中的文件
|
|
RemoteAddr string // 远程主机地址,带端口
|
|
RemotePath string // 远程主机的存储路径
|
|
}
|
|
|
|
// FileTransferService 定义文件传输服务
|
|
type FileService struct{}
|
|
|
|
// 客户端
|
|
type UpFileClient struct {
|
|
rpcClient *rpc.Client
|
|
}
|
|
|
|
// 创建新的客户端
|
|
func NewUpFileClient(addr string) (*UpFileClient, error) {
|
|
client, err := rpc.Dial("tcp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &UpFileClient{rpcClient: client}, nil
|
|
}
|
|
|
|
// 压缩文件
|
|
// 返回压缩包的名称
|
|
func (f *FileService) Compress(args *ZipBlock, reply *string) error {
|
|
// fmt.Printf("compress args: %v\n", args)
|
|
// 启用日志
|
|
logger := util.NewProductionLogger()
|
|
defer logger.Sync()
|
|
// 实际路径
|
|
realFilePath := filepath.Join(args.Basepath, args.Curpath)
|
|
logger.Info("Compress", zap.String("realFilePath", realFilePath))
|
|
// zip 文件名
|
|
zpFileName := "BIU_" + time.Now().Format("20060102_150405") + ".zip"
|
|
// 创建zip 异步?
|
|
taskId := make(chan string)
|
|
// 异步压缩
|
|
go func() {
|
|
util.CompressToZip(zpFileName, realFilePath, args.SelFile)
|
|
taskId <- "arcok"
|
|
// 日志输出
|
|
logger.Info("压缩文件", zap.String("filename", zpFileName), zap.String("path", realFilePath))
|
|
}()
|
|
// ZIP 文件的实际路径
|
|
ziprl := path.Join(config.G.FilePath, "/sync_zips", zpFileName)
|
|
// zip 创建成功后
|
|
rest := <-taskId
|
|
// 如果压缩包生成成功以后再执行
|
|
if strings.EqualFold(strings.ToLower(rest), "arcok") {
|
|
// 合并为实际路径
|
|
remotePath := path.Join("/www/wwwroot", args.RemotePath, zpFileName)
|
|
// 异步发送文件到远程服务器上
|
|
|
|
go func() {
|
|
clientUploadFile(args.RemoteAddr, ziprl, remotePath)
|
|
taskId <- "upok"
|
|
}()
|
|
|
|
// 等待上传完成
|
|
rest := <-taskId
|
|
if strings.EqualFold(strings.ToLower(rest), "upok") {
|
|
// 解压缩文件,文件存在的文化
|
|
util.DecompressZip(remotePath)
|
|
// 上传完成后删除本地压缩包
|
|
// util.DeleteFile(ziprl)
|
|
}
|
|
|
|
// 返回压缩包名称
|
|
*reply = ziprl
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 客户端调用RPC传送文件的函数
|
|
// 传入zip的实际路径,远程的服务器带端口,远程文件存放路径
|
|
func clientUploadFile(remote string, filePath string, uploadPath string) {
|
|
fmt.Printf("remote: %s, filePath: %s, uploadPath: %s\n", remote, filePath, uploadPath)
|
|
// 启用日志
|
|
logger := util.NewProductionLogger()
|
|
defer logger.Sync()
|
|
// 调用外部组件scalib 程序执行
|
|
// ./scalib -h 192.168.66.92:9098 -f /www/gfs/sync_zips/BIU_20251009_153640.zip -u /www/afs/logs/
|
|
// 执行命令
|
|
cmd := fmt.Sprintf("./scalib -h %s -f %s -u %s", remote, filePath, uploadPath)
|
|
// 执行系统命令
|
|
exec.Command(cmd)
|
|
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
|
|
|
|
if err != nil {
|
|
logger.Error("ExecCommand failed", zap.Error(err))
|
|
fmt.Printf("ExecCommand failed: %v\n", err)
|
|
return
|
|
}
|
|
fmt.Printf("out: %s\n", out)
|
|
|
|
}
|
|
|