修复待升级节点版本号对比错误

This commit is contained in:
GoEdgeLab
2021-01-18 12:35:29 +08:00
parent ce664ebf69
commit 9b60ed9ba9
5 changed files with 77 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
@@ -10,6 +11,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
@@ -603,10 +605,10 @@ func (this *NodeDAO) CountAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterI
Where("status IS NOT NULL").
Where("JSON_EXTRACT(status, '$.os')=:os").
Where("JSON_EXTRACT(status, '$.arch')=:arch").
Where("INET_ATON(JSON_UNQUOTE(JSON_EXTRACT(status, '$.buildVersion')))<INET_ATON(:version)").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("os", os).
Param("arch", arch).
Param("version", version).
Param("version", utils.VersionToLong(version)).
Count()
}
@@ -618,13 +620,14 @@ func (this *NodeDAO) FindAllLowerVersionNodesWithClusterId(tx *dbs.Tx, clusterId
Where("status IS NOT NULL").
Where("JSON_EXTRACT(status, '$.os')=:os").
Where("JSON_EXTRACT(status, '$.arch')=:arch").
Where("INET_ATON(JSON_UNQUOTE(JSON_EXTRACT(status, '$.buildVersion')))<INET_ATON(:version)").
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
Param("os", os).
Param("arch", arch).
Param("version", version).
Param("version", utils.VersionToLong(version)).
DescPk().
Slice(&result).
FindAll()
logs.Println(len(result), version) // TODO
return
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/events"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/lists"
"github.com/shirou/gopsutil/cpu"
@@ -54,6 +55,7 @@ func (this *NodeStatusExecutor) update() {
status := &nodeconfigs.NodeStatus{}
status.BuildVersion = teaconst.Version
status.BuildVersionCode = utils.VersionToLong(teaconst.Version)
status.OS = runtime.GOOS
status.Arch = runtime.GOARCH
status.ConfigVersion = 0

19
internal/utils/ip.go Normal file
View File

@@ -0,0 +1,19 @@
package utils
import (
"encoding/binary"
"net"
)
// 将IP转换为整型
func IP2Long(ip string) uint32 {
s := net.ParseIP(ip)
if s == nil {
return 0
}
if len(s) == 16 {
return binary.BigEndian.Uint32(s[12:16])
}
return binary.BigEndian.Uint32(s)
}

18
internal/utils/version.go Normal file
View File

@@ -0,0 +1,18 @@
package utils
import (
"strings"
)
// 计算版本代号
func VersionToLong(version string) uint32 {
countDots := strings.Count(version, ".")
if countDots == 2 {
version += ".0"
} else if countDots == 1 {
version += ".0.0"
} else if countDots == 0 {
version += ".0.0.0"
}
return IP2Long(version)
}

View File

@@ -0,0 +1,31 @@
package utils
import "testing"
func TestNodeStatus_ComputerBuildVersionCode(t *testing.T) {
{
t.Log("", VersionToLong(""))
}
{
t.Log("0.0.6", VersionToLong("0.0.6"))
}
{
t.Log("0.0.6.1", VersionToLong("0.0.6.1"))
}
{
t.Log("0.0.7", VersionToLong("0.0.7"))
}
{
t.Log("0.7", VersionToLong("0.7"))
}
{
t.Log("7", VersionToLong("7"))
}
{
t.Log("7.0.1", VersionToLong("7.0.1"))
}
}