新节点默认无在线;提供 debug 配置项以控制是否启用保存配置记录

This commit is contained in:
2025-12-26 16:43:08 +08:00
parent 2c818576ef
commit 54c2903c75
15 changed files with 60 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
package actions package actions
import ( import (
"zzman/model" "jhman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -2,7 +2,7 @@ package actions
import ( import (
"fmt" "fmt"
"zzman/model" "jhman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -2,8 +2,8 @@ package actions
import ( import (
"fmt" "fmt"
"zzman/clients/jd" "jhman/clients/jd"
"zzman/model" "jhman/model"
) )
func Clear() error { func Clear() error {

View File

@@ -1,7 +1,7 @@
package actions package actions
import ( import (
"zzman/model" "jhman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -2,7 +2,7 @@ package actions
import ( import (
"fmt" "fmt"
"zzman/model" "jhman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -81,7 +81,7 @@ func SaveEdges(tx *gorm.DB, edges []model.Edge) error {
updateQuery := fmt.Sprintf(` updateQuery := fmt.Sprintf(`
UPDATE edge e UPDATE edge e
JOIN %s te ON e.id = te.id JOIN %s te ON e.id = te.id
SET SET
e.macaddr = te.macaddr, e.macaddr = te.macaddr,
e.public = te.public, e.public = te.public,
e.isp = te.isp, e.isp = te.isp,

View File

@@ -2,8 +2,8 @@ package actions
import ( import (
"fmt" "fmt"
"jhman/model"
"math" "math"
"zzman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -2,10 +2,10 @@ package actions
import ( import (
"fmt" "fmt"
"jhman/model"
"strconv" "strconv"
"zzman/model"
u "zzman/util" u "jhman/util"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -3,9 +3,9 @@ package actions
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"jhman/clients/jd"
"jhman/model"
"time" "time"
"zzman/clients/jd"
"zzman/model"
) )
func RecordSubmit(time time.Time, gatewat model.Gateway, edges []jd.EdgeInfo) error { func RecordSubmit(time time.Time, gatewat model.Gateway, edges []jd.EdgeInfo) error {

View File

@@ -2,9 +2,9 @@ package actions
import ( import (
"fmt" "fmt"
"jhman/clients/jd"
"jhman/model"
"log/slog" "log/slog"
"zzman/clients/jd"
"zzman/model"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@@ -2,11 +2,12 @@ package actions
import ( import (
"fmt" "fmt"
"jhman/clients/jd"
"jhman/model"
u "jhman/util"
"log/slog" "log/slog"
"os"
"time" "time"
"zzman/clients/jd"
"zzman/model"
u "zzman/util"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -140,7 +141,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
Id: config.Id, Id: config.Id,
EdgeMac: u.P(edge.Macaddr), EdgeMac: u.P(edge.Macaddr),
IsChange: u.P(0), IsChange: u.P(0),
IsOnline: u.P(1), IsOnline: u.P(0),
}, },
} }
changesCreate = append(changesCreate, model.Change{ changesCreate = append(changesCreate, model.Change{
@@ -166,9 +167,11 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
step = time.Now() step = time.Now()
// 记录节点变更 // 记录节点变更
err = RecordChanges(tx, changesCreate) if os.Getenv("DEBUG") == "true" {
if err != nil { err = RecordChanges(tx, changesCreate)
return fmt.Errorf("记录节点变更失败:%w", err) if err != nil {
return fmt.Errorf("记录节点变更失败:%w", err)
}
} }
println(fmt.Sprintf("记录节点变更:%v", time.Since(step))) println(fmt.Sprintf("记录节点变更:%v", time.Since(step)))
@@ -203,9 +206,11 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
} }
// 记录提交配置 // 记录提交配置
err = RecordSubmit(now, gateway, edges) if os.Getenv("DEBUG") == "true" {
if err != nil { err = RecordSubmit(now, gateway, edges)
return fmt.Errorf("记录网关 %s 提交配置失败:%w", gateway.Macaddr, err) if err != nil {
return fmt.Errorf("记录网关 %s 提交配置失败:%w", gateway.Macaddr, err)
}
} }
// 提交配置到云端:配置版本 gateway.ConfigVersion // 提交配置到云端:配置版本 gateway.ConfigVersion

View File

@@ -6,11 +6,12 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/redis/go-redis/v9"
"io" "io"
"jhman/clients"
"net/http" "net/http"
"time" "time"
"zzman/clients"
"github.com/redis/go-redis/v9"
) )
const base = "https://smart.jdbox.xyz:58001" const base = "https://smart.jdbox.xyz:58001"

2
go.mod
View File

@@ -1,4 +1,4 @@
module zzman module jhman
go 1.24.0 go 1.24.0

View File

@@ -2,14 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"jhman/actions"
"jhman/clients"
"jhman/model"
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"slices" "slices"
"strings" "strings"
"zzman/actions"
"zzman/clients"
"zzman/model"
"github.com/joho/godotenv" "github.com/joho/godotenv"
slogjournal "github.com/systemd/slog-journal" slogjournal "github.com/systemd/slog-journal"

12
rollback.sh Normal file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -e
# 此脚本应当运行在服务端,与 deploy.ps1 脚本上传到的位置配合,自动切换回退
cp cli cli.new
chmod -x cli.new
chmod +x cli.bak
mv cli.bak cli
echo 'done~'

12
update.sh Normal file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -e
# 此脚本应当运行在服务端,与 deploy.ps1 脚本上传到的位置配合,自动切换更新
cp cli cli.bak
chmod -x cli.bak
chmod +x cli.new
mv cli.new cli
echo 'done~'