Skip to content

Commit 71681b1

Browse files
committed
提供下载最新边缘节点API
1 parent 6b2a015 commit 71681b1

File tree

7 files changed

+207
-21
lines changed

7 files changed

+207
-21
lines changed

internal/db/models/node_dao.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,27 @@ func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byt
402402
return err
403403
}
404404

405+
// FindNodeStatus 获取节点状态
406+
func (this *NodeDAO) FindNodeStatus(tx *dbs.Tx, nodeId int64) (*nodeconfigs.NodeStatus, error) {
407+
statusJSONString, err := this.Query(tx).
408+
Pk(nodeId).
409+
Result("status").
410+
FindStringCol("")
411+
if err != nil {
412+
return nil, err
413+
}
414+
if len(statusJSONString) == 0 {
415+
return nil, nil
416+
}
417+
418+
status := &nodeconfigs.NodeStatus{}
419+
err = json.Unmarshal([]byte(statusJSONString), status)
420+
if err != nil {
421+
return nil, err
422+
}
423+
return status, nil
424+
}
425+
405426
// UpdateNodeIsActive 更改节点在线状态
406427
func (this *NodeDAO) UpdateNodeIsActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
407428
b := "true"

internal/db/models/node_task_dao.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
type NodeTaskType = string
1414

1515
const (
16-
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
17-
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
16+
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
17+
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
18+
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
1819
)
1920

2021
type NodeTaskDAO dbs.DAO

internal/installers/deploy_file.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
2+
3+
package installers
4+
5+
import (
6+
"crypto/md5"
7+
"fmt"
8+
"io"
9+
"os"
10+
)
11+
12+
// DeployFile 部署文件描述
13+
type DeployFile struct {
14+
OS string
15+
Arch string
16+
Version string
17+
Path string
18+
}
19+
20+
// Sum 计算概要
21+
func (this *DeployFile) Sum() (string, error) {
22+
fp, err := os.Open(this.Path)
23+
if err != nil {
24+
return "", err
25+
}
26+
defer func() {
27+
_ = fp.Close()
28+
}()
29+
30+
m := md5.New()
31+
buffer := make([]byte, 128*1024)
32+
for {
33+
n, err := fp.Read(buffer)
34+
if err != nil {
35+
if err == io.EOF {
36+
break
37+
}
38+
return "", err
39+
}
40+
_, err = m.Write(buffer[:n])
41+
if err != nil {
42+
return "", err
43+
}
44+
}
45+
sum := m.Sum(nil)
46+
return fmt.Sprintf("%x", sum), nil
47+
}
48+
49+
// Read 读取一个片段数据
50+
func (this *DeployFile) Read(offset int64) (data []byte, newOffset int64, err error) {
51+
fp, err := os.Open(this.Path)
52+
if err != nil {
53+
return nil, offset, err
54+
}
55+
defer func() {
56+
_ = fp.Close()
57+
}()
58+
59+
stat, err := fp.Stat()
60+
if err != nil {
61+
return nil, offset, err
62+
}
63+
if offset >= stat.Size() {
64+
return nil, offset, io.EOF
65+
}
66+
67+
_, err = fp.Seek(offset, io.SeekStart)
68+
if err != nil {
69+
return nil, offset, err
70+
}
71+
72+
buffer := make([]byte, 128*1024)
73+
n, err := fp.Read(buffer)
74+
if err != nil {
75+
return nil, offset, err
76+
}
77+
78+
return buffer[:n], offset + int64(n), nil
79+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
2+
3+
package installers
4+
5+
import (
6+
"io"
7+
"testing"
8+
)
9+
10+
func TestDeployFile_Sum(t *testing.T) {
11+
d := &DeployFile{Path: "deploy_test.txt"}
12+
sum, err := d.Sum()
13+
if err != nil {
14+
t.Log("err:", err)
15+
return
16+
}
17+
t.Log("sum:", sum)
18+
}
19+
20+
func TestDeployFile_Read(t *testing.T) {
21+
d := &DeployFile{Path: "deploy_test.txt"}
22+
23+
var offset int64
24+
for i := 0; i < 3; i++ {
25+
data, newOffset, err := d.Read(offset)
26+
if err != nil {
27+
if err == io.EOF {
28+
break
29+
}
30+
t.Log("err: ", err)
31+
return
32+
}
33+
t.Log("offset:", newOffset, "data:", string(data))
34+
offset = newOffset
35+
}
36+
}

internal/installers/deploy_manager.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ import (
99

1010
var SharedDeployManager = NewDeployManager()
1111

12-
type DeployFile struct {
13-
OS string
14-
Arch string
15-
Version string
16-
Path string
17-
}
18-
1912
type DeployManager struct {
2013
dir string
2114
}
2215

16+
// NewDeployManager 节点部署文件管理器
2317
func NewDeployManager() *DeployManager {
2418
return &DeployManager{
2519
dir: Tea.Root + "/deploy",
@@ -61,7 +55,15 @@ func (this *DeployManager) LoadNodeFiles() []*DeployFile {
6155
return result
6256
}
6357

64-
58+
// FindNodeFile 查找特别平台的节点文件
59+
func (this *DeployManager) FindNodeFile(os string, arch string) *DeployFile {
60+
for _, file := range this.LoadNodeFiles() {
61+
if file.OS == os && file.Arch == arch {
62+
return file
63+
}
64+
}
65+
return nil
66+
}
6567

6668
// LoadNSNodeFiles 加载所有文件
6769
func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
@@ -96,4 +98,4 @@ func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
9698
result = append(result, v)
9799
}
98100
return result
99-
}
101+
}

internal/rpc/services/service_node.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/iwind/TeaGo/types"
1919
stringutil "github.com/iwind/TeaGo/utils/string"
2020
"net"
21+
"path/filepath"
2122
)
2223

2324
// NodeService 边缘节点相关服务
@@ -1349,3 +1350,31 @@ func (this *NodeService) UpdateNodeUp(ctx context.Context, req *pb.UpdateNodeUpR
13491350

13501351
return this.Success()
13511352
}
1353+
1354+
// DownloadNodeInstallationFile 下载最新边缘节点安装文件
1355+
func (this *NodeService) DownloadNodeInstallationFile(ctx context.Context, req *pb.DownloadNodeInstallationFileRequest) (*pb.DownloadNodeInstallationFileResponse, error) {
1356+
_, err := this.ValidateNode(ctx)
1357+
if err != nil {
1358+
return nil, err
1359+
}
1360+
1361+
file := installers.SharedDeployManager.FindNodeFile(req.Os, req.Arch)
1362+
if file == nil {
1363+
return &pb.DownloadNodeInstallationFileResponse{}, nil
1364+
}
1365+
1366+
sum, err := file.Sum()
1367+
if err != nil {
1368+
return nil, err
1369+
}
1370+
1371+
data, offset, err := file.Read(req.ChunkOffset)
1372+
1373+
return &pb.DownloadNodeInstallationFileResponse{
1374+
Sum: sum,
1375+
Offset: offset,
1376+
ChunkData: data,
1377+
Version: file.Version,
1378+
Filename: filepath.Base(file.Path),
1379+
}, nil
1380+
}

internal/rpc/services/service_node_task.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ package services
33
import (
44
"context"
55
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
6+
"github.com/TeaOSLab/EdgeAPI/internal/installers"
67
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
78
"github.com/iwind/TeaGo/dbs"
9+
stringutil "github.com/iwind/TeaGo/utils/string"
810
"time"
911
)
1012

11-
// 节点同步任务相关服务
13+
// NodeTaskService 节点同步任务相关服务
1214
type NodeTaskService struct {
1315
BaseService
1416
}
1517

16-
// 获取单节点同步任务
18+
// FindNodeTasks 获取单节点同步任务
1719
func (this *NodeTaskService) FindNodeTasks(ctx context.Context, req *pb.FindNodeTasksRequest) (*pb.FindNodeTasksResponse, error) {
1820
nodeId, err := this.ValidateNode(ctx)
1921
if err != nil {
@@ -36,10 +38,26 @@ func (this *NodeTaskService) FindNodeTasks(ctx context.Context, req *pb.FindNode
3638
})
3739
}
3840

41+
// 版本更新任务
42+
status, err := models.SharedNodeDAO.FindNodeStatus(tx, nodeId)
43+
if err != nil {
44+
return nil, err
45+
}
46+
if status != nil && len(status.OS) > 0 && len(status.Arch) > 0 && len(status.BuildVersion) > 0 {
47+
deployFile := installers.SharedDeployManager.FindNodeFile(status.OS, status.Arch)
48+
if deployFile != nil {
49+
if stringutil.VersionCompare(deployFile.Version, status.BuildVersion) > 0 {
50+
pbTasks = append(pbTasks, &pb.NodeTask{
51+
Type: models.NodeTaskTypeNodeVersionChanged,
52+
})
53+
}
54+
}
55+
}
56+
3957
return &pb.FindNodeTasksResponse{NodeTasks: pbTasks}, nil
4058
}
4159

42-
// 报告同步任务结果
60+
// ReportNodeTaskDone 报告同步任务结果
4361
func (this *NodeTaskService) ReportNodeTaskDone(ctx context.Context, req *pb.ReportNodeTaskDoneRequest) (*pb.RPCSuccess, error) {
4462
_, err := this.ValidateNode(ctx)
4563
if err != nil {
@@ -55,7 +73,7 @@ func (this *NodeTaskService) ReportNodeTaskDone(ctx context.Context, req *pb.Rep
5573
return this.Success()
5674
}
5775

58-
// 获取所有正在同步的集群信息
76+
// FindNodeClusterTasks 获取所有正在同步的集群信息
5977
func (this *NodeTaskService) FindNodeClusterTasks(ctx context.Context, req *pb.FindNodeClusterTasksRequest) (*pb.FindNodeClusterTasksResponse, error) {
6078
_, err := this.ValidateAdmin(ctx, 0)
6179
if err != nil {
@@ -125,7 +143,7 @@ func (this *NodeTaskService) FindNodeClusterTasks(ctx context.Context, req *pb.F
125143
return &pb.FindNodeClusterTasksResponse{ClusterTasks: pbClusterTasks}, nil
126144
}
127145

128-
// 检查是否有正在执行的任务
146+
// ExistsNodeTasks 检查是否有正在执行的任务
129147
func (this *NodeTaskService) ExistsNodeTasks(ctx context.Context, req *pb.ExistsNodeTasksRequest) (*pb.ExistsNodeTasksResponse, error) {
130148
_, err := this.ValidateAdmin(ctx, 0)
131149
if err != nil {
@@ -154,7 +172,7 @@ func (this *NodeTaskService) ExistsNodeTasks(ctx context.Context, req *pb.Exists
154172
}, nil
155173
}
156174

157-
// 删除任务
175+
// DeleteNodeTask 删除任务
158176
func (this *NodeTaskService) DeleteNodeTask(ctx context.Context, req *pb.DeleteNodeTaskRequest) (*pb.RPCSuccess, error) {
159177
_, err := this.ValidateAdmin(ctx, 0)
160178
if err != nil {
@@ -170,7 +188,7 @@ func (this *NodeTaskService) DeleteNodeTask(ctx context.Context, req *pb.DeleteN
170188
return this.Success()
171189
}
172190

173-
// 批量删除任务
191+
// DeleteNodeTasks 批量删除任务
174192
func (this *NodeTaskService) DeleteNodeTasks(ctx context.Context, req *pb.DeleteNodeTasksRequest) (*pb.RPCSuccess, error) {
175193
_, err := this.ValidateAdmin(ctx, 0)
176194
if err != nil {
@@ -188,7 +206,7 @@ func (this *NodeTaskService) DeleteNodeTasks(ctx context.Context, req *pb.Delete
188206
return this.Success()
189207
}
190208

191-
// 计算正在执行的任务数量
209+
// CountDoingNodeTasks 计算正在执行的任务数量
192210
func (this *NodeTaskService) CountDoingNodeTasks(ctx context.Context, req *pb.CountDoingNodeTasksRequest) (*pb.RPCCountResponse, error) {
193211
_, err := this.ValidateAdmin(ctx, 0)
194212
if err != nil {
@@ -205,7 +223,7 @@ func (this *NodeTaskService) CountDoingNodeTasks(ctx context.Context, req *pb.Co
205223
return this.SuccessCount(count)
206224
}
207225

208-
// 查找需要通知的任务
226+
// FindNotifyingNodeTasks 查找需要通知的任务
209227
func (this *NodeTaskService) FindNotifyingNodeTasks(ctx context.Context, req *pb.FindNotifyingNodeTasksRequest) (*pb.FindNotifyingNodeTasksResponse, error) {
210228
_, err := this.ValidateAdmin(ctx, 0)
211229
if err != nil {
@@ -241,7 +259,7 @@ func (this *NodeTaskService) FindNotifyingNodeTasks(ctx context.Context, req *pb
241259
return &pb.FindNotifyingNodeTasksResponse{NodeTasks: pbTasks}, nil
242260
}
243261

244-
// 设置任务已通知
262+
// UpdateNodeTasksNotified 设置任务已通知
245263
func (this *NodeTaskService) UpdateNodeTasksNotified(ctx context.Context, req *pb.UpdateNodeTasksNotifiedRequest) (*pb.RPCSuccess, error) {
246264
_, err := this.ValidateAdmin(ctx, 0)
247265
if err != nil {

0 commit comments

Comments
 (0)