自动更新管控端
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.
 
 
 
 
 
 

151 lines
3.8 KiB

package core
import (
"fmt"
"net/rpc"
"os"
"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"
}()
// 返回压缩包名称
*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()
// 将服务器的信息拆分为ip和端口
harr := strings.Split(remote, ":")
dstip := harr[0]
dport := harr[1]
// 连接到RPC服务器
service := fmt.Sprintf("%v:%v", dstip, dport)
client, err := NewUpFileClient(service)
if err != nil {
logger.Error("NewUpFileClient failed", zap.Error(err))
}
defer client.rpcClient.Close()
// 调用 transferFile
err = transferFile(client, filePath, uploadPath)
if err != nil {
logger.Error("TransferFile failed", zap.Error(err))
fmt.Printf("TransferFile failed: %v\n", err)
}
fmt.Printf("TransferFile success\n")
}
// 传输文件
func transferFile(c *UpFileClient, filePath string, uploadPath string) error {
// 发送文件信息
fmt.Printf("TransferFile filePath: %s, uploadPath: %s\n", filePath, uploadPath)
// 获取文件信息
fileInfo, err := os.Stat(filePath)
if err != nil {
// panic(err)
fmt.Printf("获取文件信息失败: %v\n", err)
return err
}
// 提取文件名
// 远程的文件名
fileName := filepath.Base(uploadPath)
fmt.Printf("fileName: %s\n", fileName)
// 远程服务器的路径
dirpath := filepath.Dir(uploadPath)
fmt.Printf("remote dirpath: %s\n", dirpath)
fmt.Printf("file size: %d\n", fileInfo.Size())
// 异步
go func() {
// 发送文件信息
var reply string
c.rpcClient.Call("UpFileService.SendFileInfo", FileInfo{
FileName: uploadPath,
FileSize: fileInfo.Size(),
}, &reply)
// 输出执行的结果
fmt.Printf("SendFileInfo result: %v\n", reply)
}()
return nil
}