Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93a17ced7c | ||
|
|
580d09ef99 | ||
|
|
fed725d45c | ||
|
|
350b514fc7 | ||
|
|
71d2671c04 | ||
|
|
5a22146309 | ||
|
|
ac2fb4c84b | ||
|
|
c25e3f18e0 | ||
|
|
d3e4f28c69 | ||
|
|
363892efb2 | ||
|
|
cf36559eea | ||
|
|
fa3e0ca6ab | ||
|
|
7229b0db34 | ||
|
|
23871804b1 | ||
|
|
5e00bfa4c1 | ||
|
|
473a2db335 | ||
|
|
c893de8af7 | ||
|
|
a8cf04d178 | ||
|
|
e94a7f9a77 | ||
|
|
ccf435ee8e | ||
|
|
7dc5c5f349 | ||
|
|
566c04f080 | ||
|
|
5a13c7663c | ||
|
|
1df6d579d7 | ||
|
|
20110495ab | ||
|
|
ce8d656d65 | ||
|
|
186fe3c365 | ||
|
|
a9ce2f45df | ||
|
|
5105af9918 | ||
|
|
378c485219 | ||
|
|
2465993e2c | ||
|
|
818c1c25a7 | ||
|
|
77c67ccd3f | ||
|
|
d855fdedde | ||
|
|
95c734c87a | ||
|
|
89ff1927b7 | ||
|
|
1a3aaf2846 | ||
|
|
7d25019abb | ||
|
|
4cfc7d5387 | ||
|
|
9245cf9cdb | ||
|
|
9e1e57dfd8 | ||
|
|
87f032bebd | ||
|
|
bda407fc3f | ||
|
|
3b2f6060b8 | ||
|
|
a15ad7dd04 | ||
|
|
a415ef6070 | ||
|
|
d94a822f52 | ||
|
|
9e3fb9cf66 | ||
|
|
84a9916d3e | ||
|
|
7d1ae0d242 | ||
|
|
3fbad22218 | ||
|
|
5fffc8a833 | ||
|
|
2c72d28c48 | ||
|
|
45ea803e7a | ||
|
|
edd4b59207 | ||
|
|
38ad11fda0 | ||
|
|
961d9b4dbc | ||
|
|
6436e83d9e | ||
|
|
b2d3d483e8 | ||
|
|
89ec81f6b1 | ||
|
|
5124169e28 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*_plus.go
|
||||||
|
*-plus.sh
|
||||||
@@ -6,6 +6,7 @@ function build() {
|
|||||||
DIST=$ROOT/"../dist/${NAME}"
|
DIST=$ROOT/"../dist/${NAME}"
|
||||||
OS=${1}
|
OS=${1}
|
||||||
ARCH=${2}
|
ARCH=${2}
|
||||||
|
TAG=${3}
|
||||||
|
|
||||||
if [ -z $OS ]; then
|
if [ -z $OS ]; then
|
||||||
echo "usage: build.sh OS ARCH"
|
echo "usage: build.sh OS ARCH"
|
||||||
@@ -15,11 +16,14 @@ function build() {
|
|||||||
echo "usage: build.sh OS ARCH"
|
echo "usage: build.sh OS ARCH"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
if [ -z $TAG ]; then
|
||||||
|
TAG="community"
|
||||||
|
fi
|
||||||
|
|
||||||
VERSION=$(lookup-version $ROOT/../internal/const/const.go)
|
VERSION=$(lookup-version $ROOT/../internal/const/const.go)
|
||||||
ZIP="${NAME}-${OS}-${ARCH}-v${VERSION}.zip"
|
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
|
||||||
|
|
||||||
# check edge-node
|
# build edge-node
|
||||||
NodeVersion=$(lookup-version $ROOT"/../../EdgeNode/internal/const/const.go")
|
NodeVersion=$(lookup-version $ROOT"/../../EdgeNode/internal/const/const.go")
|
||||||
echo "building edge-node v${NodeVersion} ..."
|
echo "building edge-node v${NodeVersion} ..."
|
||||||
EDGE_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeNode/build/build.sh"
|
EDGE_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeNode/build/build.sh"
|
||||||
@@ -31,16 +35,46 @@ function build() {
|
|||||||
echo "=============================="
|
echo "=============================="
|
||||||
architects=("amd64" "386" "arm64" "mips64" "mips64le")
|
architects=("amd64" "386" "arm64" "mips64" "mips64le")
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
./build.sh linux $arch
|
if [ ! -f $ROOT"/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" ]; then
|
||||||
|
./build.sh linux $arch $TAG
|
||||||
|
else
|
||||||
|
echo "use built node linux/$arch/v${NodeVersion}"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
cd -
|
cd -
|
||||||
|
|
||||||
rm -f $ROOT/deploy/*.zip
|
rm -f $ROOT/deploy/*.zip
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
cp $ROOT"/../../EdgeNode/dist/edge-node-linux-${arch}-v${NodeVersion}.zip" $ROOT/deploy/
|
cp $ROOT"/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" $ROOT/deploy/edge-node-linux-${arch}-v${NodeVersion}.zip
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# build edge-dns
|
||||||
|
if [ "$TAG" = "plus" ]; then
|
||||||
|
DNS_ROOT=$ROOT"/../../EdgeDNS"
|
||||||
|
if [ -d $DNS_ROOT ]; then
|
||||||
|
DNSNodeVersion=$(lookup-version $ROOT"/../../EdgeDNS/internal/const/const.go")
|
||||||
|
echo "building edge-dns ${DNSNodeVersion} ..."
|
||||||
|
EDGE_DNS_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeDNS/build/build.sh"
|
||||||
|
if [ ! -f $EDGE_DNS_NODE_BUILD_SCRIPT ]; then
|
||||||
|
echo "unable to find edge-dns build script 'EdgeDNS/build/build.sh'"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
cd $ROOT"/../../EdgeDNS/build"
|
||||||
|
echo "=============================="
|
||||||
|
architects=("amd64")
|
||||||
|
for arch in "${architects[@]}"; do
|
||||||
|
./build.sh linux $arch $TAG
|
||||||
|
done
|
||||||
|
echo "=============================="
|
||||||
|
cd -
|
||||||
|
|
||||||
|
for arch in "${architects[@]}"; do
|
||||||
|
cp $ROOT"/../../EdgeDNS/dist/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip" $ROOT/deploy/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# build sql
|
# build sql
|
||||||
echo "building sql ..."
|
echo "building sql ..."
|
||||||
${ROOT}/sql.sh
|
${ROOT}/sql.sh
|
||||||
@@ -62,16 +96,24 @@ function build() {
|
|||||||
rm -f $DIST/resources/ipdata/ip2region/global_region.csv
|
rm -f $DIST/resources/ipdata/ip2region/global_region.csv
|
||||||
rm -f $DIST/resources/ipdata/ip2region/ip.merge.txt
|
rm -f $DIST/resources/ipdata/ip2region/ip.merge.txt
|
||||||
|
|
||||||
# building installer
|
# building edge installer
|
||||||
echo "building installer ..."
|
echo "building node installer ..."
|
||||||
architects=("amd64" "386")
|
architects=("amd64" "386" "arm64")
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
# TODO support arm, mips ...
|
# TODO support arm, mips ...
|
||||||
env GOOS=linux GOARCH=${arch} go build --ldflags="-s -w" -o $ROOT/installers/edge-installer-helper-linux-${arch} $ROOT/../cmd/installer-helper/main.go
|
env GOOS=linux GOARCH=${arch} go build -tags $TAG --ldflags="-s -w" -o $ROOT/installers/edge-installer-helper-linux-${arch} $ROOT/../cmd/installer-helper/main.go
|
||||||
|
done
|
||||||
|
|
||||||
|
# building edge dns installer
|
||||||
|
echo "building dns node installer ..."
|
||||||
|
architects=("amd64" "386" "arm64")
|
||||||
|
for arch in "${architects[@]}"; do
|
||||||
|
# TODO support arm, mips ...
|
||||||
|
env GOOS=linux GOARCH=${arch} go build -tags $TAG --ldflags="-s -w" -o $ROOT/installers/edge-installer-dns-helper-linux-${arch} $ROOT/../cmd/installer-dns-helper/main.go
|
||||||
done
|
done
|
||||||
|
|
||||||
# building api node
|
# building api node
|
||||||
env GOOS=$OS GOARCH=$ARCH go build --ldflags="-s -w" -o $DIST/bin/edge-api $ROOT/../cmd/edge-api/main.go
|
env GOOS=$OS GOARCH=$ARCH go build -tags $TAG --ldflags="-s -w" -o $DIST/bin/edge-api $ROOT/../cmd/edge-api/main.go
|
||||||
|
|
||||||
# delete hidden files
|
# delete hidden files
|
||||||
find $DIST -name ".DS_Store" -delete
|
find $DIST -name ".DS_Store" -delete
|
||||||
@@ -102,4 +144,4 @@ function lookup-version() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
build $1 $2
|
build $1 $2 $3
|
||||||
|
|||||||
@@ -44,12 +44,13 @@ func main() {
|
|||||||
_, _ = os.Stdout.Write(resultJSON)
|
_, _ = os.Stdout.Write(resultJSON)
|
||||||
})
|
})
|
||||||
app.On("upgrade", func() {
|
app.On("upgrade", func() {
|
||||||
|
fmt.Println("start ...")
|
||||||
executor, err := setup.NewSQLExecutorFromCmd()
|
executor, err := setup.NewSQLExecutorFromCmd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ERROR: " + err.Error())
|
fmt.Println("ERROR: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = executor.Run()
|
err = executor.Run(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ERROR: " + err.Error())
|
fmt.Println("ERROR: " + err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
73
cmd/installer-dns-helper/main.go
Normal file
73
cmd/installer-dns-helper/main.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cmd := ""
|
||||||
|
flag.StringVar(&cmd, "cmd", "", "command name: [unzip]")
|
||||||
|
|
||||||
|
// unzip
|
||||||
|
zipPath := ""
|
||||||
|
targetPath := ""
|
||||||
|
flag.StringVar(&zipPath, "zip", "", "zip path")
|
||||||
|
flag.StringVar(&targetPath, "target", "", "target dir")
|
||||||
|
|
||||||
|
// parse
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(cmd) == 0 {
|
||||||
|
stderr("need '-cmd=COMMAND' argument")
|
||||||
|
} else if cmd == "test" {
|
||||||
|
// 检查是否正在运行
|
||||||
|
var sock = gosock.NewTmpSock("edge-dns")
|
||||||
|
if sock.IsListening() {
|
||||||
|
// 从systemd中停止
|
||||||
|
systemctl, _ := exec.LookPath("systemctl")
|
||||||
|
if len(systemctl) > 0 {
|
||||||
|
systemctlCmd := exec.Command(systemctl, "stop", "edge-dns")
|
||||||
|
_ = systemctlCmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从进程中停止
|
||||||
|
if sock.IsListening() {
|
||||||
|
_, _ = sock.Send(&gosock.Command{
|
||||||
|
Code: "stop",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if cmd == "unzip" { // 解压
|
||||||
|
if len(zipPath) == 0 {
|
||||||
|
stderr("ERROR: need '-zip=PATH' argument")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(targetPath) == 0 {
|
||||||
|
stderr("ERROR: need '-target=TARGET' argument")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
unzip := utils.NewUnzip(zipPath, targetPath)
|
||||||
|
err := unzip.Run()
|
||||||
|
if err != nil {
|
||||||
|
stderr("ERROR: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout("ok")
|
||||||
|
} else {
|
||||||
|
stderr("ERROR: not recognized command '" + cmd + "'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stdout(s string) {
|
||||||
|
_, _ = os.Stdout.WriteString(s + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func stderr(s string) {
|
||||||
|
_, _ = os.Stderr.WriteString(s + "\n")
|
||||||
|
}
|
||||||
@@ -3,8 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"net"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -24,11 +25,21 @@ func main() {
|
|||||||
stderr("need '-cmd=COMMAND' argument")
|
stderr("need '-cmd=COMMAND' argument")
|
||||||
} else if cmd == "test" {
|
} else if cmd == "test" {
|
||||||
// 检查是否正在运行
|
// 检查是否正在运行
|
||||||
path := os.TempDir() + "/edge-node.sock"
|
var sock = gosock.NewTmpSock("edge-node")
|
||||||
conn, err := net.Dial("unix", path)
|
if sock.IsListening() {
|
||||||
if err == nil {
|
// 从systemd中停止
|
||||||
_ = conn.Close()
|
systemctl, _ := exec.LookPath("systemctl")
|
||||||
stderr("test node status: edge node is running now, can not install again")
|
if len(systemctl) > 0 {
|
||||||
|
systemctlCmd := exec.Command(systemctl, "stop", "edge-node")
|
||||||
|
_ = systemctlCmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从进程中停止
|
||||||
|
if sock.IsListening() {
|
||||||
|
_, _ = sock.Send(&gosock.Command{
|
||||||
|
Code: "stop",
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if cmd == "unzip" { // 解压
|
} else if cmd == "unzip" { // 解压
|
||||||
if len(zipPath) == 0 {
|
if len(zipPath) == 0 {
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -14,7 +14,9 @@ require (
|
|||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible
|
github.com/go-yaml/yaml v2.1.0+incompatible
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060
|
github.com/iwind/TeaGo v0.0.0-20210809112119-a57ed0e84e34
|
||||||
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
||||||
|
github.com/json-iterator/go v1.1.11 // indirect
|
||||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
@@ -26,5 +28,4 @@ require (
|
|||||||
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect
|
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect
|
||||||
google.golang.org/grpc v1.38.0
|
google.golang.org/grpc v1.38.0
|
||||||
google.golang.org/protobuf v1.26.0
|
google.golang.org/protobuf v1.26.0
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
30
go.sum
30
go.sum
@@ -134,7 +134,6 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
|
|||||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||||
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
|
|
||||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||||
@@ -146,8 +145,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
|
|||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
|
|
||||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
@@ -181,18 +180,20 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
|||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
|
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
|
||||||
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f h1:r2O8PONj/KiuZjJHVHn7KlCePUIjNtgAmvLfgRafQ8o=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060 h1:qdLtK4PDXxk2vMKkTWl5Fl9xqYuRCukzWAgJbLHdfOo=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20210806054428-5534da0db9d1 h1:AZKkwTNEZYrpyv62zIkxpLJsWhfOS7OEFovAcwd0aco=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20210806054428-5534da0db9d1/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20210809112119-a57ed0e84e34 h1:ZCNQXLiGF5Z1cV3Pi03zCWzwwjPfsI5XhcrNhTvCFIU=
|
||||||
|
github.com/iwind/TeaGo v0.0.0-20210809112119-a57ed0e84e34/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||||
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
|
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
|
||||||
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
|
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
|
||||||
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
|
||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
||||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
@@ -292,8 +293,6 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
|
|||||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/sacloud/libsacloud v1.36.2/go.mod h1:P7YAOVmnIn3DKHqCZcUKYUXmSwGBm3yS7IBEjKVSrjg=
|
github.com/sacloud/libsacloud v1.36.2/go.mod h1:P7YAOVmnIn3DKHqCZcUKYUXmSwGBm3yS7IBEjKVSrjg=
|
||||||
github.com/shirou/gopsutil v2.20.9+incompatible h1:msXs2frUV+O/JLva9EDLpuJ84PrFsdCTCQex8PUdtkQ=
|
|
||||||
github.com/shirou/gopsutil v2.20.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
|
||||||
github.com/shirou/gopsutil v3.21.5+incompatible h1:OloQyEerMi7JUrXiNzy8wQ5XN+baemxSl12QgIzt0jc=
|
github.com/shirou/gopsutil v3.21.5+incompatible h1:OloQyEerMi7JUrXiNzy8wQ5XN+baemxSl12QgIzt0jc=
|
||||||
github.com/shirou/gopsutil v3.21.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
github.com/shirou/gopsutil v3.21.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||||
@@ -403,8 +402,8 @@ golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLL
|
|||||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
|
|
||||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
@@ -418,8 +417,8 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
|
|||||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
|
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20180622082034-63fc586f45fe/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180622082034-63fc586f45fe/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
@@ -455,9 +454,9 @@ golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
|
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@@ -468,7 +467,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
|
|||||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
@@ -515,8 +513,8 @@ golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjs
|
|||||||
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
|
||||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||||
@@ -553,7 +551,6 @@ google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4
|
|||||||
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||||
google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||||
google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
|
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||||
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced h1:c5geK1iMU3cDKtFrCVQIcjR3W+JOZMuhIyICMCTbtus=
|
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced h1:c5geK1iMU3cDKtFrCVQIcjR3W+JOZMuhIyICMCTbtus=
|
||||||
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24=
|
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24=
|
||||||
@@ -568,7 +565,6 @@ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
|
|||||||
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||||
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||||
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||||
google.golang.org/grpc v1.32.0 h1:zWTV+LMdc3kaiJMSTOFz2UgSBgx8RNQoTGiZu3fR9S0=
|
|
||||||
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||||
google.golang.org/grpc v1.38.0 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0=
|
google.golang.org/grpc v1.38.0 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0=
|
||||||
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
|
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
|
||||||
@@ -580,7 +576,6 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
|
|||||||
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
|
||||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||||
@@ -612,9 +607,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|||||||
66
internal/accesslogs/storage_base.go
Normal file
66
internal/accesslogs/storage_base.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BaseStorage struct {
|
||||||
|
isOk bool
|
||||||
|
version int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *BaseStorage) SetVersion(version int) {
|
||||||
|
this.version = version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *BaseStorage) Version() int {
|
||||||
|
return this.version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *BaseStorage) IsOk() bool {
|
||||||
|
return this.isOk
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *BaseStorage) SetOk(isOk bool) {
|
||||||
|
this.isOk = isOk
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal 对日志进行编码
|
||||||
|
func (this *BaseStorage) Marshal(accessLog *pb.HTTPAccessLog) ([]byte, error) {
|
||||||
|
return json.Marshal(accessLog)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatVariables 格式化字符串中的变量
|
||||||
|
func (this *BaseStorage) FormatVariables(s string) string {
|
||||||
|
now := time.Now()
|
||||||
|
return configutils.ParseVariables(s, func(varName string) (value string) {
|
||||||
|
switch varName {
|
||||||
|
case "year":
|
||||||
|
return strconv.Itoa(now.Year())
|
||||||
|
case "month":
|
||||||
|
return fmt.Sprintf("%02d", now.Month())
|
||||||
|
case "week":
|
||||||
|
_, week := now.ISOWeek()
|
||||||
|
return fmt.Sprintf("%02d", week)
|
||||||
|
case "day":
|
||||||
|
return fmt.Sprintf("%02d", now.Day())
|
||||||
|
case "hour":
|
||||||
|
return fmt.Sprintf("%02d", now.Hour())
|
||||||
|
case "minute":
|
||||||
|
return fmt.Sprintf("%02d", now.Minute())
|
||||||
|
case "second":
|
||||||
|
return fmt.Sprintf("%02d", now.Second())
|
||||||
|
case "date":
|
||||||
|
return fmt.Sprintf("%d%02d%02d", now.Year(), now.Month(), now.Day())
|
||||||
|
}
|
||||||
|
|
||||||
|
return varName
|
||||||
|
})
|
||||||
|
}
|
||||||
95
internal/accesslogs/storage_command.go
Normal file
95
internal/accesslogs/storage_command.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommandStorage 通过命令行存储
|
||||||
|
type CommandStorage struct {
|
||||||
|
BaseStorage
|
||||||
|
|
||||||
|
config *serverconfigs.AccessLogCommandStorageConfig
|
||||||
|
|
||||||
|
writeLocker sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommandStorage(config *serverconfigs.AccessLogCommandStorageConfig) *CommandStorage {
|
||||||
|
return &CommandStorage{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CommandStorage) Config() interface{} {
|
||||||
|
return this.config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 启动
|
||||||
|
func (this *CommandStorage) Start() error {
|
||||||
|
if len(this.config.Command) == 0 {
|
||||||
|
return errors.New("'command' should not be empty")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *CommandStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
if len(accessLogs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeLocker.Lock()
|
||||||
|
defer this.writeLocker.Unlock()
|
||||||
|
|
||||||
|
cmd := exec.Command(this.config.Command, this.config.Args...)
|
||||||
|
if len(this.config.Dir) > 0 {
|
||||||
|
cmd.Dir = this.config.Dir
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout := bytes.NewBuffer([]byte{})
|
||||||
|
cmd.Stdout = stdout
|
||||||
|
|
||||||
|
w, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
data, err := this.Marshal(accessLog)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, err = w.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write([]byte("\n"))
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = w.Close()
|
||||||
|
err = cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
|
||||||
|
if stdout.Len() > 0 {
|
||||||
|
logs.Error(errors.New(string(stdout.Bytes())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
func (this *CommandStorage) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
63
internal/accesslogs/storage_command_test.go
Normal file
63
internal/accesslogs/storage_command_test.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCommandStorage_Write(t *testing.T) {
|
||||||
|
php, err := exec.LookPath("php")
|
||||||
|
if err != nil { // not found php, so we can not test
|
||||||
|
t.Log("php:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
before := time.Now()
|
||||||
|
|
||||||
|
storage := NewCommandStorage(&serverconfigs.AccessLogCommandStorageConfig{
|
||||||
|
Command: php,
|
||||||
|
Args: []string{cwd + "/tests/command_storage.php"},
|
||||||
|
})
|
||||||
|
err = storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/hello",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/world",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/lu",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/ping",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(time.Since(before).Seconds(), "seconds")
|
||||||
|
}
|
||||||
131
internal/accesslogs/storage_es.go
Normal file
131
internal/accesslogs/storage_es.go
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ESStorage ElasticSearch存储策略
|
||||||
|
type ESStorage struct {
|
||||||
|
BaseStorage
|
||||||
|
|
||||||
|
config *serverconfigs.AccessLogESStorageConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewESStorage(config *serverconfigs.AccessLogESStorageConfig) *ESStorage {
|
||||||
|
return &ESStorage{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ESStorage) Config() interface{} {
|
||||||
|
return this.config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 开启
|
||||||
|
func (this *ESStorage) Start() error {
|
||||||
|
if len(this.config.Endpoint) == 0 {
|
||||||
|
return errors.New("'endpoint' should not be nil")
|
||||||
|
}
|
||||||
|
if !regexp.MustCompile(`(?i)^(http|https)://`).MatchString(this.config.Endpoint) {
|
||||||
|
this.config.Endpoint = "http://" + this.config.Endpoint
|
||||||
|
}
|
||||||
|
if len(this.config.Index) == 0 {
|
||||||
|
return errors.New("'index' should not be nil")
|
||||||
|
}
|
||||||
|
if len(this.config.MappingType) == 0 {
|
||||||
|
return errors.New("'mappingType' should not be nil")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
if len(accessLogs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestId int64 = 1_0000_0000_0000_0000
|
||||||
|
|
||||||
|
bulk := &strings.Builder{}
|
||||||
|
indexName := this.FormatVariables(this.config.Index)
|
||||||
|
typeName := this.FormatVariables(this.config.MappingType)
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
if len(accessLog.RequestId) == 0 {
|
||||||
|
accessLog.RequestId = strconv.FormatInt(time.Now().UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + fmt.Sprintf("%08d", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
opData, err := json.Marshal(map[string]interface{}{
|
||||||
|
"index": map[string]interface{}{
|
||||||
|
"_index": indexName,
|
||||||
|
"_type": typeName,
|
||||||
|
"_id": accessLog.RequestId,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "write failed: "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := this.Marshal(accessLog)
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "marshal data failed: "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
bulk.Write(opData)
|
||||||
|
bulk.WriteString("\n")
|
||||||
|
bulk.Write(data)
|
||||||
|
bulk.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
if bulk.Len() == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, this.config.Endpoint+"/_bulk", strings.NewReader(bulk.String()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("User-Agent", strings.ReplaceAll(teaconst.ProductName, " ", "-")+"/"+teaconst.Version)
|
||||||
|
if len(this.config.Username) > 0 || len(this.config.Password) > 0 {
|
||||||
|
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(this.config.Username+":"+this.config.Password)))
|
||||||
|
}
|
||||||
|
client := utils.SharedHttpClient(10 * time.Second)
|
||||||
|
defer func() {
|
||||||
|
_ = req.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
bodyData, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return errors.New("ElasticSearch response status code: " + fmt.Sprintf("%d", resp.StatusCode) + " content: " + string(bodyData))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
func (this *ESStorage) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
53
internal/accesslogs/storage_es_test.go
Normal file
53
internal/accesslogs/storage_es_test.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestESStorage_Write(t *testing.T) {
|
||||||
|
storage := NewESStorage(&serverconfigs.AccessLogESStorageConfig{
|
||||||
|
Endpoint: "http://127.0.0.1:9200",
|
||||||
|
Index: "logs",
|
||||||
|
MappingType: "accessLogs",
|
||||||
|
Username: "hello",
|
||||||
|
Password: "world",
|
||||||
|
})
|
||||||
|
err := storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestMethod: "POST",
|
||||||
|
RequestPath: "/1",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
||||||
|
Header: map[string]*pb.Strings{
|
||||||
|
"Content-Type": {Values: []string{"text/html"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/2",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
||||||
|
Header: map[string]*pb.Strings{
|
||||||
|
"Content-Type": {Values: []string{"text/css"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
127
internal/accesslogs/storage_file.go
Normal file
127
internal/accesslogs/storage_file.go
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileStorage 文件存储策略
|
||||||
|
type FileStorage struct {
|
||||||
|
BaseStorage
|
||||||
|
|
||||||
|
config *serverconfigs.AccessLogFileStorageConfig
|
||||||
|
|
||||||
|
writeLocker sync.Mutex
|
||||||
|
|
||||||
|
files map[string]*os.File // path => *File
|
||||||
|
filesLocker sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileStorage(config *serverconfigs.AccessLogFileStorageConfig) *FileStorage {
|
||||||
|
return &FileStorage{
|
||||||
|
config: config,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *FileStorage) Config() interface{} {
|
||||||
|
return this.config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 开启
|
||||||
|
func (this *FileStorage) Start() error {
|
||||||
|
if len(this.config.Path) == 0 {
|
||||||
|
return errors.New("'path' should not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
this.files = map[string]*os.File{}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write 写入日志
|
||||||
|
func (this *FileStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
if len(accessLogs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fp := this.fp()
|
||||||
|
if fp == nil {
|
||||||
|
return errors.New("file pointer should not be nil")
|
||||||
|
}
|
||||||
|
this.writeLocker.Lock()
|
||||||
|
defer this.writeLocker.Unlock()
|
||||||
|
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
data, err := this.Marshal(accessLog)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, err = fp.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
_ = this.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, _ = fp.WriteString("\n")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
func (this *FileStorage) Close() error {
|
||||||
|
this.filesLocker.Lock()
|
||||||
|
defer this.filesLocker.Unlock()
|
||||||
|
|
||||||
|
var resultErr error
|
||||||
|
for _, f := range this.files {
|
||||||
|
err := f.Close()
|
||||||
|
if err != nil {
|
||||||
|
resultErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *FileStorage) fp() *os.File {
|
||||||
|
path := this.FormatVariables(this.config.Path)
|
||||||
|
|
||||||
|
this.filesLocker.Lock()
|
||||||
|
defer this.filesLocker.Unlock()
|
||||||
|
fp, ok := this.files[path]
|
||||||
|
if ok {
|
||||||
|
return fp
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭其他的文件
|
||||||
|
for _, f := range this.files {
|
||||||
|
_ = f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否创建文件目录
|
||||||
|
if this.config.AutoCreate {
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
_, err := os.Stat(dir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打开新文件
|
||||||
|
fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
this.files[path] = fp
|
||||||
|
|
||||||
|
return fp
|
||||||
|
}
|
||||||
70
internal/accesslogs/storage_file_test.go
Normal file
70
internal/accesslogs/storage_file_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFileStorage_Write(t *testing.T) {
|
||||||
|
storage := NewFileStorage(&serverconfigs.AccessLogFileStorageConfig{
|
||||||
|
Path: Tea.Root + "/logs/access-${date}.log",
|
||||||
|
})
|
||||||
|
err := storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestPath: "/hello",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestPath: "/world",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestPath: "/1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestPath: "/2",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestMethod: "POST",
|
||||||
|
RequestPath: "/1",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/2",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
30
internal/accesslogs/storage_interface.go
Normal file
30
internal/accesslogs/storage_interface.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
|
||||||
|
// StorageInterface 日志存储接口
|
||||||
|
type StorageInterface interface {
|
||||||
|
// Version 获取版本
|
||||||
|
Version() int
|
||||||
|
|
||||||
|
// SetVersion 设置版本
|
||||||
|
SetVersion(version int)
|
||||||
|
|
||||||
|
IsOk() bool
|
||||||
|
|
||||||
|
SetOk(ok bool)
|
||||||
|
|
||||||
|
// Config 获取配置
|
||||||
|
Config() interface{}
|
||||||
|
|
||||||
|
// Start 开启
|
||||||
|
Start() error
|
||||||
|
|
||||||
|
// Write 写入日志
|
||||||
|
Write(accessLogs []*pb.HTTPAccessLog) error
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
199
internal/accesslogs/storage_manager.go
Normal file
199
internal/accesslogs/storage_manager.go
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var SharedStorageManager = NewStorageManager()
|
||||||
|
|
||||||
|
type StorageManager struct {
|
||||||
|
storageMap map[int64]StorageInterface // policyId => Storage
|
||||||
|
|
||||||
|
locker sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorageManager() *StorageManager {
|
||||||
|
return &StorageManager{
|
||||||
|
storageMap: map[int64]StorageInterface{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StorageManager) Start() {
|
||||||
|
var ticker = time.NewTicker(1 * time.Minute)
|
||||||
|
if Tea.IsTesting() {
|
||||||
|
ticker = time.NewTicker(5 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动时执行一次
|
||||||
|
var err = this.Loop()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 循环执行
|
||||||
|
for range ticker.C {
|
||||||
|
err := this.Loop()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
this.locker.Lock()
|
||||||
|
storage, ok := this.storageMap[policyId]
|
||||||
|
this.locker.Unlock()
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !storage.IsOk() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return storage.Write(accessLogs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop 更新
|
||||||
|
func (this *StorageManager) Loop() error {
|
||||||
|
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var policyIds = []int64{}
|
||||||
|
for _, policy := range policies {
|
||||||
|
if policy.IsOn == 1 {
|
||||||
|
policyIds = append(policyIds, int64(policy.Id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.locker.Lock()
|
||||||
|
defer this.locker.Unlock()
|
||||||
|
|
||||||
|
// 关闭不用的
|
||||||
|
for policyId, storage := range this.storageMap {
|
||||||
|
if !lists.ContainsInt64(policyIds, policyId) {
|
||||||
|
err := storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close '"+types.String(policyId)+"' failed: "+err.Error())
|
||||||
|
}
|
||||||
|
delete(this.storageMap, policyId)
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "remove '"+types.String(policyId)+"'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, policy := range policies {
|
||||||
|
var policyId = int64(policy.Id)
|
||||||
|
storage, ok := this.storageMap[policyId]
|
||||||
|
if ok {
|
||||||
|
// 检查配置是否有变更
|
||||||
|
if types.Int(policy.Version) != storage.Version() {
|
||||||
|
err = storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close policy '"+types.String(policyId)+"' failed: "+err.Error())
|
||||||
|
|
||||||
|
// 继续往下执行
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(policy.Options) > 0 {
|
||||||
|
err = json.Unmarshal([]byte(policy.Options), storage.Config())
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "unmarshal policy '"+types.String(policyId)+"' config failed: "+err.Error())
|
||||||
|
storage.SetOk(false)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
storage.SetVersion(types.Int(policy.Version))
|
||||||
|
err := storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
storage.SetOk(true)
|
||||||
|
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "restart policy '"+types.String(policyId)+"'")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
storage, err := this.createStorage(policy.Type, []byte(policy.Options))
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "create policy '"+types.String(policyId)+"' failed: "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
storage.SetVersion(types.Int(policy.Version))
|
||||||
|
this.storageMap[policyId] = storage
|
||||||
|
err = storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
storage.SetOk(true)
|
||||||
|
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StorageManager) createStorage(storageType string, optionsJSON []byte) (StorageInterface, error) {
|
||||||
|
switch storageType {
|
||||||
|
case serverconfigs.AccessLogStorageTypeFile:
|
||||||
|
var config = &serverconfigs.AccessLogFileStorageConfig{}
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
err := json.Unmarshal(optionsJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewFileStorage(config), nil
|
||||||
|
case serverconfigs.AccessLogStorageTypeES:
|
||||||
|
var config = &serverconfigs.AccessLogESStorageConfig{}
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
err := json.Unmarshal(optionsJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewESStorage(config), nil
|
||||||
|
case serverconfigs.AccessLogStorageTypeTCP:
|
||||||
|
var config = &serverconfigs.AccessLogTCPStorageConfig{}
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
err := json.Unmarshal(optionsJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewTCPStorage(config), nil
|
||||||
|
case serverconfigs.AccessLogStorageTypeSyslog:
|
||||||
|
var config = &serverconfigs.AccessLogSyslogStorageConfig{}
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
err := json.Unmarshal(optionsJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewSyslogStorage(config), nil
|
||||||
|
case serverconfigs.AccessLogStorageTypeCommand:
|
||||||
|
var config = &serverconfigs.AccessLogCommandStorageConfig{}
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
err := json.Unmarshal(optionsJSON, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewCommandStorage(config), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("invalid policy type '" + storageType + "'")
|
||||||
|
}
|
||||||
17
internal/accesslogs/storage_manager_test.go
Normal file
17
internal/accesslogs/storage_manager_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStorageManager_Loop(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var storage = NewStorageManager()
|
||||||
|
err := storage.Loop()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(storage.storageMap)
|
||||||
|
}
|
||||||
137
internal/accesslogs/storage_syslog.go
Normal file
137
internal/accesslogs/storage_syslog.go
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SyslogStorageProtocol = string
|
||||||
|
|
||||||
|
const (
|
||||||
|
SyslogStorageProtocolTCP SyslogStorageProtocol = "tcp"
|
||||||
|
SyslogStorageProtocolUDP SyslogStorageProtocol = "udp"
|
||||||
|
SyslogStorageProtocolNone SyslogStorageProtocol = "none"
|
||||||
|
SyslogStorageProtocolSocket SyslogStorageProtocol = "socket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SyslogStoragePriority = int
|
||||||
|
|
||||||
|
// SyslogStorage syslog存储策略
|
||||||
|
type SyslogStorage struct {
|
||||||
|
BaseStorage
|
||||||
|
|
||||||
|
config *serverconfigs.AccessLogSyslogStorageConfig
|
||||||
|
|
||||||
|
exe string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSyslogStorage(config *serverconfigs.AccessLogSyslogStorageConfig) *SyslogStorage {
|
||||||
|
return &SyslogStorage{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *SyslogStorage) Config() interface{} {
|
||||||
|
return this.config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 开启
|
||||||
|
func (this *SyslogStorage) Start() error {
|
||||||
|
if runtime.GOOS != "linux" {
|
||||||
|
return errors.New("'syslog' storage only works on linux")
|
||||||
|
}
|
||||||
|
|
||||||
|
exe, err := exec.LookPath("logger")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
this.exe = exe
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *SyslogStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
if len(accessLogs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{}
|
||||||
|
if len(this.config.Tag) > 0 {
|
||||||
|
args = append(args, "-t", this.config.Tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
if this.config.Priority >= 0 {
|
||||||
|
args = append(args, "-p", strconv.Itoa(this.config.Priority))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch this.config.Protocol {
|
||||||
|
case SyslogStorageProtocolTCP:
|
||||||
|
args = append(args, "-T")
|
||||||
|
if len(this.config.ServerAddr) > 0 {
|
||||||
|
args = append(args, "-n", this.config.ServerAddr)
|
||||||
|
}
|
||||||
|
if this.config.ServerPort > 0 {
|
||||||
|
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
||||||
|
}
|
||||||
|
case SyslogStorageProtocolUDP:
|
||||||
|
args = append(args, "-d")
|
||||||
|
if len(this.config.ServerAddr) > 0 {
|
||||||
|
args = append(args, "-n", this.config.ServerAddr)
|
||||||
|
}
|
||||||
|
if this.config.ServerPort > 0 {
|
||||||
|
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
||||||
|
}
|
||||||
|
case SyslogStorageProtocolSocket:
|
||||||
|
args = append(args, "-u")
|
||||||
|
args = append(args, this.config.Socket)
|
||||||
|
case SyslogStorageProtocolNone:
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, "-S", "10240")
|
||||||
|
|
||||||
|
cmd := exec.Command(this.exe, args...)
|
||||||
|
w, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
data, err := this.Marshal(accessLog)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, err = w.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write([]byte("\n"))
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = w.Close()
|
||||||
|
err = cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
func (this *SyslogStorage) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
111
internal/accesslogs/storage_tcp.go
Normal file
111
internal/accesslogs/storage_tcp.go
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TCPStorage TCP存储策略
|
||||||
|
type TCPStorage struct {
|
||||||
|
BaseStorage
|
||||||
|
|
||||||
|
config *serverconfigs.AccessLogTCPStorageConfig
|
||||||
|
|
||||||
|
writeLocker sync.Mutex
|
||||||
|
|
||||||
|
connLocker sync.Mutex
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTCPStorage(config *serverconfigs.AccessLogTCPStorageConfig) *TCPStorage {
|
||||||
|
return &TCPStorage{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *TCPStorage) Config() interface{} {
|
||||||
|
return this.config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 开启
|
||||||
|
func (this *TCPStorage) Start() error {
|
||||||
|
if len(this.config.Network) == 0 {
|
||||||
|
return errors.New("'network' should not be empty")
|
||||||
|
}
|
||||||
|
if len(this.config.Addr) == 0 {
|
||||||
|
return errors.New("'addr' should not be empty")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入日志
|
||||||
|
func (this *TCPStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
||||||
|
if len(accessLogs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := this.connect()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := this.conn
|
||||||
|
if conn == nil {
|
||||||
|
return errors.New("connection should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeLocker.Lock()
|
||||||
|
defer this.writeLocker.Unlock()
|
||||||
|
|
||||||
|
for _, accessLog := range accessLogs {
|
||||||
|
data, err := this.Marshal(accessLog)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, err = conn.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
_ = this.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, err = conn.Write([]byte("\n"))
|
||||||
|
if err != nil {
|
||||||
|
_ = this.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭
|
||||||
|
func (this *TCPStorage) Close() error {
|
||||||
|
this.connLocker.Lock()
|
||||||
|
defer this.connLocker.Unlock()
|
||||||
|
|
||||||
|
if this.conn != nil {
|
||||||
|
err := this.conn.Close()
|
||||||
|
this.conn = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *TCPStorage) connect() error {
|
||||||
|
this.connLocker.Lock()
|
||||||
|
defer this.connLocker.Unlock()
|
||||||
|
|
||||||
|
if this.conn != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.Dial(this.config.Network, this.config.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
this.conn = conn
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
72
internal/accesslogs/storage_tcp_test.go
Normal file
72
internal/accesslogs/storage_tcp_test.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package accesslogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTCPStorage_Write(t *testing.T) {
|
||||||
|
go func() {
|
||||||
|
server, err := net.Listen("tcp", "127.0.0.1:9981")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
conn, err := server.Accept()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
t.Log(string(buf[:n]))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_ = server.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
storage := NewTCPStorage(&serverconfigs.AccessLogTCPStorageConfig{
|
||||||
|
Network: "tcp",
|
||||||
|
Addr: "127.0.0.1:9981",
|
||||||
|
})
|
||||||
|
err := storage.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err = storage.Write([]*pb.HTTPAccessLog{
|
||||||
|
{
|
||||||
|
RequestMethod: "POST",
|
||||||
|
RequestPath: "/1",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RequestMethod: "GET",
|
||||||
|
RequestPath: "/2",
|
||||||
|
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
err = storage.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
24
internal/accesslogs/tests/command_storage.php
Normal file
24
internal/accesslogs/tests/command_storage.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// test command storage
|
||||||
|
|
||||||
|
// open access log file
|
||||||
|
$fp = fopen("/tmp/goedge-command-storage.log", "a+");
|
||||||
|
|
||||||
|
// read access logs from stdin
|
||||||
|
$stdin = fopen("php://stdin", "r");
|
||||||
|
while(true) {
|
||||||
|
if (feof($stdin)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$line = fgets($stdin);
|
||||||
|
|
||||||
|
// write to access log file
|
||||||
|
fwrite($fp, $line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// close file pointers
|
||||||
|
fclose($fp);
|
||||||
|
fclose($stdin);
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -2,8 +2,11 @@ package apps
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -11,7 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App命令帮助
|
// AppCmd App命令帮助
|
||||||
type AppCmd struct {
|
type AppCmd struct {
|
||||||
product string
|
product string
|
||||||
version string
|
version string
|
||||||
@@ -20,10 +23,14 @@ type AppCmd struct {
|
|||||||
appendStrings []string
|
appendStrings []string
|
||||||
|
|
||||||
directives []*Directive
|
directives []*Directive
|
||||||
|
|
||||||
|
sock *gosock.Sock
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAppCmd() *AppCmd {
|
func NewAppCmd() *AppCmd {
|
||||||
return &AppCmd{}
|
return &AppCmd{
|
||||||
|
sock: gosock.NewTmpSock(teaconst.ProcessName),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandHelpOption struct {
|
type CommandHelpOption struct {
|
||||||
@@ -31,25 +38,25 @@ type CommandHelpOption struct {
|
|||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
// 产品
|
// Product 产品
|
||||||
func (this *AppCmd) Product(product string) *AppCmd {
|
func (this *AppCmd) Product(product string) *AppCmd {
|
||||||
this.product = product
|
this.product = product
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 版本
|
// Version 版本
|
||||||
func (this *AppCmd) Version(version string) *AppCmd {
|
func (this *AppCmd) Version(version string) *AppCmd {
|
||||||
this.version = version
|
this.version = version
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用方法
|
// Usage 使用方法
|
||||||
func (this *AppCmd) Usage(usage string) *AppCmd {
|
func (this *AppCmd) Usage(usage string) *AppCmd {
|
||||||
this.usage = usage
|
this.usage = usage
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 选项
|
// Option 选项
|
||||||
func (this *AppCmd) Option(code string, description string) *AppCmd {
|
func (this *AppCmd) Option(code string, description string) *AppCmd {
|
||||||
this.options = append(this.options, &CommandHelpOption{
|
this.options = append(this.options, &CommandHelpOption{
|
||||||
Code: code,
|
Code: code,
|
||||||
@@ -58,13 +65,13 @@ func (this *AppCmd) Option(code string, description string) *AppCmd {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 附加内容
|
// Append 附加内容
|
||||||
func (this *AppCmd) Append(appendString string) *AppCmd {
|
func (this *AppCmd) Append(appendString string) *AppCmd {
|
||||||
this.appendStrings = append(this.appendStrings, appendString)
|
this.appendStrings = append(this.appendStrings, appendString)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印
|
// Print 打印
|
||||||
func (this *AppCmd) Print() {
|
func (this *AppCmd) Print() {
|
||||||
fmt.Println(this.product + " v" + this.version)
|
fmt.Println(this.product + " v" + this.version)
|
||||||
|
|
||||||
@@ -103,7 +110,7 @@ func (this *AppCmd) Print() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加指令
|
// On 添加指令
|
||||||
func (this *AppCmd) On(arg string, callback func()) {
|
func (this *AppCmd) On(arg string, callback func()) {
|
||||||
this.directives = append(this.directives, &Directive{
|
this.directives = append(this.directives, &Directive{
|
||||||
Arg: arg,
|
Arg: arg,
|
||||||
@@ -111,7 +118,7 @@ func (this *AppCmd) On(arg string, callback func()) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 运行
|
// Run 运行
|
||||||
func (this *AppCmd) Run(main func()) {
|
func (this *AppCmd) Run(main func()) {
|
||||||
// 获取参数
|
// 获取参数
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
@@ -150,9 +157,6 @@ func (this *AppCmd) Run(main func()) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录PID
|
|
||||||
_ = this.writePid()
|
|
||||||
|
|
||||||
// 日志
|
// 日志
|
||||||
writer := new(LogWriter)
|
writer := new(LogWriter)
|
||||||
writer.Init()
|
writer.Init()
|
||||||
@@ -164,7 +168,7 @@ func (this *AppCmd) Run(main func()) {
|
|||||||
|
|
||||||
// 版本号
|
// 版本号
|
||||||
func (this *AppCmd) runVersion() {
|
func (this *AppCmd) runVersion() {
|
||||||
fmt.Println(this.product+" v"+this.version, "(build: "+runtime.Version(), runtime.GOOS, runtime.GOARCH+")")
|
fmt.Println(this.product+" v"+this.version, "(build: "+runtime.Version(), runtime.GOOS, runtime.GOARCH, teaconst.Tag+")")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 帮助
|
// 帮助
|
||||||
@@ -174,9 +178,9 @@ func (this *AppCmd) runHelp() {
|
|||||||
|
|
||||||
// 启动
|
// 启动
|
||||||
func (this *AppCmd) runStart() {
|
func (this *AppCmd) runStart() {
|
||||||
proc := this.checkPid()
|
var pid = this.getPID()
|
||||||
if proc != nil {
|
if pid > 0 {
|
||||||
fmt.Println(this.product+" already started, pid:", proc.Pid)
|
fmt.Println(this.product+" already started, pid:", pid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,18 +196,15 @@ func (this *AppCmd) runStart() {
|
|||||||
|
|
||||||
// 停止
|
// 停止
|
||||||
func (this *AppCmd) runStop() {
|
func (this *AppCmd) runStop() {
|
||||||
proc := this.checkPid()
|
var pid = this.getPID()
|
||||||
if proc == nil {
|
if pid == 0 {
|
||||||
fmt.Println(this.product + " not started yet")
|
fmt.Println(this.product + " not started yet")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 停止进程
|
_, _ = this.sock.Send(&gosock.Command{Code: "stop"})
|
||||||
_ = proc.Kill()
|
|
||||||
|
|
||||||
// 在Windows上经常不能及时释放资源
|
fmt.Println(this.product+" stopped ok, pid:", types.String(pid))
|
||||||
_ = DeletePid(Tea.Root + "/bin/pid")
|
|
||||||
fmt.Println(this.product+" stopped ok, pid:", proc.Pid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重启
|
// 重启
|
||||||
@@ -215,20 +216,24 @@ func (this *AppCmd) runRestart() {
|
|||||||
|
|
||||||
// 状态
|
// 状态
|
||||||
func (this *AppCmd) runStatus() {
|
func (this *AppCmd) runStatus() {
|
||||||
proc := this.checkPid()
|
var pid = this.getPID()
|
||||||
if proc == nil {
|
if pid == 0 {
|
||||||
fmt.Println(this.product + " not started yet")
|
fmt.Println(this.product + " not started yet")
|
||||||
} else {
|
return
|
||||||
fmt.Println(this.product + " is running, pid: " + fmt.Sprintf("%d", proc.Pid))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(this.product + " is running, pid: " + types.String(pid))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查PID
|
// 获取当前的PID
|
||||||
func (this *AppCmd) checkPid() *os.Process {
|
func (this *AppCmd) getPID() int {
|
||||||
return CheckPid(Tea.Root + "/bin/pid")
|
if !this.sock.IsListening() {
|
||||||
}
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
// 写入PID
|
reply, err := this.sock.Send(&gosock.Command{Code: "pid"})
|
||||||
func (this *AppCmd) writePid() error {
|
if err != nil {
|
||||||
return WritePid(Tea.Root + "/bin/pid")
|
return 0
|
||||||
|
}
|
||||||
|
return maps.NewMap(reply.Params).GetInt("pid")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
// +build !windows
|
|
||||||
|
|
||||||
package apps
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
// lock file
|
|
||||||
func LockFile(fp *os.File) error {
|
|
||||||
return syscall.Flock(int(fp.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
|
||||||
}
|
|
||||||
|
|
||||||
func UnlockFile(fp *os.File) error {
|
|
||||||
return syscall.Flock(int(fp.Fd()), syscall.LOCK_UN)
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
// +build windows
|
|
||||||
|
|
||||||
package apps
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// lock file
|
|
||||||
func LockFile(fp *os.File) error {
|
|
||||||
return errors.New("not implemented on windows")
|
|
||||||
}
|
|
||||||
|
|
||||||
func UnlockFile(fp *os.File) error {
|
|
||||||
return errors.New("not implemented on windows")
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
package apps
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
var pidFileList = []*os.File{}
|
|
||||||
|
|
||||||
// 检查Pid
|
|
||||||
func CheckPid(path string) *os.Process {
|
|
||||||
// windows上打开的文件是不能删除的
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
if os.Remove(path) == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
_ = file.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 是否能取得Lock
|
|
||||||
err = LockFile(file)
|
|
||||||
if err == nil {
|
|
||||||
_ = UnlockFile(file)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pidBytes, err := ioutil.ReadAll(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
pid := types.Int(string(pidBytes))
|
|
||||||
|
|
||||||
if pid <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
proc, _ := os.FindProcess(pid)
|
|
||||||
return proc
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入Pid
|
|
||||||
func WritePid(path string) error {
|
|
||||||
fp, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_RDONLY, 0666)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
err = LockFile(fp)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pidFileList = append(pidFileList, fp) // hold the file pointers
|
|
||||||
|
|
||||||
_, err = fp.WriteString(fmt.Sprintf("%d", os.Getpid()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入Ppid
|
|
||||||
func WritePpid(path string) error {
|
|
||||||
fp, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_RDONLY, 0666)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
err = LockFile(fp)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pidFileList = append(pidFileList, fp) // hold the file pointers
|
|
||||||
|
|
||||||
_, err = fp.WriteString(fmt.Sprintf("%d", os.Getppid()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除Pid
|
|
||||||
func DeletePid(path string) error {
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fp := range pidFileList {
|
|
||||||
_ = UnlockFile(fp)
|
|
||||||
_ = fp.Close()
|
|
||||||
}
|
|
||||||
return os.Remove(path)
|
|
||||||
}
|
|
||||||
8
internal/const/build.go
Normal file
8
internal/const/build.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
// +build community
|
||||||
|
|
||||||
|
package teaconst
|
||||||
|
|
||||||
|
const BuildCommunity = true
|
||||||
|
const BuildPlus = false
|
||||||
|
const Tag = "community"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.2.5"
|
Version = "0.2.9"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,9 +18,9 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.2.5"
|
NodeVersion = "0.2.8"
|
||||||
UserNodeVersion = "0.0.10"
|
UserNodeVersion = "0.0.10"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.2"
|
MonitorNodeVersion = "0.0.2"
|
||||||
DNSNodeVersion = "0.0.2"
|
DNSNodeVersion = "0.1.0"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func TestDB_Instance(t *testing.T) {
|
|||||||
if err == driver.ErrBadConn {
|
if err == driver.ErrBadConn {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.Fatal(i, "exec:", err)
|
t.Error(i, "exec:", err)
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ var accessLogLocker = &sync.RWMutex{}
|
|||||||
type httpAccessLogDefinition struct {
|
type httpAccessLogDefinition struct {
|
||||||
Name string
|
Name string
|
||||||
HasRemoteAddr bool
|
HasRemoteAddr bool
|
||||||
|
HasDomain bool
|
||||||
Exists bool
|
Exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +83,7 @@ func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查表格是否存在
|
// 检查表格是否存在
|
||||||
func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, hasRemoteAddr bool, ok bool, err error) {
|
func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, hasRemoteAddr bool, hasDomain bool, ok bool, err error) {
|
||||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
||||||
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
|
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
|
||||||
return
|
return
|
||||||
@@ -90,7 +91,7 @@ func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, hasRe
|
|||||||
|
|
||||||
config, err := db.Config()
|
config, err := db.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", false, false, err
|
return "", false, false, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName = "edgeHTTPAccessLogs_" + day
|
tableName = "edgeHTTPAccessLogs_" + day
|
||||||
@@ -100,15 +101,15 @@ func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, hasRe
|
|||||||
def, ok := httpAccessLogTableMapping[cacheKey]
|
def, ok := httpAccessLogTableMapping[cacheKey]
|
||||||
accessLogLocker.RUnlock()
|
accessLogLocker.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return tableName, def.HasRemoteAddr, true, nil
|
return tableName, def.HasRemoteAddr, def.HasDomain, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
def, err = findHTTPAccessLogTable(db, day, false)
|
def, err = findHTTPAccessLogTable(db, day, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tableName, false, false, err
|
return tableName, false, false, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tableName, def.HasRemoteAddr, def.Exists, nil
|
return tableName, def.HasRemoteAddr, def.HasDomain, def.Exists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
|
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
|
||||||
@@ -174,6 +175,7 @@ func findHTTPAccessLogTable(db *dbs.DB, day string, force bool) (*httpAccessLogD
|
|||||||
var definition = &httpAccessLogDefinition{
|
var definition = &httpAccessLogDefinition{
|
||||||
Name: tableName,
|
Name: tableName,
|
||||||
HasRemoteAddr: table.FindFieldWithName("remoteAddr") != nil,
|
HasRemoteAddr: table.FindFieldWithName("remoteAddr") != nil,
|
||||||
|
HasDomain: table.FindFieldWithName("domain") != nil,
|
||||||
Exists: true,
|
Exists: true,
|
||||||
}
|
}
|
||||||
httpAccessLogTableMapping[cacheKey] = definition
|
httpAccessLogTableMapping[cacheKey] = definition
|
||||||
@@ -182,11 +184,16 @@ func findHTTPAccessLogTable(db *dbs.DB, day string, force bool) (*httpAccessLogD
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !force {
|
if !force {
|
||||||
return &httpAccessLogDefinition{Name: tableName, HasRemoteAddr: true, Exists: false}, nil
|
return &httpAccessLogDefinition{
|
||||||
|
Name: tableName,
|
||||||
|
HasRemoteAddr: true,
|
||||||
|
HasDomain: true,
|
||||||
|
Exists: false,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建表格
|
// 创建表格
|
||||||
_, err = db.Exec("CREATE TABLE `" + tableName + "` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',`serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',`nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',`status` int(3) unsigned DEFAULT '0' COMMENT '状态码',`createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',`content` json DEFAULT NULL COMMENT '日志内容',`requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',`firewallPolicyId` int(11) unsigned DEFAULT '0' COMMENT 'WAF策略ID',`firewallRuleGroupId` int(11) unsigned DEFAULT '0' COMMENT 'WAF分组ID',`firewallRuleSetId` int(11) unsigned DEFAULT '0' COMMENT 'WAF集ID',`firewallRuleId` int(11) unsigned DEFAULT '0' COMMENT 'WAF规则ID',`remoteAddr` varchar(64) DEFAULT NULL COMMENT 'IP地址',PRIMARY KEY (`id`),KEY `serverId` (`serverId`),KEY `nodeId` (`nodeId`),KEY `serverId_status` (`serverId`,`status`),KEY `requestId` (`requestId`),KEY `firewallPolicyId` (`firewallPolicyId`),KEY `firewallRuleGroupId` (`firewallRuleGroupId`),KEY `firewallRuleSetId` (`firewallRuleSetId`), KEY `firewallRuleId` (`firewallRuleId`), KEY `remoteAddr` (`remoteAddr`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='访问日志';")
|
_, err = db.Exec("CREATE TABLE `" + tableName + "` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',`serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',`nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',`status` int(3) unsigned DEFAULT '0' COMMENT '状态码',`createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间', `content` json DEFAULT NULL COMMENT '日志内容', `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID', `firewallPolicyId` int(11) unsigned DEFAULT '0' COMMENT 'WAF策略ID', `firewallRuleGroupId` int(11) unsigned DEFAULT '0' COMMENT 'WAF分组ID', `firewallRuleSetId` int(11) unsigned DEFAULT '0' COMMENT 'WAF集ID', `firewallRuleId` int(11) unsigned DEFAULT '0' COMMENT 'WAF规则ID', `remoteAddr` varchar(64) DEFAULT NULL COMMENT 'IP地址', `domain` varchar(128) DEFAULT NULL COMMENT '域名', PRIMARY KEY (`id`), KEY `serverId` (`serverId`), KEY `nodeId` (`nodeId`), KEY `serverId_status` (`serverId`,`status`), KEY `requestId` (`requestId`), KEY `firewallPolicyId` (`firewallPolicyId`), KEY `firewallRuleGroupId` (`firewallRuleGroupId`), KEY `firewallRuleSetId` (`firewallRuleSetId`), KEY `firewallRuleId` (`firewallRuleId`), KEY `remoteAddr` (`remoteAddr`), KEY `domain` (`domain`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='访问日志';")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -234,7 +241,7 @@ func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建表格
|
// 创建表格
|
||||||
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
|
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `remoteAddr` varchar(128) DEFAULT NULL COMMENT 'IP',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`),\n KEY `remoteAddr` (`remoteAddr`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tableName, err
|
return tableName, err
|
||||||
}
|
}
|
||||||
@@ -346,7 +353,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
||||||
}
|
}
|
||||||
@@ -390,7 +397,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
||||||
|
|
||||||
// 创建节点日志
|
// 创建节点日志
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||||
if createLogErr != nil {
|
if createLogErr != nil {
|
||||||
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,6 +218,8 @@ func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
|
|||||||
|
|
||||||
// ExistDomainRecord 检查域名解析记录是否存在
|
// ExistDomainRecord 检查域名解析记录是否存在
|
||||||
func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
|
func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
|
||||||
|
recordType = strings.ToUpper(recordType)
|
||||||
|
|
||||||
query := maps.Map{
|
query := maps.Map{
|
||||||
"name": recordName,
|
"name": recordName,
|
||||||
"type": recordType,
|
"type": recordType,
|
||||||
@@ -239,7 +241,6 @@ func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordNa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
recordType = strings.ToUpper(recordType)
|
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(domainId).
|
Pk(domainId).
|
||||||
Where("JSON_CONTAINS(records, :query)").
|
Where("JSON_CONTAINS(records, :query)").
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -88,7 +90,10 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
|
|||||||
|
|
||||||
// TODO 根据集群、服务设置获取IP
|
// TODO 根据集群、服务设置获取IP
|
||||||
if tableDef.HasRemoteAddr {
|
if tableDef.HasRemoteAddr {
|
||||||
fields["remoteAddr"] = accessLog.RawRemoteAddr
|
fields["remoteAddr"] = accessLog.RemoteAddr
|
||||||
|
}
|
||||||
|
if tableDef.HasDomain {
|
||||||
|
fields["domain"] = accessLog.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := json.Marshal(accessLog)
|
content, err := json.Marshal(accessLog)
|
||||||
@@ -125,7 +130,20 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListAccessLogs 读取往前的 单页访问日志
|
// ListAccessLogs 读取往前的 单页访问日志
|
||||||
func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string) (result []*HTTPAccessLog, nextLastRequestId string, hasMore bool, err error) {
|
func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string,
|
||||||
|
size int64,
|
||||||
|
day string,
|
||||||
|
serverId int64,
|
||||||
|
reverse bool,
|
||||||
|
hasError bool,
|
||||||
|
firewallPolicyId int64,
|
||||||
|
firewallRuleGroupId int64,
|
||||||
|
firewallRuleSetId int64,
|
||||||
|
hasFirewallPolicy bool,
|
||||||
|
userId int64,
|
||||||
|
keyword string,
|
||||||
|
ip string,
|
||||||
|
domain string) (result []*HTTPAccessLog, nextLastRequestId string, hasMore bool, err error) {
|
||||||
if len(day) != 8 {
|
if len(day) != 8 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -135,18 +153,18 @@ func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
size = 1000
|
size = 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword)
|
result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain)
|
||||||
if err != nil || int64(len(result)) < size {
|
if err != nil || int64(len(result)) < size {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword)
|
moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, serverId, reverse, hasError, firewallPolicyId, firewallRuleGroupId, firewallRuleSetId, hasFirewallPolicy, userId, keyword, ip, domain)
|
||||||
hasMore = len(moreResult) > 0
|
hasMore = len(moreResult) > 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取往前的单页访问日志
|
// 读取往前的单页访问日志
|
||||||
func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string) (result []*HTTPAccessLog, nextLastRequestId string, err error) {
|
func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64, keyword string, ip string, domain string) (result []*HTTPAccessLog, nextLastRequestId string, err error) {
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
return nil, lastRequestId, nil
|
return nil, lastRequestId, nil
|
||||||
}
|
}
|
||||||
@@ -187,7 +205,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
|
|
||||||
dao := daoWrapper.DAO
|
dao := daoWrapper.DAO
|
||||||
|
|
||||||
tableName, hasRemoteAddr, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
|
tableName, hasRemoteAddrField, hasDomainField, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
|
||||||
if !exists {
|
if !exists {
|
||||||
// 表格不存在则跳过
|
// 表格不存在则跳过
|
||||||
return
|
return
|
||||||
@@ -223,22 +241,60 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keyword
|
// keyword
|
||||||
|
if len(ip) > 0 {
|
||||||
|
// TODO 支持IP范围
|
||||||
|
if hasRemoteAddrField {
|
||||||
|
// IP格式
|
||||||
|
if strings.Contains(ip, ",") || strings.Contains(ip, "-") {
|
||||||
|
rangeConfig, err := shared.ParseIPRange(ip)
|
||||||
|
if err == nil {
|
||||||
|
if len(rangeConfig.IPFrom) > 0 && len(rangeConfig.IPTo) > 0 {
|
||||||
|
query.Between("INET_ATON(remoteAddr)", utils.IP2Long(rangeConfig.IPFrom), utils.IP2Long(rangeConfig.IPTo))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
query.Attr("remoteAddr", ip)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
query.Where("JSON_EXTRACT(content, '$.remoteAddr')=:ip1").
|
||||||
|
Param("ip1", ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(domain) > 0 {
|
||||||
|
if hasDomainField {
|
||||||
|
if strings.Contains(domain, "*") {
|
||||||
|
domain = strings.ReplaceAll(domain, "*", "%")
|
||||||
|
domain = regexp.MustCompile(`[^a-zA-Z0-9-.%]`).ReplaceAllString(domain, "")
|
||||||
|
query.Where("domain LIKE :host2").
|
||||||
|
Param("host2", domain)
|
||||||
|
} else {
|
||||||
|
query.Attr("domain", domain)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
query.Where("JSON_EXTRACT(content, '$.host')=:host1").
|
||||||
|
Param("host1", domain)
|
||||||
|
}
|
||||||
|
}
|
||||||
if len(keyword) > 0 {
|
if len(keyword) > 0 {
|
||||||
// remoteAddr
|
// remoteAddr
|
||||||
if hasRemoteAddr && net.ParseIP(keyword) != nil {
|
if hasRemoteAddrField && net.ParseIP(keyword) != nil {
|
||||||
query.Attr("remoteAddr", keyword)
|
query.Attr("remoteAddr", keyword)
|
||||||
} else if hasRemoteAddr && regexp.MustCompile(`^ip:.+`).MatchString(keyword) {
|
} else if hasRemoteAddrField && regexp.MustCompile(`^ip:.+`).MatchString(keyword) {
|
||||||
keyword = keyword[3:]
|
keyword = keyword[3:]
|
||||||
pieces := strings.SplitN(keyword, ",", 2)
|
pieces := strings.SplitN(keyword, ",", 2)
|
||||||
if len(pieces) == 1 || len(pieces[1]) == 0 {
|
if len(pieces) == 1 || len(pieces[1]) == 0 {
|
||||||
query.Attr("remoteAddr", pieces[0])
|
query.Attr("remoteAddr", pieces[0])
|
||||||
} else {
|
} else {
|
||||||
query.Between("remoteAddr", pieces[0], pieces[1])
|
query.Between("INET_ATON(remoteAddr)", utils.IP2Long(pieces[0]), utils.IP2Long(pieces[1]))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if regexp.MustCompile(`^ip:.+`).MatchString(keyword) {
|
||||||
|
keyword = keyword[3:]
|
||||||
|
}
|
||||||
|
|
||||||
useOriginKeyword := false
|
useOriginKeyword := false
|
||||||
|
|
||||||
where := "JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.requestURI') LIKE :keyword OR JSON_EXTRACT(content, '$.host') LIKE :keyword"
|
where := "JSON_EXTRACT(content, '$.remoteAddr') LIKE :keyword OR JSON_EXTRACT(content, '$.requestURI') LIKE :keyword OR JSON_EXTRACT(content, '$.host') LIKE :keyword OR JSON_EXTRACT(content, '$.userAgent') LIKE :keyword"
|
||||||
|
|
||||||
jsonKeyword, err := json.Marshal(keyword)
|
jsonKeyword, err := json.Marshal(keyword)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -377,7 +433,7 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s
|
|||||||
|
|
||||||
dao := daoWrapper.DAO
|
dao := daoWrapper.DAO
|
||||||
|
|
||||||
tableName, _, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
|
tableName, _, _, exists, err := findHTTPAccessLogTableName(dao.Instance, day)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Println("[DB_NODE]" + err.Error())
|
logs.Println("[DB_NODE]" + err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "")
|
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "", "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page(t *testing.T) {
|
|||||||
times := 0 // 防止循环次数太多
|
times := 0 // 防止循环次数太多
|
||||||
for {
|
for {
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "")
|
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0, "", "", "")
|
||||||
cost := time.Since(before).Seconds()
|
cost := time.Since(before).Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -99,7 +99,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Reverse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0, "")
|
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0, "", "", "")
|
||||||
cost := time.Since(before).Seconds()
|
cost := time.Since(before).Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -124,7 +124,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page_NotExists(t *testing.T) {
|
|||||||
times := 0 // 防止循环次数太多
|
times := 0 // 防止循环次数太多
|
||||||
for {
|
for {
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0, "")
|
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0, "", "", "")
|
||||||
cost := time.Since(before).Seconds()
|
cost := time.Since(before).Seconds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type HTTPAccessLog struct {
|
|||||||
FirewallRuleSetId uint32 `field:"firewallRuleSetId"` // WAF集ID
|
FirewallRuleSetId uint32 `field:"firewallRuleSetId"` // WAF集ID
|
||||||
FirewallRuleId uint32 `field:"firewallRuleId"` // WAF规则ID
|
FirewallRuleId uint32 `field:"firewallRuleId"` // WAF规则ID
|
||||||
RemoteAddr string `field:"remoteAddr"` // IP地址
|
RemoteAddr string `field:"remoteAddr"` // IP地址
|
||||||
|
Domain string `field:"domain"` // 域名
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPAccessLogOperator struct {
|
type HTTPAccessLogOperator struct {
|
||||||
@@ -29,6 +30,7 @@ type HTTPAccessLogOperator struct {
|
|||||||
FirewallRuleSetId interface{} // WAF集ID
|
FirewallRuleSetId interface{} // WAF集ID
|
||||||
FirewallRuleId interface{} // WAF规则ID
|
FirewallRuleId interface{} // WAF规则ID
|
||||||
RemoteAddr interface{} // IP地址
|
RemoteAddr interface{} // IP地址
|
||||||
|
Domain interface{} // 域名
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogOperator() *HTTPAccessLogOperator {
|
func NewHTTPAccessLogOperator() *HTTPAccessLogOperator {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -35,12 +36,12 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// Init 初始化
|
||||||
func (this *HTTPAccessLogPolicyDAO) Init() {
|
func (this *HTTPAccessLogPolicyDAO) Init() {
|
||||||
_ = this.DAOObject.Init()
|
_ = this.DAOObject.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableHTTPAccessLogPolicy 启用条目
|
||||||
func (this *HTTPAccessLogPolicyDAO) EnableHTTPAccessLogPolicy(tx *dbs.Tx, id int64) error {
|
func (this *HTTPAccessLogPolicyDAO) EnableHTTPAccessLogPolicy(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -49,7 +50,7 @@ func (this *HTTPAccessLogPolicyDAO) EnableHTTPAccessLogPolicy(tx *dbs.Tx, id int
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableHTTPAccessLogPolicy 禁用条目
|
||||||
func (this *HTTPAccessLogPolicyDAO) DisableHTTPAccessLogPolicy(tx *dbs.Tx, id int64) error {
|
func (this *HTTPAccessLogPolicyDAO) DisableHTTPAccessLogPolicy(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -58,7 +59,7 @@ func (this *HTTPAccessLogPolicyDAO) DisableHTTPAccessLogPolicy(tx *dbs.Tx, id in
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledHTTPAccessLogPolicy 查找启用中的条目
|
||||||
func (this *HTTPAccessLogPolicyDAO) FindEnabledHTTPAccessLogPolicy(tx *dbs.Tx, id int64) (*HTTPAccessLogPolicy, error) {
|
func (this *HTTPAccessLogPolicyDAO) FindEnabledHTTPAccessLogPolicy(tx *dbs.Tx, id int64) (*HTTPAccessLogPolicy, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -70,7 +71,7 @@ func (this *HTTPAccessLogPolicyDAO) FindEnabledHTTPAccessLogPolicy(tx *dbs.Tx, i
|
|||||||
return result.(*HTTPAccessLogPolicy), err
|
return result.(*HTTPAccessLogPolicy), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindHTTPAccessLogPolicyName 根据主键查找名称
|
||||||
func (this *HTTPAccessLogPolicyDAO) FindHTTPAccessLogPolicyName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *HTTPAccessLogPolicyDAO) FindHTTPAccessLogPolicyName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -78,51 +79,116 @@ func (this *HTTPAccessLogPolicyDAO) FindHTTPAccessLogPolicyName(tx *dbs.Tx, id i
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找所有可用策略信息
|
// CountAllEnabledPolicies 计算策略数量
|
||||||
func (this *HTTPAccessLogPolicyDAO) FindAllEnabledAccessLogPolicies(tx *dbs.Tx) (result []*HTTPAccessLogPolicy, err error) {
|
func (this *HTTPAccessLogPolicyDAO) CountAllEnabledPolicies(tx *dbs.Tx) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListEnabledPolicies 查找所有可用策略信息
|
||||||
|
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(HTTPAccessLogPolicyStateEnabled).
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
DescPk().
|
DescPk().
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组合配置
|
// FindAllEnabledAndOnPolicies 获取所有的策略信息
|
||||||
func (this *HTTPAccessLogPolicyDAO) ComposeAccessLogPolicyConfig(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPAccessLogStoragePolicy, error) {
|
func (this *HTTPAccessLogPolicyDAO) FindAllEnabledAndOnPolicies(tx *dbs.Tx) (result []*HTTPAccessLogPolicy, err error) {
|
||||||
policy, err := this.FindEnabledHTTPAccessLogPolicy(tx, policyId)
|
_, err = this.Query(tx).
|
||||||
if err != nil {
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
return nil, err
|
Attr("isOn", true).
|
||||||
}
|
Slice(&result).
|
||||||
if policy == nil {
|
FindAll()
|
||||||
return nil, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &serverconfigs.HTTPAccessLogStoragePolicy{}
|
// CreatePolicy 创建策略
|
||||||
config.Id = int64(policy.Id)
|
func (this *HTTPAccessLogPolicyDAO) CreatePolicy(tx *dbs.Tx, name string, policyType string, optionsJSON []byte, condsJSON []byte, isPublic bool) (policyId int64, err error) {
|
||||||
config.IsOn = policy.IsOn == 1
|
var op = NewHTTPAccessLogPolicyOperator()
|
||||||
config.Name = policy.Name
|
op.Name = name
|
||||||
config.Type = policy.Type
|
op.Type = policyType
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
// 选项
|
op.Options = optionsJSON
|
||||||
if IsNotNull(policy.Options) {
|
}
|
||||||
m := map[string]interface{}{}
|
if len(condsJSON) > 0 {
|
||||||
err = json.Unmarshal([]byte(policy.Options), &m)
|
op.Conds = condsJSON
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
op.IsPublic = isPublic
|
||||||
}
|
op.IsOn = true
|
||||||
config.Options = m
|
op.State = HTTPAccessLogPolicyStateEnabled
|
||||||
}
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
// 条件
|
|
||||||
if IsNotNull(policy.Conds) {
|
// UpdatePolicy 修改策略
|
||||||
condsConfig := &shared.HTTPRequestCondsConfig{}
|
func (this *HTTPAccessLogPolicyDAO) UpdatePolicy(tx *dbs.Tx, policyId int64, name string, optionsJSON []byte, condsJSON []byte, isPublic bool, isOn bool) error {
|
||||||
err = json.Unmarshal([]byte(policy.Conds), condsConfig)
|
if policyId <= 0 {
|
||||||
if err != nil {
|
return errors.New("invalid policyId")
|
||||||
return nil, err
|
}
|
||||||
}
|
|
||||||
config.Conds = condsConfig
|
oldOne, err := this.Query(tx).
|
||||||
}
|
Pk(policyId).
|
||||||
|
Find()
|
||||||
return config, nil
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if oldOne == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var oldPolicy = oldOne.(*HTTPAccessLogPolicy)
|
||||||
|
|
||||||
|
var op = NewHTTPAccessLogPolicyOperator()
|
||||||
|
op.Id = policyId
|
||||||
|
op.Name = name
|
||||||
|
if len(optionsJSON) > 0 {
|
||||||
|
op.Options = optionsJSON
|
||||||
|
} else {
|
||||||
|
op.Options = "{}"
|
||||||
|
}
|
||||||
|
if len(condsJSON) > 0 {
|
||||||
|
op.Conds = condsJSON
|
||||||
|
} else {
|
||||||
|
op.Conds = "{}"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 版本号
|
||||||
|
if len(oldPolicy.Options) == 0 || len(optionsJSON) == 0 {
|
||||||
|
op.Version = dbs.SQL("version+1")
|
||||||
|
} else {
|
||||||
|
var m1 = maps.Map{}
|
||||||
|
_ = json.Unmarshal([]byte(oldPolicy.Options), &m1)
|
||||||
|
|
||||||
|
var m2 = maps.Map{}
|
||||||
|
_ = json.Unmarshal(optionsJSON, &m2)
|
||||||
|
|
||||||
|
if bytes.Compare(m1.AsJSON(), m2.AsJSON()) != 0 {
|
||||||
|
op.Version = dbs.SQL("version+1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op.IsPublic = isPublic
|
||||||
|
op.IsOn = isOn
|
||||||
|
return this.Save(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelAllPublicPolicies 取消别的公用的策略
|
||||||
|
func (this *HTTPAccessLogPolicyDAO) CancelAllPublicPolicies(tx *dbs.Tx) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
|
Set("isPublic", 0).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindCurrentPublicPolicyId 取得当前的公用策略
|
||||||
|
func (this *HTTPAccessLogPolicyDAO) FindCurrentPublicPolicyId(tx *dbs.Tx) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
|
Attr("isPublic", 1).
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 访问日志策略
|
// HTTPAccessLogPolicy 访问日志策略
|
||||||
type HTTPAccessLogPolicy struct {
|
type HTTPAccessLogPolicy struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
TemplateId uint32 `field:"templateId"` // 模版ID
|
TemplateId uint32 `field:"templateId"` // 模版ID
|
||||||
@@ -13,6 +13,8 @@ type HTTPAccessLogPolicy struct {
|
|||||||
Type string `field:"type"` // 存储类型
|
Type string `field:"type"` // 存储类型
|
||||||
Options string `field:"options"` // 存储选项
|
Options string `field:"options"` // 存储选项
|
||||||
Conds string `field:"conds"` // 请求条件
|
Conds string `field:"conds"` // 请求条件
|
||||||
|
IsPublic uint8 `field:"isPublic"` // 是否为公用
|
||||||
|
Version uint32 `field:"version"` // 版本号
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPAccessLogPolicyOperator struct {
|
type HTTPAccessLogPolicyOperator struct {
|
||||||
@@ -27,6 +29,8 @@ type HTTPAccessLogPolicyOperator struct {
|
|||||||
Type interface{} // 存储类型
|
Type interface{} // 存储类型
|
||||||
Options interface{} // 存储选项
|
Options interface{} // 存储选项
|
||||||
Conds interface{} // 请求条件
|
Conds interface{} // 请求条件
|
||||||
|
IsPublic interface{} // 是否为公用
|
||||||
|
Version interface{} // 版本号
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {
|
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -275,6 +276,17 @@ func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId in
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledLocationIdWithReverseProxyId 查找包含某个反向代理的Server
|
||||||
|
func (this *HTTPLocationDAO) FindEnabledLocationIdWithReverseProxyId(tx *dbs.Tx, reverseProxyId int64) (serverId int64, err error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(ServerStateEnabled).
|
||||||
|
Where("JSON_CONTAINS(reverseProxy, :jsonQuery)").
|
||||||
|
Param("jsonQuery", maps.Map{"reverseProxyId": reverseProxyId}.AsJSON()).
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPLocationDAO) NotifyUpdate(tx *dbs.Tx, locationId int64) error {
|
func (this *HTTPLocationDAO) NotifyUpdate(tx *dbs.Tx, locationId int64) error {
|
||||||
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithLocationId(tx, locationId)
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithLocationId(tx, locationId)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -239,6 +240,20 @@ func (this *IPItemDAO) FindEnabledItemContainsIP(tx *dbs.Tx, listId int64, ip ui
|
|||||||
return one.(*IPItem), nil
|
return one.(*IPItem), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledItemsWithIP 根据IP查找Item
|
||||||
|
func (this *IPItemDAO) FindEnabledItemsWithIP(tx *dbs.Tx, ip string) (result []*IPItem, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("ipFrom", ip).
|
||||||
|
Attr("ipTo", "").
|
||||||
|
Where("(expiredAt=0 OR expiredAt>:nowTime)").
|
||||||
|
Param("nowTime", time.Now().Unix()).
|
||||||
|
Where("listId IN (SELECT id FROM " + SharedIPListDAO.Table + " WHERE state=1)").
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// ExistsEnabledItem 检查IP是否存在
|
// ExistsEnabledItem 检查IP是否存在
|
||||||
func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error) {
|
func (this *IPItemDAO) ExistsEnabledItem(tx *dbs.Tx, itemId int64) (bool, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -302,7 +317,7 @@ func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
|
|||||||
|
|
||||||
if len(resultClusterIds) > 0 {
|
if len(resultClusterIds) > 0 {
|
||||||
for _, clusterId := range resultClusterIds {
|
for _, clusterId := range resultClusterIds {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeIPItemChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeIPItemChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -253,7 +254,7 @@ func (this *IPListDAO) NotifyUpdate(tx *dbs.Tx, listId int64, taskType NodeTaskT
|
|||||||
|
|
||||||
if len(resultClusterIds) > 0 {
|
if len(resultClusterIds) > 0 {
|
||||||
for _, clusterId := range resultClusterIds {
|
for _, clusterId := range resultClusterIds {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, taskType)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, taskType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -28,8 +29,8 @@ const (
|
|||||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
|
||||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
|
||||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
|
||||||
MessageTypeNodeInactive MessageType = "NodeInactive" // 节点不活跃
|
MessageTypeNodeInactive MessageType = "NodeInactive" // 边缘节点不活跃
|
||||||
MessageTypeNodeActive MessageType = "NodeActive" // 节点活跃
|
MessageTypeNodeActive MessageType = "NodeActive" // 边缘节点活跃
|
||||||
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed" // DNS同步失败
|
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed" // DNS同步失败
|
||||||
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
||||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||||
@@ -39,6 +40,9 @@ const (
|
|||||||
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
||||||
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
MessageTypeThresholdSatisfied MessageType = "ThresholdSatisfied" // 满足阈值
|
||||||
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
MessageTypeFirewallEvent MessageType = "FirewallEvent" // 防火墙事件
|
||||||
|
|
||||||
|
MessageTypeNSNodeInactive MessageType = "NSNodeInactive" // 边缘节点不活跃
|
||||||
|
MessageTypeNSNodeActive MessageType = "NSNodeActive" // 边缘节点活跃
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageDAO dbs.DAO
|
type MessageDAO dbs.DAO
|
||||||
@@ -93,8 +97,8 @@ func (this *MessageDAO) FindEnabledMessage(tx *dbs.Tx, id int64) (*Message, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateClusterMessage 创建集群消息
|
// CreateClusterMessage 创建集群消息
|
||||||
func (this *MessageDAO) CreateClusterMessage(tx *dbs.Tx, clusterId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
func (this *MessageDAO) CreateClusterMessage(tx *dbs.Tx, role string, clusterId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
||||||
_, err := this.createMessage(tx, clusterId, 0, messageType, level, subject, body, paramsJSON)
|
_, err := this.createMessage(tx, role, clusterId, 0, messageType, level, subject, body, paramsJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -113,9 +117,9 @@ func (this *MessageDAO) CreateClusterMessage(tx *dbs.Tx, clusterId int64, messag
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateNodeMessage 创建节点消息
|
// CreateNodeMessage 创建节点消息
|
||||||
func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, role string, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) error {
|
||||||
// 检查N分钟内是否已经发送过
|
// 检查N分钟内是否已经发送过
|
||||||
hash := this.calHash(subject, body, paramsJSON)
|
hash := this.calHash(role, clusterId, nodeId, subject, body, paramsJSON)
|
||||||
exists, err := this.Query(tx).
|
exists, err := this.Query(tx).
|
||||||
Attr("hash", hash).
|
Attr("hash", hash).
|
||||||
Gt("createdAt", time.Now().Unix()-10*60). // 10分钟
|
Gt("createdAt", time.Now().Unix()-10*60). // 10分钟
|
||||||
@@ -127,31 +131,34 @@ func (this *MessageDAO) CreateNodeMessage(tx *dbs.Tx, clusterId int64, nodeId in
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.createMessage(tx, clusterId, nodeId, messageType, level, subject, body, paramsJSON)
|
_, err = this.createMessage(tx, role, clusterId, nodeId, messageType, level, subject, body, paramsJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送给媒介接收人 - 集群
|
// TODO 目前只支持边缘节点发送消息,将来要支持NS节点
|
||||||
err = SharedMessageTaskDAO.CreateMessageTasks(tx, MessageTaskTarget{
|
if role == nodeconfigs.NodeRoleNode {
|
||||||
ClusterId: clusterId,
|
// 发送给媒介接收人 - 集群
|
||||||
NodeId: 0,
|
|
||||||
ServerId: 0,
|
|
||||||
}, messageType, subject, body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送给媒介接收人 - 节点
|
|
||||||
if nodeId > 0 {
|
|
||||||
err = SharedMessageTaskDAO.CreateMessageTasks(tx, MessageTaskTarget{
|
err = SharedMessageTaskDAO.CreateMessageTasks(tx, MessageTaskTarget{
|
||||||
ClusterId: clusterId,
|
ClusterId: clusterId,
|
||||||
NodeId: nodeId,
|
NodeId: 0,
|
||||||
ServerId: 0,
|
ServerId: 0,
|
||||||
}, messageType, subject, body)
|
}, messageType, subject, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送给媒介接收人 - 节点
|
||||||
|
if nodeId > 0 {
|
||||||
|
err = SharedMessageTaskDAO.CreateMessageTasks(tx, MessageTaskTarget{
|
||||||
|
ClusterId: clusterId,
|
||||||
|
NodeId: nodeId,
|
||||||
|
ServerId: 0,
|
||||||
|
}, messageType, subject, body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -179,7 +186,7 @@ func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, m
|
|||||||
op.State = MessageStateEnabled
|
op.State = MessageStateEnabled
|
||||||
op.IsRead = false
|
op.IsRead = false
|
||||||
op.Day = timeutil.Format("Ymd")
|
op.Day = timeutil.Format("Ymd")
|
||||||
op.Hash = this.calHash(subject, body, paramsJSON)
|
op.Hash = this.calHash(nodeconfigs.NodeRoleAdmin, 0, 0, subject, body, paramsJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -287,13 +294,14 @@ func (this *MessageDAO) CheckMessageUser(tx *dbs.Tx, messageId int64, adminId in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建消息
|
// 创建消息
|
||||||
func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) (int64, error) {
|
func (this *MessageDAO) createMessage(tx *dbs.Tx, role string, clusterId int64, nodeId int64, messageType MessageType, level string, subject string, body string, paramsJSON []byte) (int64, error) {
|
||||||
// TODO 检查同样的消息最近是否发送过
|
// TODO 检查同样的消息最近是否发送过
|
||||||
|
|
||||||
// 创建新消息
|
// 创建新消息
|
||||||
op := NewMessageOperator()
|
op := NewMessageOperator()
|
||||||
op.AdminId = 0 // TODO
|
op.AdminId = 0 // TODO
|
||||||
op.UserId = 0 // TODO
|
op.UserId = 0 // TODO
|
||||||
|
op.Role = role
|
||||||
op.ClusterId = clusterId
|
op.ClusterId = clusterId
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
op.Type = messageType
|
op.Type = messageType
|
||||||
@@ -314,7 +322,7 @@ func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64,
|
|||||||
op.State = MessageStateEnabled
|
op.State = MessageStateEnabled
|
||||||
op.CreatedAt = time.Now().Unix()
|
op.CreatedAt = time.Now().Unix()
|
||||||
op.Day = timeutil.Format("Ymd")
|
op.Day = timeutil.Format("Ymd")
|
||||||
op.Hash = this.calHash(subject, body, paramsJSON)
|
op.Hash = this.calHash(role, clusterId, nodeId, subject, body, paramsJSON)
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -324,10 +332,11 @@ func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 计算Hash
|
// 计算Hash
|
||||||
func (this *MessageDAO) calHash(subject string, body string, paramsJSON []byte) string {
|
func (this *MessageDAO) calHash(role string, clusterId int64, nodeId int64, subject string, body string, paramsJSON []byte) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(subject))
|
h.Write([]byte(role + "@" + types.String(clusterId) + "@" + types.String(nodeId)))
|
||||||
h.Write([]byte(body))
|
h.Write([]byte(subject + "@"))
|
||||||
|
h.Write([]byte(body + "@"))
|
||||||
h.Write(paramsJSON)
|
h.Write(paramsJSON)
|
||||||
return fmt.Sprintf("%x", h.Sum(nil))
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -11,7 +12,7 @@ func TestMessageDAO_CreateClusterMessage(t *testing.T) {
|
|||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
|
|
||||||
dao := NewMessageDAO()
|
dao := NewMessageDAO()
|
||||||
err := dao.CreateClusterMessage(tx, 1, "test", "error", "123", "123", []byte("456"))
|
err := dao.CreateClusterMessage(tx, nodeconfigs.NodeRoleNode, 1, "test", "error", "123", "123", []byte("456"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ type Message struct {
|
|||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
|
Role string `field:"role"` // 角色
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
Level string `field:"level"` // 级别
|
Level string `field:"level"` // 级别
|
||||||
@@ -23,6 +24,7 @@ type MessageOperator struct {
|
|||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
|
Role interface{} // 角色
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId interface{} // 集群ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
Level interface{} // 级别
|
Level interface{} // 级别
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func (this *MetricChartDAO) FindMetricChartName(tx *dbs.Tx, chartId int64) (stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateChart 创建图表
|
// CreateChart 创建图表
|
||||||
func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map) (int64, error) {
|
func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, ignoreEmptyKeys bool, ignoredKeys []string) (int64, error) {
|
||||||
op := NewMetricChartOperator()
|
op := NewMetricChartOperator()
|
||||||
op.ItemId = itemId
|
op.ItemId = itemId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -90,13 +90,25 @@ func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, c
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
|
op.IgnoreEmptyKeys = ignoreEmptyKeys
|
||||||
|
|
||||||
|
if len(ignoredKeys) == 0 {
|
||||||
|
op.IgnoredKeys = "[]"
|
||||||
|
} else {
|
||||||
|
ignoredKeysJSON, err := json.Marshal(ignoredKeys)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.IgnoredKeys = ignoredKeysJSON
|
||||||
|
}
|
||||||
|
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = MetricChartStateEnabled
|
op.State = MetricChartStateEnabled
|
||||||
return this.SaveInt64(tx, op)
|
return this.SaveInt64(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateChart 修改图表
|
// UpdateChart 修改图表
|
||||||
func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, isOn bool) error {
|
func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, ignoreEmptyKeys bool, ignoredKeys []string, isOn bool) error {
|
||||||
if chartId <= 0 {
|
if chartId <= 0 {
|
||||||
return errors.New("invalid chartId")
|
return errors.New("invalid chartId")
|
||||||
}
|
}
|
||||||
@@ -115,6 +127,17 @@ func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
|
op.IgnoreEmptyKeys = ignoreEmptyKeys
|
||||||
|
|
||||||
|
if len(ignoredKeys) == 0 {
|
||||||
|
op.IgnoredKeys = "[]"
|
||||||
|
} else {
|
||||||
|
ignoredKeysJSON, err := json.Marshal(ignoredKeys)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.IgnoredKeys = ignoredKeysJSON
|
||||||
|
}
|
||||||
|
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
|
|
||||||
|
|||||||
@@ -2,31 +2,35 @@ package models
|
|||||||
|
|
||||||
// MetricChart 指标图表
|
// MetricChart 指标图表
|
||||||
type MetricChart struct {
|
type MetricChart struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
ItemId uint32 `field:"itemId"` // 指标ID
|
ItemId uint32 `field:"itemId"` // 指标ID
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Code string `field:"code"` // 代号
|
Code string `field:"code"` // 代号
|
||||||
Type string `field:"type"` // 图形类型
|
Type string `field:"type"` // 图形类型
|
||||||
WidthDiv int32 `field:"widthDiv"` // 宽度划分
|
WidthDiv int32 `field:"widthDiv"` // 宽度划分
|
||||||
Params string `field:"params"` // 图形参数
|
Params string `field:"params"` // 图形参数
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
MaxItems uint32 `field:"maxItems"` // 最多条目
|
MaxItems uint32 `field:"maxItems"` // 最多条目
|
||||||
|
IgnoreEmptyKeys uint8 `field:"ignoreEmptyKeys"` // 忽略空的键值
|
||||||
|
IgnoredKeys string `field:"ignoredKeys"` // 忽略键值
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricChartOperator struct {
|
type MetricChartOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
ItemId interface{} // 指标ID
|
ItemId interface{} // 指标ID
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Code interface{} // 代号
|
Code interface{} // 代号
|
||||||
Type interface{} // 图形类型
|
Type interface{} // 图形类型
|
||||||
WidthDiv interface{} // 宽度划分
|
WidthDiv interface{} // 宽度划分
|
||||||
Params interface{} // 图形参数
|
Params interface{} // 图形参数
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
MaxItems interface{} // 最多条目
|
MaxItems interface{} // 最多条目
|
||||||
|
IgnoreEmptyKeys interface{} // 忽略空的键值
|
||||||
|
IgnoredKeys interface{} // 忽略键值
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMetricChartOperator() *MetricChartOperator {
|
func NewMetricChartOperator() *MetricChartOperator {
|
||||||
|
|||||||
@@ -1 +1,17 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
func (this *MetricChart) DecodeIgnoredKeys() []string {
|
||||||
|
if len(this.IgnoredKeys) == 0 {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err := json.Unmarshal([]byte(this.IgnoredKeys), &result)
|
||||||
|
if err != nil {
|
||||||
|
// 这里忽略错误
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -195,7 +196,7 @@ func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, key
|
|||||||
|
|
||||||
// 删除旧数据
|
// 删除旧数据
|
||||||
if versionChanged {
|
if versionChanged {
|
||||||
err := SharedMetricStatDAO.DeleteOldItemStats(tx, itemId, types.Int32(oldItem.Version+1))
|
err := SharedMetricStatDAO.DeleteOldVersionItemStats(tx, itemId, types.Int32(oldItem.Version+1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -308,7 +309,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
for _, clusterId := range clusterIds {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -320,7 +321,7 @@ func (this *MetricItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64, isPublic bool)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, clusterId := range clusterIds {
|
for _, clusterId := range clusterIds {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,11 +81,12 @@ func (this *MetricStatDAO) CreateStat(tx *dbs.Tx, hash string, clusterId int64,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteOldItemStats 删除以前版本的统计数据
|
// DeleteOldVersionItemStats 删除以前版本的统计数据
|
||||||
func (this *MetricStatDAO) DeleteOldItemStats(tx *dbs.Tx, itemId int64, version int32) error {
|
func (this *MetricStatDAO) DeleteOldVersionItemStats(tx *dbs.Tx, itemId int64, version int32) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Where("version<:version").
|
Where("version<:version").
|
||||||
|
Param("version", version).
|
||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -98,6 +99,17 @@ func (this *MetricStatDAO) DeleteItemStats(tx *dbs.Tx, itemId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteNodeItemStats 删除某个节点的统计数据
|
||||||
|
func (this *MetricStatDAO) DeleteNodeItemStats(tx *dbs.Tx, nodeId int64, serverId int64, itemId int64, time string) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Attr("nodeId", nodeId).
|
||||||
|
Attr("serverId", serverId).
|
||||||
|
Attr("itemId", itemId).
|
||||||
|
Attr("time", time).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// CountItemStats 计算统计数据数量
|
// CountItemStats 计算统计数据数量
|
||||||
func (this *MetricStatDAO) CountItemStats(tx *dbs.Tx, itemId int64, version int32) (int64, error) {
|
func (this *MetricStatDAO) CountItemStats(tx *dbs.Tx, itemId int64, version int32) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -123,7 +135,7 @@ func (this *MetricStatDAO) ListItemStats(tx *dbs.Tx, itemId int64, version int32
|
|||||||
|
|
||||||
// FindItemStatsAtLastTime 取得所有集群最近一次计时前 N 个数据
|
// FindItemStatsAtLastTime 取得所有集群最近一次计时前 N 个数据
|
||||||
// 适合每条数据中包含不同的Key的场景
|
// 适合每条数据中包含不同的Key的场景
|
||||||
func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
// 最近一次时间
|
// 最近一次时间
|
||||||
statOne, err := this.Query(tx).
|
statOne, err := this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -133,13 +145,13 @@ func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ver
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if statOne == nil {
|
if statOne == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
var lastStat = statOne.(*MetricStat)
|
var lastStat = statOne.(*MetricStat)
|
||||||
var lastTime = lastStat.Time
|
var lastTime = lastStat.Time
|
||||||
|
var query = this.Query(tx).
|
||||||
_, err = this.Query(tx).
|
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
Attr("time", lastTime).
|
Attr("time", lastTime).
|
||||||
@@ -149,14 +161,26 @@ func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ver
|
|||||||
Desc("value").
|
Desc("value").
|
||||||
Group("keys").
|
Group("keys").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindItemStatsWithClusterIdAndLastTime 取得集群最近一次计时前 N 个数据
|
// FindItemStatsWithClusterIdAndLastTime 取得集群最近一次计时前 N 个数据
|
||||||
// 适合每条数据中包含不同的Key的场景
|
// 适合每条数据中包含不同的Key的场景
|
||||||
func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clusterId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clusterId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
// 最近一次时间
|
// 最近一次时间
|
||||||
statOne, err := this.Query(tx).
|
statOne, err := this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -172,7 +196,7 @@ func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clu
|
|||||||
var lastStat = statOne.(*MetricStat)
|
var lastStat = statOne.(*MetricStat)
|
||||||
var lastTime = lastStat.Time
|
var lastTime = lastStat.Time
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -183,14 +207,27 @@ func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clu
|
|||||||
Desc("value").
|
Desc("value").
|
||||||
Group("keys").
|
Group("keys").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindItemStatsWithNodeIdAndLastTime 取得节点最近一次计时前 N 个数据
|
// FindItemStatsWithNodeIdAndLastTime 取得节点最近一次计时前 N 个数据
|
||||||
// 适合每条数据中包含不同的Key的场景
|
// 适合每条数据中包含不同的Key的场景
|
||||||
func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
// 最近一次时间
|
// 最近一次时间
|
||||||
statOne, err := this.Query(tx).
|
statOne, err := this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -205,8 +242,7 @@ func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId
|
|||||||
}
|
}
|
||||||
var lastStat = statOne.(*MetricStat)
|
var lastStat = statOne.(*MetricStat)
|
||||||
var lastTime = lastStat.Time
|
var lastTime = lastStat.Time
|
||||||
|
var query = this.Query(tx).
|
||||||
_, err = this.Query(tx).
|
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -217,14 +253,27 @@ func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId
|
|||||||
Desc("value").
|
Desc("value").
|
||||||
Group("keys").
|
Group("keys").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindItemStatsWithServerIdAndLastTime 取得节点最近一次计时前 N 个数据
|
// FindItemStatsWithServerIdAndLastTime 取得节点最近一次计时前 N 个数据
|
||||||
// 适合每条数据中包含不同的Key的场景
|
// 适合每条数据中包含不同的Key的场景
|
||||||
func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serverId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serverId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
// 最近一次时间
|
// 最近一次时间
|
||||||
statOne, err := this.Query(tx).
|
statOne, err := this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
@@ -240,7 +289,7 @@ func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serv
|
|||||||
var lastStat = statOne.(*MetricStat)
|
var lastStat = statOne.(*MetricStat)
|
||||||
var lastTime = lastStat.Time
|
var lastTime = lastStat.Time
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -251,15 +300,28 @@ func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serv
|
|||||||
Desc("value").
|
Desc("value").
|
||||||
Group("keys").
|
Group("keys").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindLatestItemStats 取得所有集群上最近 N 个时间的数据
|
// FindLatestItemStats 取得所有集群上最近 N 个时间的数据
|
||||||
// 适合同个Key在不同时间段的变化场景
|
// 适合同个Key在不同时间段的变化场景
|
||||||
func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
// TODO 增加更多聚合算法,比如 AVG、MEDIAN、MIN、MAX 等
|
// TODO 增加更多聚合算法,比如 AVG、MEDIAN、MIN、MAX 等
|
||||||
@@ -268,7 +330,20 @@ func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version
|
|||||||
Desc("time").
|
Desc("time").
|
||||||
Group("time").
|
Group("time").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -279,8 +354,8 @@ func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version
|
|||||||
|
|
||||||
// FindLatestItemStatsWithClusterId 取得集群最近 N 个时间的数据
|
// FindLatestItemStatsWithClusterId 取得集群最近 N 个时间的数据
|
||||||
// 适合同个Key在不同时间段的变化场景
|
// 适合同个Key在不同时间段的变化场景
|
||||||
func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -290,7 +365,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterI
|
|||||||
Desc("time").
|
Desc("time").
|
||||||
Group("time").
|
Group("time").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -301,8 +389,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterI
|
|||||||
|
|
||||||
// FindLatestItemStatsWithNodeId 取得节点最近 N 个时间的数据
|
// FindLatestItemStatsWithNodeId 取得节点最近 N 个时间的数据
|
||||||
// 适合同个Key在不同时间段的变化场景
|
// 适合同个Key在不同时间段的变化场景
|
||||||
func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -312,7 +400,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int6
|
|||||||
Desc("time").
|
Desc("time").
|
||||||
Group("time").
|
Group("time").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -323,8 +424,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int6
|
|||||||
|
|
||||||
// FindLatestItemStatsWithServerId 取得服务最近 N 个时间的数据
|
// FindLatestItemStatsWithServerId 取得服务最近 N 个时间的数据
|
||||||
// 适合同个Key在不同时间段的变化场景
|
// 适合同个Key在不同时间段的变化场景
|
||||||
func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
|
func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
|
||||||
_, err = this.Query(tx).
|
var query = this.Query(tx).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Attr("itemId", itemId).
|
Attr("itemId", itemId).
|
||||||
Attr("version", version).
|
Attr("version", version).
|
||||||
@@ -334,7 +435,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId
|
|||||||
Desc("time").
|
Desc("time").
|
||||||
Group("time").
|
Group("time").
|
||||||
Limit(size).
|
Limit(size).
|
||||||
Slice(&result).
|
Slice(&result)
|
||||||
|
if ignoreEmptyKeys {
|
||||||
|
query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
|
||||||
|
}
|
||||||
|
if len(ignoreKeys) > 0 {
|
||||||
|
ignoreKeysJSON, err := json.Marshal(ignoreKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
|
||||||
|
query.Param("ignoredKeys", string(ignoreKeysJSON))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = query.
|
||||||
FindAll()
|
FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -3,4 +3,16 @@ package models
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNewMetricStatDAO_InsertMany(t *testing.T) {
|
||||||
|
for i := 0; i <= 1; i++ {
|
||||||
|
err := NewMetricStatDAO().CreateStat(nil, types.String(i) + "_v1", 18, 48, 23, 25, []string{"/html" + types.String(i)}, 1, "20210728", 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Log("done")
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package nameservers
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -35,12 +36,15 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnableNSDomain 启用条目
|
// EnableNSDomain 启用条目
|
||||||
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id int64) error {
|
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, domainId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(domainId).
|
||||||
Set("state", NSDomainStateEnabled).
|
Set("state", NSDomainStateEnabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, domainId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableNSDomain 禁用条目
|
// DisableNSDomain 禁用条目
|
||||||
@@ -55,7 +59,10 @@ func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, domainId int64) error {
|
|||||||
Set("state", NSDomainStateDisabled).
|
Set("state", NSDomainStateDisabled).
|
||||||
Set("version", version).
|
Set("version", version).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, domainId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledNSDomain 查找启用中的条目
|
// FindEnabledNSDomain 查找启用中的条目
|
||||||
@@ -92,7 +99,16 @@ func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64,
|
|||||||
op.Version = version
|
op.Version = version
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = NSDomainStateEnabled
|
op.State = NSDomainStateEnabled
|
||||||
return this.SaveInt64(tx, op)
|
domainId, err := this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.NotifyUpdate(tx, domainId)
|
||||||
|
if err != nil {
|
||||||
|
return domainId, err
|
||||||
|
}
|
||||||
|
return domainId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDomain 修改域名
|
// UpdateDomain 修改域名
|
||||||
@@ -101,6 +117,14 @@ func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int6
|
|||||||
return errors.New("invalid domainId")
|
return errors.New("invalid domainId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldClusterId, err := this.Query(tx).
|
||||||
|
Pk(domainId).
|
||||||
|
Result("clusterId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
version, err := this.IncreaseVersion(tx)
|
version, err := this.IncreaseVersion(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -112,7 +136,20 @@ func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int6
|
|||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Version = version
|
op.Version = version
|
||||||
return this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知更新
|
||||||
|
if oldClusterId > 0 && oldClusterId != clusterId {
|
||||||
|
err = models.SharedNSClusterDAO.NotifyUpdate(tx, oldClusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, domainId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledDomains 计算域名数量
|
// CountAllEnabledDomains 计算域名数量
|
||||||
@@ -121,7 +158,7 @@ func (this *NSDomainDAO) CountAllEnabledDomains(tx *dbs.Tx, clusterId int64, use
|
|||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
query.Attr("clusterId", clusterId)
|
query.Attr("clusterId", clusterId)
|
||||||
} else {
|
} else {
|
||||||
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
|
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
if userId > 0 {
|
if userId > 0 {
|
||||||
query.Attr("userId", userId)
|
query.Attr("userId", userId)
|
||||||
@@ -144,7 +181,7 @@ func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId
|
|||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
query.Attr("clusterId", clusterId)
|
query.Attr("clusterId", clusterId)
|
||||||
} else {
|
} else {
|
||||||
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
|
query.Where("clusterId IN (SELECT id FROM " + models.SharedNSClusterDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
if userId > 0 {
|
if userId > 0 {
|
||||||
query.Attr("userId", userId)
|
query.Attr("userId", userId)
|
||||||
@@ -194,3 +231,59 @@ func (this *NSDomainDAO) FindDomainIdWithName(tx *dbs.Tx, clusterId int64, name
|
|||||||
ResultPk().
|
ResultPk().
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledDomainTSIG 获取TSIG配置
|
||||||
|
func (this *NSDomainDAO) FindEnabledDomainTSIG(tx *dbs.Tx, domainId int64) ([]byte, error) {
|
||||||
|
tsig, err := this.Query(tx).
|
||||||
|
Pk(domainId).
|
||||||
|
Result("tsig").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return []byte(tsig), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDomainTSIG 修改TSIG配置
|
||||||
|
func (this *NSDomainDAO) UpdateDomainTSIG(tx *dbs.Tx, domainId int64, tsigJSON []byte) error {
|
||||||
|
version, err := this.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(domainId).
|
||||||
|
Set("tsig", tsigJSON).
|
||||||
|
Set("version", version).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, domainId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledDomainClusterId 获取域名的集群ID
|
||||||
|
func (this *NSDomainDAO) FindEnabledDomainClusterId(tx *dbs.Tx, domainId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(domainId).
|
||||||
|
State(NSDomainStateEnabled).
|
||||||
|
Result("clusterId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更改
|
||||||
|
func (this *NSDomainDAO) NotifyUpdate(tx *dbs.Tx, domainId int64) error {
|
||||||
|
clusterId, err := this.Query(tx).
|
||||||
|
Result("clusterId").
|
||||||
|
Pk(domainId).
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if clusterId > 0 {
|
||||||
|
return models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, models.NSNodeTaskTypeDomainChanged)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type NSDomain struct {
|
|||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
Version uint64 `field:"version"` // 版本
|
Version uint64 `field:"version"` // 版本
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
|
Tsig string `field:"tsig"` // TSIG配置
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSDomainOperator struct {
|
type NSDomainOperator struct {
|
||||||
@@ -21,6 +22,7 @@ type NSDomainOperator struct {
|
|||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
Version interface{} // 版本
|
Version interface{} // 版本
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
Tsig interface{} // TSIG配置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSDomainOperator() *NSDomainOperator {
|
func NewNSDomainOperator() *NSDomainOperator {
|
||||||
|
|||||||
209
internal/db/models/nameservers/ns_key_dao.go
Normal file
209
internal/db/models/nameservers/ns_key_dao.go
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NSKeyStateEnabled = 1 // 已启用
|
||||||
|
NSKeyStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type NSKeyDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewNSKeyDAO() *NSKeyDAO {
|
||||||
|
return dbs.NewDAO(&NSKeyDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeNSKeys",
|
||||||
|
Model: new(NSKey),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*NSKeyDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedNSKeyDAO *NSKeyDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedNSKeyDAO = NewNSKeyDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableNSKey 启用条目
|
||||||
|
func (this *NSKeyDAO) EnableNSKey(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", NSKeyStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableNSKey 禁用条目
|
||||||
|
func (this *NSKeyDAO) DisableNSKey(tx *dbs.Tx, keyId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Set("state", NSKeyStateDisabled).
|
||||||
|
Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, keyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledNSKey 查找启用中的条目
|
||||||
|
func (this *NSKeyDAO) FindEnabledNSKey(tx *dbs.Tx, id int64) (*NSKey, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", NSKeyStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*NSKey), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindNSKeyName 根据主键查找名称
|
||||||
|
func (this *NSKeyDAO) FindNSKeyName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateKey 创建Key
|
||||||
|
func (this *NSKeyDAO) CreateKey(tx *dbs.Tx, domainId int64, zoneId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string) (int64, error) {
|
||||||
|
op := NewNSKeyOperator()
|
||||||
|
op.DomainId = domainId
|
||||||
|
op.ZoneId = zoneId
|
||||||
|
op.Name = name
|
||||||
|
op.Algo = algo
|
||||||
|
op.Secret = secret
|
||||||
|
op.SecretType = secretType
|
||||||
|
op.State = NSKeyStateEnabled
|
||||||
|
keyId, err := this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.NotifyUpdate(tx, keyId)
|
||||||
|
if err != nil {
|
||||||
|
return keyId, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateKey 修改Key
|
||||||
|
func (this *NSKeyDAO) UpdateKey(tx *dbs.Tx, keyId int64, name string, algo dnsconfigs.KeyAlgorithmType, secret string, secretType string, isOn bool) error {
|
||||||
|
if keyId <= 0 {
|
||||||
|
return errors.New("invalid keyId")
|
||||||
|
}
|
||||||
|
op := NewNSKeyOperator()
|
||||||
|
op.Id = keyId
|
||||||
|
op.Name = name
|
||||||
|
op.Algo = algo
|
||||||
|
op.Secret = secret
|
||||||
|
op.SecretType = secretType
|
||||||
|
op.IsOn = isOn
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, keyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountEnabledKeys 计算Key的数量
|
||||||
|
func (this *NSKeyDAO) CountEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64) (int64, error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
State(NSKeyStateEnabled)
|
||||||
|
if domainId > 0 {
|
||||||
|
query.Attr("domainId", domainId)
|
||||||
|
}
|
||||||
|
if zoneId > 0 {
|
||||||
|
query.Attr("zoneId", zoneId)
|
||||||
|
}
|
||||||
|
return query.Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListEnabledKeys 列出单页Key
|
||||||
|
func (this *NSKeyDAO) ListEnabledKeys(tx *dbs.Tx, domainId int64, zoneId int64, offset int64, size int64) (result []*NSKey, err error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
State(NSKeyStateEnabled)
|
||||||
|
if domainId > 0 {
|
||||||
|
query.Attr("domainId", domainId)
|
||||||
|
}
|
||||||
|
if zoneId > 0 {
|
||||||
|
query.Attr("zoneId", zoneId)
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
|
DescPk().
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncreaseVersion 增加版本
|
||||||
|
func (this *NSKeyDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
|
||||||
|
return models.SharedSysLockerDAO.Increase(tx, "NS_KEY_VERSION", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListKeysAfterVersion 列出某个版本后的密钥
|
||||||
|
func (this *NSKeyDAO) ListKeysAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSKey, err error) {
|
||||||
|
if size <= 0 {
|
||||||
|
size = 10000
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Gte("version", version).
|
||||||
|
Limit(size).
|
||||||
|
Asc("version").
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更新
|
||||||
|
func (this *NSKeyDAO) NotifyUpdate(tx *dbs.Tx, keyId int64) error {
|
||||||
|
version, err := this.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Set("version", version).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知集群
|
||||||
|
domainId, err := this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Result("domainId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if domainId > 0 {
|
||||||
|
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if clusterId > 0 {
|
||||||
|
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, models.NSNodeTaskTypeKeyChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
32
internal/db/models/nameservers/ns_key_model.go
Normal file
32
internal/db/models/nameservers/ns_key_model.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
// NSKey 密钥管理
|
||||||
|
type NSKey struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
IsOn uint8 `field:"isOn"` // 状态
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
DomainId uint64 `field:"domainId"` // 域名ID
|
||||||
|
ZoneId uint64 `field:"zoneId"` // 子域ID
|
||||||
|
Algo string `field:"algo"` // 算法
|
||||||
|
Secret string `field:"secret"` // 密码
|
||||||
|
SecretType string `field:"secretType"` // 密码类型
|
||||||
|
Version uint64 `field:"version"` // 版本号
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type NSKeyOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
IsOn interface{} // 状态
|
||||||
|
Name interface{} // 名称
|
||||||
|
DomainId interface{} // 域名ID
|
||||||
|
ZoneId interface{} // 子域ID
|
||||||
|
Algo interface{} // 算法
|
||||||
|
Secret interface{} // 密码
|
||||||
|
SecretType interface{} // 密码类型
|
||||||
|
Version interface{} // 版本号
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNSKeyOperator() *NSKeyOperator {
|
||||||
|
return &NSKeyOperator{}
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package nameservers
|
|
||||||
|
|
||||||
// NSNode 域名服务器节点
|
|
||||||
type NSNode struct {
|
|
||||||
Id uint32 `field:"id"` // ID
|
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
|
||||||
Name string `field:"name"` // 节点名称
|
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
|
||||||
Status string `field:"status"` // 运行状态
|
|
||||||
UniqueId string `field:"uniqueId"` // 节点ID
|
|
||||||
Secret string `field:"secret"` // 密钥
|
|
||||||
IsUp uint8 `field:"isUp"` // 是否运行
|
|
||||||
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
|
|
||||||
InstallStatus string `field:"installStatus"` // 安装状态
|
|
||||||
InstallDir string `field:"installDir"` // 安装目录
|
|
||||||
State uint8 `field:"state"` // 状态
|
|
||||||
}
|
|
||||||
|
|
||||||
type NSNodeOperator struct {
|
|
||||||
Id interface{} // ID
|
|
||||||
AdminId interface{} // 管理员ID
|
|
||||||
ClusterId interface{} // 集群ID
|
|
||||||
Name interface{} // 节点名称
|
|
||||||
IsOn interface{} // 是否启用
|
|
||||||
Status interface{} // 运行状态
|
|
||||||
UniqueId interface{} // 节点ID
|
|
||||||
Secret interface{} // 密钥
|
|
||||||
IsUp interface{} // 是否运行
|
|
||||||
IsInstalled interface{} // 是否已安装
|
|
||||||
InstallStatus interface{} // 安装状态
|
|
||||||
InstallDir interface{} // 安装目录
|
|
||||||
State interface{} // 状态
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewNSNodeOperator() *NSNodeOperator {
|
|
||||||
return &NSNodeOperator{}
|
|
||||||
}
|
|
||||||
67
internal/db/models/nameservers/ns_question_option_dao.go
Normal file
67
internal/db/models/nameservers/ns_question_option_dao.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NSQuestionOptionDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewNSQuestionOptionDAO() *NSQuestionOptionDAO {
|
||||||
|
return dbs.NewDAO(&NSQuestionOptionDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeNSQuestionOptions",
|
||||||
|
Model: new(NSQuestionOption),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*NSQuestionOptionDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedNSQuestionOptionDAO *NSQuestionOptionDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedNSQuestionOptionDAO = NewNSQuestionOptionDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindNSQuestionOptionName 根据主键查找名称
|
||||||
|
func (this *NSQuestionOptionDAO) FindNSQuestionOptionName(tx *dbs.Tx, id uint64) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOption 创建选项
|
||||||
|
func (this *NSQuestionOptionDAO) CreateOption(tx *dbs.Tx, name string, values maps.Map) (int64, error) {
|
||||||
|
if values == nil {
|
||||||
|
values = maps.Map{}
|
||||||
|
}
|
||||||
|
var op = NewNSQuestionOptionOperator()
|
||||||
|
op.Name = name
|
||||||
|
op.Values = values.AsJSON()
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOption 读取选项
|
||||||
|
func (this *NSQuestionOptionDAO) FindOption(tx *dbs.Tx, optionId int64) (*NSQuestionOption, error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(optionId).
|
||||||
|
Find()
|
||||||
|
if one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return one.(*NSQuestionOption), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOption 删除选项
|
||||||
|
func (this *NSQuestionOptionDAO) DeleteOption(tx *dbs.Tx, optionId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(optionId).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
20
internal/db/models/nameservers/ns_question_option_model.go
Normal file
20
internal/db/models/nameservers/ns_question_option_model.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
// NSQuestionOption DNS请求选项
|
||||||
|
type NSQuestionOption struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 选项名
|
||||||
|
Values string `field:"values"` // 选项值
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
type NSQuestionOptionOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Name interface{} // 选项名
|
||||||
|
Values interface{} // 选项值
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNSQuestionOptionOperator() *NSQuestionOptionOperator {
|
||||||
|
return &NSQuestionOptionOperator{}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package nameservers
|
||||||
@@ -5,10 +5,10 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -38,27 +38,33 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnableNSRecord 启用条目
|
// EnableNSRecord 启用条目
|
||||||
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
|
func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, recordId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(recordId).
|
||||||
Set("state", NSRecordStateEnabled).
|
Set("state", NSRecordStateEnabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, recordId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableNSRecord 禁用条目
|
// DisableNSRecord 禁用条目
|
||||||
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id int64) error {
|
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, recordId int64) error {
|
||||||
version, err := this.IncreaseVersion(tx)
|
version, err := this.IncreaseVersion(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Pk(id).
|
Pk(recordId).
|
||||||
Set("state", NSRecordStateDisabled).
|
Set("state", NSRecordStateDisabled).
|
||||||
Set("version", version).
|
Set("version", version).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, recordId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledNSRecord 查找启用中的条目
|
// FindEnabledNSRecord 查找启用中的条目
|
||||||
@@ -82,7 +88,7 @@ func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateRecord 创建记录
|
// CreateRecord 创建记录
|
||||||
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) (int64, error) {
|
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string) (int64, error) {
|
||||||
version, err := this.IncreaseVersion(tx)
|
version, err := this.IncreaseVersion(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -97,7 +103,7 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
|
|||||||
op.Ttl = ttl
|
op.Ttl = ttl
|
||||||
|
|
||||||
if len(routeIds) == 0 {
|
if len(routeIds) == 0 {
|
||||||
op.RouteIds = "[]"
|
op.RouteIds = `["default"]`
|
||||||
} else {
|
} else {
|
||||||
routeIds, err := json.Marshal(routeIds)
|
routeIds, err := json.Marshal(routeIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -109,11 +115,20 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
|
|||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = NSRecordStateEnabled
|
op.State = NSRecordStateEnabled
|
||||||
op.Version = version
|
op.Version = version
|
||||||
return this.SaveInt64(tx, op)
|
recordId, err := this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.NotifyUpdate(tx, recordId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return recordId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRecord 修改记录
|
// UpdateRecord 修改记录
|
||||||
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64, isOn bool) error {
|
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []string, isOn bool) error {
|
||||||
if recordId <= 0 {
|
if recordId <= 0 {
|
||||||
return errors.New("invalid recordId")
|
return errors.New("invalid recordId")
|
||||||
}
|
}
|
||||||
@@ -133,7 +148,7 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
|
|||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
|
|
||||||
if len(routeIds) == 0 {
|
if len(routeIds) == 0 {
|
||||||
op.RouteIds = "[]"
|
op.RouteIds = `["default"]`
|
||||||
} else {
|
} else {
|
||||||
routeIds, err := json.Marshal(routeIds)
|
routeIds, err := json.Marshal(routeIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -144,11 +159,16 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
|
|||||||
|
|
||||||
op.Version = version
|
op.Version = version
|
||||||
|
|
||||||
return this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, recordId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledDomainRecords 计算域名中记录数量
|
// CountAllEnabledDomainRecords 计算域名中记录数量
|
||||||
func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64) (int64, error) {
|
func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string) (int64, error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("domainId", domainId).
|
Attr("domainId", domainId).
|
||||||
State(NSRecordStateEnabled)
|
State(NSRecordStateEnabled)
|
||||||
@@ -159,8 +179,12 @@ func (this *NSRecordDAO) CountAllEnabledDomainRecords(tx *dbs.Tx, domainId int64
|
|||||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||||
Param("keyword", "%"+keyword+"%")
|
Param("keyword", "%"+keyword+"%")
|
||||||
}
|
}
|
||||||
if routeId > 0 {
|
if len(routeCode) > 0 {
|
||||||
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
|
routeCodeJSON, err := json.Marshal(routeCode)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||||
}
|
}
|
||||||
return query.Count()
|
return query.Count()
|
||||||
}
|
}
|
||||||
@@ -174,7 +198,7 @@ func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledRecords 列出单页记录
|
// ListEnabledRecords 列出单页记录
|
||||||
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64, offset int64, size int64) (result []*NSRecord, err error) {
|
func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeCode string, offset int64, size int64) (result []*NSRecord, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("domainId", domainId).
|
Attr("domainId", domainId).
|
||||||
State(NSRecordStateEnabled)
|
State(NSRecordStateEnabled)
|
||||||
@@ -185,8 +209,12 @@ func (this *NSRecordDAO) ListEnabledRecords(tx *dbs.Tx, domainId int64, dnsType
|
|||||||
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
query.Where("(name LIKE :keyword OR value LIKE :keyword OR description LIKE :keyword)").
|
||||||
Param("keyword", "%"+keyword+"%")
|
Param("keyword", "%"+keyword+"%")
|
||||||
}
|
}
|
||||||
if routeId > 0 {
|
if len(routeCode) > 0 {
|
||||||
query.JSONContains("routeIds", strconv.FormatInt(routeId, 10))
|
routeCodeJSON, err := json.Marshal(routeCode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query.JSONContains("routeIds", string(routeCodeJSON))
|
||||||
}
|
}
|
||||||
_, err = query.
|
_, err = query.
|
||||||
DescPk().
|
DescPk().
|
||||||
@@ -230,3 +258,31 @@ func (this *NSRecordDAO) FindEnabledRecordWithName(tx *dbs.Tx, domainId int64, r
|
|||||||
}
|
}
|
||||||
return record.(*NSRecord), nil
|
return record.(*NSRecord), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更新
|
||||||
|
func (this *NSRecordDAO) NotifyUpdate(tx *dbs.Tx, recordId int64) error {
|
||||||
|
domainId, err := this.Query(tx).
|
||||||
|
Pk(recordId).
|
||||||
|
Result("domainId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if domainId == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
clusterId, err := SharedNSDomainDAO.FindEnabledDomainClusterId(tx, domainId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if clusterId > 0 {
|
||||||
|
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, models.NSNodeTaskTypeRecordChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,4 +3,27 @@ package nameservers
|
|||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNSRecord_DecodeRouteIds(t *testing.T) {
|
||||||
|
{
|
||||||
|
record := &NSRecord{}
|
||||||
|
t.Log(record.DecodeRouteIds())
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
record := &NSRecord{RouteIds: "[]"}
|
||||||
|
t.Log(record.DecodeRouteIds())
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
record := &NSRecord{RouteIds: "[1, 2, 3]"}
|
||||||
|
t.Log(record.DecodeRouteIds())
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
record := &NSRecord{RouteIds: `["id:1", "id:2", "isp:liantong"]`}
|
||||||
|
t.Log(record.DecodeRouteIds())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,26 @@
|
|||||||
package nameservers
|
package nameservers
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
)
|
||||||
|
|
||||||
func (this *NSRecord) DecodeRouteIds() []int64 {
|
func (this *NSRecord) DecodeRouteIds() []string {
|
||||||
routeIds := []int64{}
|
var routeIds = []string{}
|
||||||
if len(this.RouteIds) > 0 {
|
if len(this.RouteIds) > 0 {
|
||||||
_ = json.Unmarshal([]byte(this.RouteIds), &routeIds)
|
err := json.Unmarshal([]byte(this.RouteIds), &routeIds)
|
||||||
|
if err != nil {
|
||||||
|
// 检查是否有旧的数据
|
||||||
|
var oldRouteIds = []int64{}
|
||||||
|
err = json.Unmarshal([]byte(this.RouteIds), &oldRouteIds)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
routeIds = []string{}
|
||||||
|
for _, routeId := range oldRouteIds {
|
||||||
|
routeIds = append(routeIds, "id:"+types.String(routeId))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return routeIds
|
return routeIds
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,14 @@ package nameservers
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -35,12 +40,21 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EnableNSRoute 启用条目
|
// EnableNSRoute 启用条目
|
||||||
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id int64) error {
|
func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, routeId int64) error {
|
||||||
_, err := this.Query(tx).
|
version, err := this.IncreaseVersion(tx)
|
||||||
Pk(id).
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Pk(routeId).
|
||||||
Set("state", NSRouteStateEnabled).
|
Set("state", NSRouteStateEnabled).
|
||||||
|
Set("version", version).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableNSRoute 禁用条目
|
// DisableNSRoute 禁用条目
|
||||||
@@ -55,7 +69,10 @@ func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, routeId int64) error {
|
|||||||
Set("state", NSRouteStateDisabled).
|
Set("state", NSRouteStateDisabled).
|
||||||
Set("version", version).
|
Set("version", version).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledNSRoute 查找启用中的条目
|
// FindEnabledNSRoute 查找启用中的条目
|
||||||
@@ -70,6 +87,33 @@ func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, erro
|
|||||||
return result.(*NSRoute), err
|
return result.(*NSRoute), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledRouteWithCode 根据代号获取线路信息
|
||||||
|
func (this *NSRouteDAO) FindEnabledRouteWithCode(tx *dbs.Tx, code string) (*NSRoute, error) {
|
||||||
|
if regexp.MustCompile(`^id:\d+$`).MatchString(code) {
|
||||||
|
var routeId = types.Int64(code[strings.Index(code, ":")+1:])
|
||||||
|
route, err := this.FindEnabledNSRoute(tx, routeId)
|
||||||
|
if route == nil || err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
route.Code = "id:" + types.String(routeId)
|
||||||
|
return route, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
route := dnsconfigs.FindDefaultRoute(code)
|
||||||
|
if route == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &NSRoute{
|
||||||
|
Id: 0,
|
||||||
|
IsOn: 1,
|
||||||
|
Name: route.Name,
|
||||||
|
Code: route.Code,
|
||||||
|
State: NSRouteStateEnabled,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindNSRouteName 根据主键查找名称
|
// FindNSRouteName 根据主键查找名称
|
||||||
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -98,7 +142,17 @@ func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64,
|
|||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = NSRouteStateEnabled
|
op.State = NSRouteStateEnabled
|
||||||
op.Version = version
|
op.Version = version
|
||||||
return this.SaveInt64(tx, op)
|
routeId, err := this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.NotifyUpdate(tx)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return routeId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRoute 修改线路
|
// UpdateRoute 修改线路
|
||||||
@@ -123,7 +177,12 @@ func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rang
|
|||||||
|
|
||||||
op.Version = version
|
op.Version = version
|
||||||
|
|
||||||
return this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRouteOrders 修改线路排序
|
// UpdateRouteOrders 修改线路排序
|
||||||
@@ -145,7 +204,8 @@ func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
|
|||||||
}
|
}
|
||||||
order--
|
order--
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return this.NotifyUpdate(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledRoutes 列出所有线路
|
// FindAllEnabledRoutes 列出所有线路
|
||||||
@@ -190,3 +250,19 @@ func (this *NSRouteDAO) ListRoutesAfterVersion(tx *dbs.Tx, version int64, size i
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更新
|
||||||
|
func (this *NSRouteDAO) NotifyUpdate(tx *dbs.Tx) error {
|
||||||
|
// 线路变更时所有集群都要更新
|
||||||
|
clusterIds, err := models.SharedNSClusterDAO.FindAllEnabledClusterIds(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
err = models.SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, models.NSNodeTaskTypeRouteChanged)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ type NSRoute struct {
|
|||||||
Ranges string `field:"ranges"` // 范围
|
Ranges string `field:"ranges"` // 范围
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
Version uint64 `field:"version"` // 版本号
|
Version uint64 `field:"version"` // 版本号
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,6 +25,7 @@ type NSRouteOperator struct {
|
|||||||
Ranges interface{} // 范围
|
Ranges interface{} // 范围
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
Version interface{} // 版本号
|
Version interface{} // 版本号
|
||||||
|
Code interface{} // 代号
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
63
internal/db/models/nameservers/ns_zone_dao.go
Normal file
63
internal/db/models/nameservers/ns_zone_dao.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NSZoneStateEnabled = 1 // 已启用
|
||||||
|
NSZoneStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type NSZoneDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewNSZoneDAO() *NSZoneDAO {
|
||||||
|
return dbs.NewDAO(&NSZoneDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeNSZones",
|
||||||
|
Model: new(NSZone),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*NSZoneDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedNSZoneDAO *NSZoneDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedNSZoneDAO = NewNSZoneDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableNSZone 启用条目
|
||||||
|
func (this *NSZoneDAO) EnableNSZone(tx *dbs.Tx, id uint64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", NSZoneStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableNSZone 禁用条目
|
||||||
|
func (this *NSZoneDAO) DisableNSZone(tx *dbs.Tx, id uint64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", NSZoneStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledNSZone 查找启用中的条目
|
||||||
|
func (this *NSZoneDAO) FindEnabledNSZone(tx *dbs.Tx, id uint64) (*NSZone, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", NSZoneStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*NSZone), err
|
||||||
|
}
|
||||||
6
internal/db/models/nameservers/ns_zone_dao_test.go
Normal file
6
internal/db/models/nameservers/ns_zone_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
26
internal/db/models/nameservers/ns_zone_model.go
Normal file
26
internal/db/models/nameservers/ns_zone_model.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package nameservers
|
||||||
|
|
||||||
|
// NSZone 域名子域
|
||||||
|
type NSZone struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
DomainId uint64 `field:"domainId"` // 域名ID
|
||||||
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
Version uint64 `field:"version"` // 版本
|
||||||
|
Tsig string `field:"tsig"` // TSIG配置
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type NSZoneOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
DomainId interface{} // 域名ID
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Order interface{} // 排序
|
||||||
|
Version interface{} // 版本
|
||||||
|
Tsig interface{} // TSIG配置
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNSZoneOperator() *NSZoneOperator {
|
||||||
|
return &NSZoneOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/nameservers/ns_zone_model_ext.go
Normal file
1
internal/db/models/nameservers/ns_zone_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package nameservers
|
||||||
@@ -380,7 +380,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(tx *dbs.Tx, dn
|
|||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeClusterStateEnabled).
|
State(NodeClusterStateEnabled).
|
||||||
Attr("dnsDomainId", dnsDomainId).
|
Attr("dnsDomainId", dnsDomainId).
|
||||||
Result("id", "name", "dnsName", "dnsDomainId").
|
Result("id", "name", "dnsName", "dnsDomainId", "isOn").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
@@ -391,7 +391,7 @@ func (this *NodeClusterDAO) FindAllEnabledClustersHaveDNSDomain(tx *dbs.Tx) (res
|
|||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeClusterStateEnabled).
|
State(NodeClusterStateEnabled).
|
||||||
Gt("dnsDomainId", 0).
|
Gt("dnsDomainId", 0).
|
||||||
Result("id", "name", "dnsName", "dnsDomainId").
|
Result("id", "name", "dnsName", "dnsDomainId", "isOn").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
@@ -409,7 +409,7 @@ func (this *NodeClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int
|
|||||||
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64) (*NodeCluster, error) {
|
func (this *NodeClusterDAO) FindClusterDNSInfo(tx *dbs.Tx, clusterId int64) (*NodeCluster, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
Result("id", "name", "dnsName", "dnsDomainId", "dns").
|
Result("id", "name", "dnsName", "dnsDomainId", "dns", "isOn").
|
||||||
Find()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -499,7 +499,7 @@ func (this *NodeClusterDAO) CheckClusterDNS(tx *dbs.Tx, cluster *NodeCluster) (i
|
|||||||
// TODO 检查域名是否已解析
|
// TODO 检查域名是否已解析
|
||||||
|
|
||||||
// 检查节点
|
// 检查节点
|
||||||
nodes, err := SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId)
|
nodes, err := SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -837,9 +837,39 @@ func (this *NodeClusterDAO) FindLatestNodeClusters(tx *dbs.Tx, size int64) (resu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckNodeClusterIsOn 获取集群是否正在启用状态
|
||||||
|
func (this *NodeClusterDAO) CheckNodeClusterIsOn(tx *dbs.Tx, clusterId int64) (bool, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
State(NodeClusterStateEnabled).
|
||||||
|
Attr("isOn", true).
|
||||||
|
Exist()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledNodeClustersWithIds 查找一组集群
|
||||||
|
func (this *NodeClusterDAO) FindEnabledNodeClustersWithIds(tx *dbs.Tx, clusterIds []int64) (result []*NodeCluster, err error) {
|
||||||
|
if len(clusterIds) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
cluster, err := this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
State(NodeClusterStateEnabled).
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if cluster == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, cluster.(*NodeCluster))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyDNSUpdate 通知DNS更新
|
// NotifyDNSUpdate 通知DNS更新
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -158,5 +159,5 @@ func (this *NodeClusterMetricItemDAO) ExistsClusterItem(tx *dbs.Tx, clusterId in
|
|||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeClusterMetricItemDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
func (this *NodeClusterMetricItemDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 节点集群
|
// NodeCluster 节点集群
|
||||||
type NodeCluster struct {
|
type NodeCluster struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
UseAllAPINodes uint8 `field:"useAllAPINodes"` // 是否使用所有API节点
|
||||||
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
ApiNodes string `field:"apiNodes"` // 使用的API节点
|
||||||
@@ -31,6 +32,7 @@ type NodeClusterOperator struct {
|
|||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
UseAllAPINodes interface{} // 是否使用所有API节点
|
UseAllAPINodes interface{} // 是否使用所有API节点
|
||||||
ApiNodes interface{} // 使用的API节点
|
ApiNodes interface{} // 使用的API节点
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNode 修改节点
|
// UpdateNode 修改节点
|
||||||
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, groupId int64, regionId int64, maxCPU int32, isOn bool, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte) error {
|
func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, maxCPU int32, isOn bool, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte) error {
|
||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
return errors.New("invalid nodeId")
|
return errors.New("invalid nodeId")
|
||||||
}
|
}
|
||||||
@@ -171,6 +171,24 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
|
|||||||
op.Id = nodeId
|
op.Id = nodeId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.ClusterId = clusterId
|
op.ClusterId = clusterId
|
||||||
|
|
||||||
|
// 去重
|
||||||
|
var filteredSecondaryClusterIds = []int64{}
|
||||||
|
for _, secondaryClusterId := range secondaryClusterIds {
|
||||||
|
if secondaryClusterId <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if lists.ContainsInt64(filteredSecondaryClusterIds, secondaryClusterId) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filteredSecondaryClusterIds = append(filteredSecondaryClusterIds, secondaryClusterId)
|
||||||
|
}
|
||||||
|
filteredSecondaryClusterIdsJSON, err := json.Marshal(filteredSecondaryClusterIds)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op.SecondaryClusterIds = filteredSecondaryClusterIdsJSON
|
||||||
|
|
||||||
op.GroupId = groupId
|
op.GroupId = groupId
|
||||||
op.RegionId = regionId
|
op.RegionId = regionId
|
||||||
op.LatestVersion = dbs.SQL("latestVersion+1")
|
op.LatestVersion = dbs.SQL("latestVersion+1")
|
||||||
@@ -182,7 +200,7 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
|
|||||||
if len(maxCacheMemoryCapacityJSON) > 0 {
|
if len(maxCacheMemoryCapacityJSON) > 0 {
|
||||||
op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON
|
op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON
|
||||||
}
|
}
|
||||||
err := this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -212,6 +230,7 @@ func (this *NodeDAO) ListEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
keyword string,
|
keyword string,
|
||||||
groupId int64,
|
groupId int64,
|
||||||
regionId int64,
|
regionId int64,
|
||||||
|
includeSecondaryNodes bool,
|
||||||
order string,
|
order string,
|
||||||
offset int64,
|
offset int64,
|
||||||
size int64) (result []*Node, err error) {
|
size int64) (result []*Node, err error) {
|
||||||
@@ -223,7 +242,13 @@ func (this *NodeDAO) ListEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
|
|
||||||
// 集群
|
// 集群
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
query.Attr("clusterId", clusterId)
|
if includeSecondaryNodes {
|
||||||
|
query.Where("(clusterId=:primaryClusterId OR JSON_CONTAINS(secondaryClusterIds, :primaryClusterIdString))").
|
||||||
|
Param("primaryClusterId", clusterId).
|
||||||
|
Param("primaryClusterIdString", types.String(clusterId))
|
||||||
|
} else {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
@@ -327,12 +352,51 @@ func (this *NodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error)
|
|||||||
return types.Int64(col), err
|
return types.Int64(col), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledAndOnNodeClusterIds 获取节点所属所有可用而且启用的集群ID
|
||||||
|
func (this *NodeDAO) FindEnabledAndOnNodeClusterIds(tx *dbs.Tx, nodeId int64) (result []int64, err error) {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Result("clusterId", "secondaryClusterIds").
|
||||||
|
Find()
|
||||||
|
if one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var clusterId = int64(one.(*Node).ClusterId)
|
||||||
|
if clusterId > 0 {
|
||||||
|
result = append(result, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, clusterId := range one.(*Node).DecodeSecondaryClusterIds() {
|
||||||
|
if lists.ContainsInt64(result, clusterId) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否启用
|
||||||
|
isOn, err := SharedNodeClusterDAO.CheckNodeClusterIsOn(tx, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !isOn {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, clusterId)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// FindAllNodeIdsMatch 匹配节点并返回节点ID
|
// FindAllNodeIdsMatch 匹配节点并返回节点ID
|
||||||
func (this *NodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, isOn configutils.BoolState) (result []int64, err error) {
|
func (this *NodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, isOn configutils.BoolState) (result []int64, err error) {
|
||||||
query := this.Query(tx)
|
query := this.Query(tx)
|
||||||
query.State(NodeStateEnabled)
|
query.State(NodeStateEnabled)
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
query.Attr("clusterId", clusterId)
|
if includeSecondaryNodes {
|
||||||
|
query.Where("(clusterId=:primaryClusterId OR JSON_CONTAINS(secondaryClusterIds, :primaryClusterIdString))").
|
||||||
|
Param("primaryClusterId", clusterId).
|
||||||
|
Param("primaryClusterIdString", types.String(clusterId))
|
||||||
|
} else {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
@@ -368,9 +432,9 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int
|
|||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("isOn", true). // 只监控启用的节点
|
Attr("isOn", true). // 只监控启用的节点
|
||||||
Attr("isInstalled", true). // 只监控已经安装的节点
|
Attr("isInstalled", true). // 只监控已经安装的节点
|
||||||
Attr("isActive", true). // 当前已经在线的
|
Attr("isActive", true). // 当前已经在线的
|
||||||
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
|
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
|
||||||
Result("id", "name").
|
Result("id", "name").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
@@ -385,13 +449,20 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx,
|
|||||||
activeState configutils.BoolState,
|
activeState configutils.BoolState,
|
||||||
keyword string,
|
keyword string,
|
||||||
groupId int64,
|
groupId int64,
|
||||||
regionId int64) (int64, error) {
|
regionId int64,
|
||||||
|
includeSecondaryNodes bool) (int64, error) {
|
||||||
query := this.Query(tx)
|
query := this.Query(tx)
|
||||||
query.State(NodeStateEnabled)
|
query.State(NodeStateEnabled)
|
||||||
|
|
||||||
// 集群
|
// 集群
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
query.Attr("clusterId", clusterId)
|
if includeSecondaryNodes {
|
||||||
|
query.Where("(clusterId=:primaryClusterId OR JSON_CONTAINS(secondaryClusterIds, :primaryClusterIdString))").
|
||||||
|
Param("primaryClusterId", clusterId).
|
||||||
|
Param("primaryClusterIdString", types.String(clusterId))
|
||||||
|
} else {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
query.Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
@@ -575,11 +646,13 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
serverConfig := &serverconfigs.ServerConfig{}
|
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server)
|
||||||
err = json.Unmarshal([]byte(server.Config), serverConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if serverConfig == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
config.Servers = append(config.Servers, serverConfig)
|
config.Servers = append(config.Servers, serverConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,34 +671,37 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
|
|||||||
config.GlobalConfig = globalConfig
|
config.GlobalConfig = globalConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// WAF
|
var primaryClusterId = int64(node.ClusterId)
|
||||||
clusterId := int64(node.ClusterId)
|
var clusterIds = []int64{primaryClusterId}
|
||||||
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId)
|
clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
|
||||||
if err != nil {
|
for _, clusterId := range clusterIds {
|
||||||
return nil, err
|
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId)
|
||||||
}
|
|
||||||
if httpFirewallPolicyId > 0 {
|
|
||||||
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if firewallPolicy != nil {
|
if httpFirewallPolicyId > 0 {
|
||||||
config.HTTPFirewallPolicy = firewallPolicy
|
firewallPolicy, err := SharedHTTPFirewallPolicyDAO.ComposeFirewallPolicy(tx, httpFirewallPolicyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if firewallPolicy != nil {
|
||||||
|
config.HTTPFirewallPolicies = append(config.HTTPFirewallPolicies, firewallPolicy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 缓存策略
|
// 缓存策略
|
||||||
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId)
|
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if httpCachePolicyId > 0 {
|
|
||||||
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if cachePolicy != nil {
|
if httpCachePolicyId > 0 {
|
||||||
config.HTTPCachePolicy = cachePolicy
|
cachePolicy, err := SharedHTTPCachePolicyDAO.ComposeCachePolicy(tx, httpCachePolicyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if cachePolicy != nil {
|
||||||
|
config.HTTPCachePolicies = append(config.HTTPCachePolicies, cachePolicy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -653,14 +729,14 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TOA
|
// TOA
|
||||||
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, clusterId)
|
toaConfig, err := SharedNodeClusterDAO.FindClusterTOAConfig(tx, primaryClusterId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.TOA = toaConfig
|
config.TOA = toaConfig
|
||||||
|
|
||||||
// 系统服务
|
// 系统服务
|
||||||
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, clusterId)
|
services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, primaryClusterId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -669,7 +745,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 防火墙动作
|
// 防火墙动作
|
||||||
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, clusterId)
|
actions, err := SharedNodeClusterFirewallActionDAO.FindAllEnabledFirewallActions(tx, primaryClusterId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -865,6 +941,7 @@ func (this *NodeDAO) CountAllEnabledNodesWithGroupId(tx *dbs.Tx, groupId int64)
|
|||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("groupId", groupId).
|
Attr("groupId", groupId).
|
||||||
|
Where("clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1)").
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -878,10 +955,20 @@ func (this *NodeDAO) CountAllEnabledNodesWithRegionId(tx *dbs.Tx, regionId int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledNodesDNSWithClusterId 获取一个集群的节点DNS信息
|
// FindAllEnabledNodesDNSWithClusterId 获取一个集群的节点DNS信息
|
||||||
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64) (result []*Node, err error) {
|
func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool) (result []*Node, err error) {
|
||||||
_, err = this.Query(tx).
|
if clusterId <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if includeSecondaryNodes {
|
||||||
|
query.Where("(clusterId=:primaryClusterId OR JSON_CONTAINS(secondaryClusterIds, :primaryClusterIdString))").
|
||||||
|
Param("primaryClusterId", clusterId).
|
||||||
|
Param("primaryClusterIdString", types.String(clusterId))
|
||||||
|
} else {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("clusterId", clusterId).
|
|
||||||
Attr("isOn", true).
|
Attr("isOn", true).
|
||||||
Attr("isUp", true).
|
Attr("isUp", true).
|
||||||
Result("id", "name", "dnsRoutes", "isOn").
|
Result("id", "name", "dnsRoutes", "isOn").
|
||||||
@@ -911,7 +998,7 @@ func (this *NodeDAO) FindEnabledNodeDNS(tx *dbs.Tx, nodeId int64) (*Node, error)
|
|||||||
Pk(nodeId).
|
Pk(nodeId).
|
||||||
Result("id", "name", "dnsRoutes", "clusterId", "isOn").
|
Result("id", "name", "dnsRoutes", "clusterId", "isOn").
|
||||||
Find()
|
Find()
|
||||||
if err != nil || one == nil {
|
if one == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return one.(*Node), nil
|
return one.(*Node), nil
|
||||||
@@ -1108,6 +1195,94 @@ func (this *NodeDAO) FindEnabledNodesWithIds(tx *dbs.Tx, nodeIds []int64) (resul
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteNodeFromCluster 从集群中删除节点
|
||||||
|
func (this *NodeDAO) DeleteNodeFromCluster(tx *dbs.Tx, nodeId int64, clusterId int64) error {
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Result("clusterId", "secondaryClusterIds").
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if one == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var node = one.(*Node)
|
||||||
|
|
||||||
|
var secondaryClusterIds = []int64{}
|
||||||
|
for _, secondaryClusterId := range node.DecodeSecondaryClusterIds() {
|
||||||
|
if secondaryClusterId == clusterId {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
secondaryClusterIds = append(secondaryClusterIds, secondaryClusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
var newClusterId = int64(node.ClusterId)
|
||||||
|
|
||||||
|
if newClusterId == clusterId {
|
||||||
|
newClusterId = 0
|
||||||
|
|
||||||
|
// 选择一个从集群作为主集群
|
||||||
|
if len(secondaryClusterIds) > 0 {
|
||||||
|
newClusterId = secondaryClusterIds[0]
|
||||||
|
secondaryClusterIds = secondaryClusterIds[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
secondaryClusterIdsJSON, err := json.Marshal(secondaryClusterIds)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op := NewNodeOperator()
|
||||||
|
op.Id = nodeId
|
||||||
|
op.ClusterId = newClusterId
|
||||||
|
op.SecondaryClusterIds = secondaryClusterIdsJSON
|
||||||
|
|
||||||
|
if newClusterId == 0 {
|
||||||
|
op.State = NodeStateDisabled
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Save(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransferPrimaryClusterNodes 自动转移集群下的节点
|
||||||
|
func (this *NodeDAO) TransferPrimaryClusterNodes(tx *dbs.Tx, primaryClusterId int64) error {
|
||||||
|
if primaryClusterId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
Attr("clusterId", primaryClusterId).
|
||||||
|
Result("id", "secondaryClusterIds").
|
||||||
|
State(NodeStateEnabled).
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var node = one.(*Node)
|
||||||
|
clusterIds := node.DecodeSecondaryClusterIds()
|
||||||
|
if len(clusterIds) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var clusterId = clusterIds[0]
|
||||||
|
var secondaryClusterIds = clusterIds[1:]
|
||||||
|
secondaryClusterIdsJSON, err := json.Marshal(secondaryClusterIds)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(node.Id).
|
||||||
|
Set("clusterId", clusterId).
|
||||||
|
Set("secondaryClusterIds", secondaryClusterIdsJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||||
clusterId, err := this.FindNodeClusterId(tx, nodeId)
|
clusterId, err := this.FindNodeClusterId(tx, nodeId)
|
||||||
@@ -1115,32 +1290,32 @@ func (this *NodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
return SharedNodeTaskDAO.CreateNodeTask(tx, clusterId, nodeId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyDNSUpdate 通知DNS更新
|
// NotifyDNSUpdate 通知DNS更新
|
||||||
func (this *NodeDAO) NotifyDNSUpdate(tx *dbs.Tx, nodeId int64) error {
|
func (this *NodeDAO) NotifyDNSUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||||
clusterId, err := this.Query(tx).
|
clusterIds, err := this.FindEnabledAndOnNodeClusterIds(tx, nodeId)
|
||||||
Pk(nodeId).
|
|
||||||
Result("clusterId").
|
|
||||||
FindInt64Col(0) // 这里不需要加服务状态条件,因为我们即使删除也要删除对应的服务的DNS解析
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if clusterId <= 0 {
|
for _, clusterId := range clusterIds {
|
||||||
return nil
|
dnsInfo, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dnsInfo == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(dnsInfo.DnsName) == 0 || dnsInfo.DnsDomainId <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = dns.SharedDNSTaskDAO.CreateNodeTask(tx, nodeId, dns.DNSTaskTypeNodeChange)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dnsInfo, err := SharedNodeClusterDAO.FindClusterDNSInfo(tx, clusterId)
|
return nil
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if dnsInfo == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(dnsInfo.DnsName) == 0 || dnsInfo.DnsDomainId <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return dns.SharedDNSTaskDAO.CreateNodeTask(tx, nodeId, dns.DNSTaskTypeNodeChange)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func TestNodeDAO_FindAllNodeIdsMatch(t *testing.T) {
|
func TestNodeDAO_FindAllNodeIdsMatch(t *testing.T) {
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, 1, 0)
|
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, 1, true, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -24,3 +24,13 @@ func TestNodeDAO_UpdateNodeUp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNodeDAO_FindEnabledNodeClusterIds(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
clusterIds, err := NewNodeDAO().FindEnabledAndOnNodeClusterIds(tx, 48)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(clusterIds)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 节点授权
|
// NodeGrant 节点授权
|
||||||
type NodeGrant struct {
|
type NodeGrant struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
@@ -12,6 +12,7 @@ type NodeGrant struct {
|
|||||||
PrivateKey string `field:"privateKey"` // 密钥
|
PrivateKey string `field:"privateKey"` // 密钥
|
||||||
Description string `field:"description"` // 备注
|
Description string `field:"description"` // 备注
|
||||||
NodeId uint32 `field:"nodeId"` // 专有节点
|
NodeId uint32 `field:"nodeId"` // 专有节点
|
||||||
|
Role string `field:"role"` // 角色
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
}
|
}
|
||||||
@@ -27,6 +28,7 @@ type NodeGrantOperator struct {
|
|||||||
PrivateKey interface{} // 密钥
|
PrivateKey interface{} // 密钥
|
||||||
Description interface{} // 备注
|
Description interface{} // 备注
|
||||||
NodeId interface{} // 专有节点
|
NodeId interface{} // 专有节点
|
||||||
|
Role interface{} // 角色
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ func (this *NodeIPAddressDAO) DisableAllAddressesWithNodeId(tx *dbs.Tx, nodeId i
|
|||||||
}
|
}
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
|
Attr("role", role).
|
||||||
Set("state", NodeIPAddressStateDisabled).
|
Set("state", NodeIPAddressStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -36,8 +37,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLog 创建日志
|
// CreateLog 创建日志
|
||||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
|
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error {
|
||||||
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
|
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description)
|
||||||
|
|
||||||
// 检查是否在重复最后一条,避免重复创建
|
// 检查是否在重复最后一条,避免重复创建
|
||||||
lastLog, err := this.Query(tx).
|
lastLog, err := this.Query(tx).
|
||||||
@@ -62,6 +63,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
|
|||||||
op.Role = nodeRole
|
op.Role = nodeRole
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
op.ServerId = serverId
|
op.ServerId = serverId
|
||||||
|
op.OriginId = originId
|
||||||
op.Level = level
|
op.Level = level
|
||||||
op.Tag = tag
|
op.Tag = tag
|
||||||
op.Description = description
|
op.Description = description
|
||||||
@@ -88,7 +90,7 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountNodeLogs 计算节点日志数量
|
// CountNodeLogs 计算节点日志数量
|
||||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, originId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("role", role)
|
Attr("role", role)
|
||||||
if nodeId > 0 {
|
if nodeId > 0 {
|
||||||
@@ -96,14 +98,17 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
|
|||||||
} else {
|
} else {
|
||||||
switch role {
|
switch role {
|
||||||
case nodeconfigs.NodeRoleNode:
|
case nodeconfigs.NodeRoleNode:
|
||||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1 AND clusterId>0)")
|
||||||
case nodeconfigs.NodeRoleDNS:
|
case nodeconfigs.NodeRoleDNS:
|
||||||
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
|
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1 AND clusterId > 0)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if serverId > 0 {
|
if serverId > 0 {
|
||||||
query.Attr("serverId", serverId)
|
query.Attr("serverId", serverId)
|
||||||
}
|
}
|
||||||
|
if originId > 0 {
|
||||||
|
query.Attr("originId", originId)
|
||||||
|
}
|
||||||
if len(dayFrom) > 0 {
|
if len(dayFrom) > 0 {
|
||||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||||
query.Gte("day", dayFrom)
|
query.Gte("day", dayFrom)
|
||||||
@@ -128,6 +133,7 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
|||||||
role string,
|
role string,
|
||||||
nodeId int64,
|
nodeId int64,
|
||||||
serverId int64,
|
serverId int64,
|
||||||
|
originId int64,
|
||||||
allServers bool,
|
allServers bool,
|
||||||
dayFrom string,
|
dayFrom string,
|
||||||
dayTo string,
|
dayTo string,
|
||||||
@@ -143,9 +149,9 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
|||||||
} else {
|
} else {
|
||||||
switch role {
|
switch role {
|
||||||
case nodeconfigs.NodeRoleNode:
|
case nodeconfigs.NodeRoleNode:
|
||||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1 AND clusterId>0)")
|
||||||
case nodeconfigs.NodeRoleDNS:
|
case nodeconfigs.NodeRoleDNS:
|
||||||
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
|
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1 AND clusterId>0)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if serverId > 0 {
|
if serverId > 0 {
|
||||||
@@ -153,6 +159,9 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
|||||||
} else if allServers {
|
} else if allServers {
|
||||||
query.Where("serverId>0")
|
query.Where("serverId>0")
|
||||||
}
|
}
|
||||||
|
if originId > 0 {
|
||||||
|
query.Attr("originId", originId)
|
||||||
|
}
|
||||||
if fixedState == configutils.BoolStateYes {
|
if fixedState == configutils.BoolStateYes {
|
||||||
query.Attr("isFixed", 1)
|
query.Attr("isFixed", 1)
|
||||||
} else if fixedState == configutils.BoolStateNo {
|
} else if fixedState == configutils.BoolStateNo {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ type NodeLog struct {
|
|||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
Day string `field:"day"` // 日期
|
Day string `field:"day"` // 日期
|
||||||
ServerId uint32 `field:"serverId"` // 服务ID
|
ServerId uint32 `field:"serverId"` // 服务ID
|
||||||
|
OriginId uint32 `field:"originId"` // 源站ID
|
||||||
Hash string `field:"hash"` // 信息内容Hash
|
Hash string `field:"hash"` // 信息内容Hash
|
||||||
Count uint32 `field:"count"` // 重复次数
|
Count uint32 `field:"count"` // 重复次数
|
||||||
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||||
@@ -26,6 +27,7 @@ type NodeLogOperator struct {
|
|||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
Day interface{} // 日期
|
Day interface{} // 日期
|
||||||
ServerId interface{} // 服务ID
|
ServerId interface{} // 服务ID
|
||||||
|
OriginId interface{} // 源站ID
|
||||||
Hash interface{} // 信息内容Hash
|
Hash interface{} // 信息内容Hash
|
||||||
Count interface{} // 重复次数
|
Count interface{} // 重复次数
|
||||||
IsFixed interface{} // 是否已处理
|
IsFixed interface{} // 是否已处理
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -36,7 +37,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableNodeLogin 启用条目
|
||||||
func (this *NodeLoginDAO) EnableNodeLogin(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
|
func (this *NodeLoginDAO) EnableNodeLogin(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -44,16 +45,16 @@ func (this *NodeLoginDAO) EnableNodeLogin(tx *dbs.Tx, id uint32) (rowsAffected i
|
|||||||
Update()
|
Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableNodeLogin 禁用条目
|
||||||
func (this *NodeLoginDAO) DisableNodeLogin(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
|
func (this *NodeLoginDAO) DisableNodeLogin(tx *dbs.Tx, loginId int64) (rowsAffected int64, err error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(loginId).
|
||||||
Set("state", NodeLoginStateDisabled).
|
Set("state", NodeLoginStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledNodeLogin 查找启用中的条目
|
||||||
func (this *NodeLoginDAO) FindEnabledNodeLogin(tx *dbs.Tx, id uint32) (*NodeLogin, error) {
|
func (this *NodeLoginDAO) FindEnabledNodeLogin(tx *dbs.Tx, id int64) (*NodeLogin, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
Attr("state", NodeLoginStateEnabled).
|
Attr("state", NodeLoginStateEnabled).
|
||||||
@@ -64,7 +65,7 @@ func (this *NodeLoginDAO) FindEnabledNodeLogin(tx *dbs.Tx, id uint32) (*NodeLogi
|
|||||||
return result.(*NodeLogin), err
|
return result.(*NodeLogin), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindNodeLoginName 根据主键查找名称
|
||||||
func (this *NodeLoginDAO) FindNodeLoginName(tx *dbs.Tx, id uint32) (string, error) {
|
func (this *NodeLoginDAO) FindNodeLoginName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
name, err := this.Query(tx).
|
name, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -73,9 +74,14 @@ func (this *NodeLoginDAO) FindNodeLoginName(tx *dbs.Tx, id uint32) (string, erro
|
|||||||
return name.(string), err
|
return name.(string), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建认证
|
// CreateNodeLogin 创建认证
|
||||||
func (this *NodeLoginDAO) CreateNodeLogin(tx *dbs.Tx, nodeId int64, name string, loginType string, paramsJSON []byte) (loginId int64, err error) {
|
func (this *NodeLoginDAO) CreateNodeLogin(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64, name string, loginType string, paramsJSON []byte) (loginId int64, err error) {
|
||||||
|
if len(role) == 0 {
|
||||||
|
role = nodeconfigs.NodeRoleNode
|
||||||
|
}
|
||||||
|
|
||||||
login := NewNodeLoginOperator()
|
login := NewNodeLoginOperator()
|
||||||
|
login.Role = role
|
||||||
login.NodeId = nodeId
|
login.NodeId = nodeId
|
||||||
login.Name = name
|
login.Name = name
|
||||||
login.Type = loginType
|
login.Type = loginType
|
||||||
@@ -85,7 +91,7 @@ func (this *NodeLoginDAO) CreateNodeLogin(tx *dbs.Tx, nodeId int64, name string,
|
|||||||
return types.Int64(login.Id), err
|
return types.Int64(login.Id), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改认证
|
// UpdateNodeLogin 修改认证
|
||||||
func (this *NodeLoginDAO) UpdateNodeLogin(tx *dbs.Tx, loginId int64, name string, loginType string, paramsJSON []byte) error {
|
func (this *NodeLoginDAO) UpdateNodeLogin(tx *dbs.Tx, loginId int64, name string, loginType string, paramsJSON []byte) error {
|
||||||
if loginId <= 0 {
|
if loginId <= 0 {
|
||||||
return errors.New("invalid loginId")
|
return errors.New("invalid loginId")
|
||||||
@@ -99,9 +105,13 @@ func (this *NodeLoginDAO) UpdateNodeLogin(tx *dbs.Tx, loginId int64, name string
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找认证
|
// FindEnabledNodeLoginWithNodeId 查找认证
|
||||||
func (this *NodeLoginDAO) FindEnabledNodeLoginWithNodeId(tx *dbs.Tx, nodeId int64) (*NodeLogin, error) {
|
func (this *NodeLoginDAO) FindEnabledNodeLoginWithNodeId(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) (*NodeLogin, error) {
|
||||||
|
if len(role) == 0 {
|
||||||
|
role = nodeconfigs.NodeRoleNode
|
||||||
|
}
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
State(NodeLoginStateEnabled).
|
State(NodeLoginStateEnabled).
|
||||||
Find()
|
Find()
|
||||||
@@ -114,11 +124,63 @@ func (this *NodeLoginDAO) FindEnabledNodeLoginWithNodeId(tx *dbs.Tx, nodeId int6
|
|||||||
return one.(*NodeLogin), nil
|
return one.(*NodeLogin), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用某个节点的认证
|
// DisableNodeLogins 禁用某个节点的认证
|
||||||
func (this *NodeLoginDAO) DisableNodeLogins(tx *dbs.Tx, nodeId int64) error {
|
func (this *NodeLoginDAO) DisableNodeLogins(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
|
||||||
|
if len(role) == 0 {
|
||||||
|
role = nodeconfigs.NodeRoleNode
|
||||||
|
}
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Set("state", NodeLoginStateDisabled).
|
Set("state", NodeLoginStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *NodeLoginDAO) FindFrequentPorts(tx *dbs.Tx) ([]int32, error) {
|
||||||
|
ones, _, err := this.Query(tx).
|
||||||
|
Attr("state", NodeLoginStateEnabled).
|
||||||
|
Result("JSON_EXTRACT(params, '$.port') as `port`", "COUNT(*) AS c").
|
||||||
|
Having("port>0").
|
||||||
|
Desc("c").
|
||||||
|
Limit(10).
|
||||||
|
Group("port").
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var ports = []int32{}
|
||||||
|
for _, one := range ones {
|
||||||
|
ports = append(ports, one.GetInt32("port"))
|
||||||
|
}
|
||||||
|
return ports, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NodeLoginDAO) FindFrequentGrantIds(tx *dbs.Tx, nodeClusterId int64, nsClusterId int64) ([]int64, error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
Attr("state", NodeLoginStateEnabled).
|
||||||
|
Result("JSON_EXTRACT(params, '$.grantId') as `grantId`", "COUNT(*) AS c").
|
||||||
|
Having("grantId>0").
|
||||||
|
Desc("c").
|
||||||
|
Limit(3).
|
||||||
|
Group("grantId")
|
||||||
|
if nodeClusterId > 0 {
|
||||||
|
query.Attr("role", nodeconfigs.NodeRoleNode)
|
||||||
|
query.Where("(nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
|
||||||
|
Param("clusterId", nodeClusterId)
|
||||||
|
} else if nsClusterId > 0 {
|
||||||
|
query.Attr("role", nodeconfigs.NodeRoleDNS)
|
||||||
|
query.Where("(nodeId IN (SELECT id FROM "+SharedNSNodeDAO.Table+" WHERE state=1 AND clusterId=:clusterId))").
|
||||||
|
Param("clusterId", nsClusterId)
|
||||||
|
}
|
||||||
|
ones, _, err := query.
|
||||||
|
FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var grantIds = []int64{}
|
||||||
|
for _, one := range ones {
|
||||||
|
grantIds = append(grantIds, one.GetInt64("grantId"))
|
||||||
|
}
|
||||||
|
return grantIds, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,4 +2,16 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNodeLoginDAO_FindFrequentPorts(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
ports, err := SharedNodeLoginDAO.FindFrequentPorts(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(ports)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
//
|
// NodeLogin 节点登录信息
|
||||||
type NodeLogin struct {
|
type NodeLogin struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
|
Role string `field:"role"` // 角色
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Type string `field:"type"` // 类型:ssh,agent
|
Type string `field:"type"` // 类型:ssh,agent
|
||||||
Params string `field:"params"` // 配置参数
|
Params string `field:"params"` // 配置参数
|
||||||
@@ -13,6 +14,7 @@ type NodeLogin struct {
|
|||||||
type NodeLoginOperator struct {
|
type NodeLoginOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
|
Role interface{} // 角色
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Type interface{} // 类型:ssh,agent
|
Type interface{} // 类型:ssh,agent
|
||||||
Params interface{} // 配置参数
|
Params interface{} // 配置参数
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ type Node struct {
|
|||||||
Secret string `field:"secret"` // 密钥
|
Secret string `field:"secret"` // 密钥
|
||||||
Name string `field:"name"` // 节点名
|
Name string `field:"name"` // 节点名
|
||||||
Code string `field:"code"` // 代号
|
Code string `field:"code"` // 代号
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 主集群ID
|
||||||
|
SecondaryClusterIds string `field:"secondaryClusterIds"` // 从集群ID
|
||||||
RegionId uint32 `field:"regionId"` // 区域ID
|
RegionId uint32 `field:"regionId"` // 区域ID
|
||||||
GroupId uint32 `field:"groupId"` // 分组ID
|
GroupId uint32 `field:"groupId"` // 分组ID
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
@@ -45,7 +46,8 @@ type NodeOperator struct {
|
|||||||
Secret interface{} // 密钥
|
Secret interface{} // 密钥
|
||||||
Name interface{} // 节点名
|
Name interface{} // 节点名
|
||||||
Code interface{} // 代号
|
Code interface{} // 代号
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId interface{} // 主集群ID
|
||||||
|
SecondaryClusterIds interface{} // 从集群ID
|
||||||
RegionId interface{} // 区域ID
|
RegionId interface{} // 区域ID
|
||||||
GroupId interface{} // 分组ID
|
GroupId interface{} // 分组ID
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 安装状态
|
// DecodeInstallStatus 安装状态
|
||||||
func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
||||||
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
|
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
|
||||||
return NewNodeInstallStatus(), nil
|
return NewNodeInstallStatus(), nil
|
||||||
@@ -27,7 +27,7 @@ func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
|||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 节点状态
|
// DecodeStatus 节点状态
|
||||||
func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
||||||
if len(this.Status) == 0 || this.Status == "null" {
|
if len(this.Status) == 0 || this.Status == "null" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -40,20 +40,21 @@ func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
|||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 所有的DNS线路
|
// DNSRouteCodes 所有的DNS线路
|
||||||
func (this *Node) DNSRouteCodes() (map[int64][]string, error) {
|
func (this *Node) DNSRouteCodes() map[int64][]string {
|
||||||
routes := map[int64][]string{} // domainId => routes
|
routes := map[int64][]string{} // domainId => routes
|
||||||
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
|
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
|
||||||
return routes, nil
|
return routes
|
||||||
}
|
}
|
||||||
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
|
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return map[int64][]string{}, err
|
// 忽略错误
|
||||||
|
return routes
|
||||||
}
|
}
|
||||||
return routes, nil
|
return routes
|
||||||
}
|
}
|
||||||
|
|
||||||
// DNS线路
|
// DNSRouteCodesForDomainId DNS线路
|
||||||
func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) {
|
func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) {
|
||||||
routes := map[int64][]string{} // domainId => routes
|
routes := map[int64][]string{} // domainId => routes
|
||||||
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
|
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
|
||||||
@@ -67,7 +68,7 @@ func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error)
|
|||||||
return domainRoutes, nil
|
return domainRoutes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 连接的API
|
// DecodeConnectedAPINodeIds 连接的API
|
||||||
func (this *Node) DecodeConnectedAPINodeIds() ([]int64, error) {
|
func (this *Node) DecodeConnectedAPINodeIds() ([]int64, error) {
|
||||||
apiNodeIds := []int64{}
|
apiNodeIds := []int64{}
|
||||||
if IsNotNull(this.ConnectedAPINodes) {
|
if IsNotNull(this.ConnectedAPINodes) {
|
||||||
@@ -78,3 +79,14 @@ func (this *Node) DecodeConnectedAPINodeIds() ([]int64, error) {
|
|||||||
}
|
}
|
||||||
return apiNodeIds, nil
|
return apiNodeIds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DecodeSecondaryClusterIds 从集群IDs
|
||||||
|
func (this *Node) DecodeSecondaryClusterIds() []int64 {
|
||||||
|
if len(this.SecondaryClusterIds) == 0 {
|
||||||
|
return []int64{}
|
||||||
|
}
|
||||||
|
var result = []int64{}
|
||||||
|
// 不需要处理错误
|
||||||
|
_ = json.Unmarshal([]byte(this.SecondaryClusterIds), &result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,6 +17,14 @@ const (
|
|||||||
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
|
NodeTaskTypeConfigChanged NodeTaskType = "configChanged"
|
||||||
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
NodeTaskTypeIPItemChanged NodeTaskType = "ipItemChanged"
|
||||||
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
NodeTaskTypeNodeVersionChanged NodeTaskType = "nodeVersionChanged"
|
||||||
|
|
||||||
|
// NS相关
|
||||||
|
|
||||||
|
NSNodeTaskTypeConfigChanged NodeTaskType = "nsConfigChanged"
|
||||||
|
NSNodeTaskTypeDomainChanged NodeTaskType = "nsDomainChanged"
|
||||||
|
NSNodeTaskTypeRecordChanged NodeTaskType = "nsRecordChanged"
|
||||||
|
NSNodeTaskTypeRouteChanged NodeTaskType = "nsRouteChanged"
|
||||||
|
NSNodeTaskTypeKeyChanged NodeTaskType = "nsKeyChanged"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NodeTaskDAO dbs.DAO
|
type NodeTaskDAO dbs.DAO
|
||||||
@@ -39,15 +48,16 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建单个节点任务
|
// CreateNodeTask 创建单个节点任务
|
||||||
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, clusterId int64, nodeId int64, taskType NodeTaskType) error {
|
func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, role string, clusterId int64, nodeId int64, taskType NodeTaskType) error {
|
||||||
if clusterId <= 0 || nodeId <= 0 {
|
if clusterId <= 0 || nodeId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
uniqueId := numberutils.FormatInt64(nodeId) + "@node@" + taskType
|
uniqueId := role + "@" + types.String(nodeId) + "@node@" + taskType
|
||||||
updatedAt := time.Now().Unix()
|
updatedAt := time.Now().Unix()
|
||||||
_, _, err := this.Query(tx).
|
_, _, err := this.Query(tx).
|
||||||
InsertOrUpdate(maps.Map{
|
InsertOrUpdate(maps.Map{
|
||||||
|
"role": role,
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"nodeId": nodeId,
|
"nodeId": nodeId,
|
||||||
"type": taskType,
|
"type": taskType,
|
||||||
@@ -57,25 +67,27 @@ func (this *NodeTaskDAO) CreateNodeTask(tx *dbs.Tx, clusterId int64, nodeId int6
|
|||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"updatedAt": updatedAt,
|
"updatedAt": updatedAt,
|
||||||
"isDone": 0,
|
"isDone": 0,
|
||||||
"isOk": 0,
|
"isOk": 0,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"isNotified": 0,
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建集群任务
|
// CreateClusterTask 创建集群任务
|
||||||
func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, role string, clusterId int64, taskType NodeTaskType) error {
|
||||||
if clusterId <= 0 {
|
if clusterId <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
uniqueId := numberutils.FormatInt64(clusterId) + "@cluster@" + taskType
|
uniqueId := role + "@" + types.String(clusterId) + "@cluster@" + taskType
|
||||||
updatedAt := time.Now().Unix()
|
updatedAt := time.Now().Unix()
|
||||||
_, _, err := this.Query(tx).
|
_, _, err := this.Query(tx).
|
||||||
InsertOrUpdate(maps.Map{
|
InsertOrUpdate(maps.Map{
|
||||||
|
"role": role,
|
||||||
"clusterId": clusterId,
|
"clusterId": clusterId,
|
||||||
"nodeId": 0,
|
"nodeId": 0,
|
||||||
"type": taskType,
|
"type": taskType,
|
||||||
@@ -95,15 +107,17 @@ func (this *NodeTaskDAO) CreateClusterTask(tx *dbs.Tx, clusterId int64, taskType
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分解集群任务
|
// ExtractNodeClusterTask 分解边缘节点集群任务
|
||||||
func (this *NodeTaskDAO) ExtractClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
func (this *NodeTaskDAO) ExtractNodeClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
||||||
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, clusterId, configutils.BoolStateYes)
|
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", nodeconfigs.NodeRoleNode).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
|
Param("clusterIdString", types.String(clusterId)).
|
||||||
Where("nodeId> 0").
|
Where("nodeId> 0").
|
||||||
Attr("type", taskType).
|
Attr("type", taskType).
|
||||||
Delete()
|
Delete()
|
||||||
@@ -112,13 +126,14 @@ func (this *NodeTaskDAO) ExtractClusterTask(tx *dbs.Tx, clusterId int64, taskTyp
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, nodeId := range nodeIds {
|
for _, nodeId := range nodeIds {
|
||||||
err = this.CreateNodeTask(tx, clusterId, nodeId, taskType)
|
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, clusterId, nodeId, taskType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", nodeconfigs.NodeRoleNode).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("nodeId", 0).
|
Attr("nodeId", 0).
|
||||||
Attr("type", taskType).
|
Attr("type", taskType).
|
||||||
@@ -130,9 +145,48 @@ func (this *NodeTaskDAO) ExtractClusterTask(tx *dbs.Tx, clusterId int64, taskTyp
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分解所有集群任务
|
// ExtractNSClusterTask 分解NS节点集群任务
|
||||||
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx) error {
|
func (this *NodeTaskDAO) ExtractNSClusterTask(tx *dbs.Tx, clusterId int64, taskType NodeTaskType) error {
|
||||||
|
nodeIds, err := SharedNSNodeDAO.FindAllNodeIdsMatch(tx, clusterId, true, configutils.BoolStateYes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", nodeconfigs.NodeRoleDNS).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
Param("clusterIdString", types.String(clusterId)).
|
||||||
|
Where("nodeId> 0").
|
||||||
|
Attr("type", taskType).
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, nodeId := range nodeIds {
|
||||||
|
err = this.CreateNodeTask(tx, nodeconfigs.NodeRoleDNS, clusterId, nodeId, taskType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", nodeconfigs.NodeRoleDNS).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
Attr("nodeId", 0).
|
||||||
|
Attr("type", taskType).
|
||||||
|
Delete()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtractAllClusterTasks 分解所有集群任务
|
||||||
|
func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx, role string) error {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("nodeId", 0).
|
Attr("nodeId", 0).
|
||||||
FindAll()
|
FindAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -140,36 +194,47 @@ func (this *NodeTaskDAO) ExtractAllClusterTasks(tx *dbs.Tx) error {
|
|||||||
}
|
}
|
||||||
for _, one := range ones {
|
for _, one := range ones {
|
||||||
clusterId := int64(one.(*NodeTask).ClusterId)
|
clusterId := int64(one.(*NodeTask).ClusterId)
|
||||||
err = this.ExtractClusterTask(tx, clusterId, one.(*NodeTask).Type)
|
switch role {
|
||||||
if err != nil {
|
case nodeconfigs.NodeRoleNode:
|
||||||
return err
|
err = this.ExtractNodeClusterTask(tx, clusterId, one.(*NodeTask).Type)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case nodeconfigs.NodeRoleDNS:
|
||||||
|
err = this.ExtractNSClusterTask(tx, clusterId, one.(*NodeTask).Type)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除集群所有相关任务
|
// DeleteAllClusterTasks 删除集群所有相关任务
|
||||||
func (this *NodeTaskDAO) DeleteAllClusterTasks(tx *dbs.Tx, clusterId int64) error {
|
func (this *NodeTaskDAO) DeleteAllClusterTasks(tx *dbs.Tx, role string, clusterId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除节点相关任务
|
// DeleteNodeTasks 删除节点相关任务
|
||||||
func (this *NodeTaskDAO) DeleteNodeTasks(tx *dbs.Tx, nodeId int64) error {
|
func (this *NodeTaskDAO) DeleteNodeTasks(tx *dbs.Tx, role string, nodeId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Delete()
|
Delete()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询一个节点的所有任务
|
// FindDoingNodeTasks 查询一个节点的所有任务
|
||||||
func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, nodeId int64) (result []*NodeTask, err error) {
|
func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, role string, nodeId int64) (result []*NodeTask, err error) {
|
||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("nodeId", nodeId).
|
Attr("nodeId", nodeId).
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
@@ -177,7 +242,7 @@ func (this *NodeTaskDAO) FindDoingNodeTasks(tx *dbs.Tx, nodeId int64) (result []
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改节点任务的完成状态
|
// UpdateNodeTaskDone 修改节点任务的完成状态
|
||||||
func (this *NodeTaskDAO) UpdateNodeTaskDone(tx *dbs.Tx, taskId int64, isOk bool, errorMessage string) error {
|
func (this *NodeTaskDAO) UpdateNodeTaskDone(tx *dbs.Tx, taskId int64, isOk bool, errorMessage string) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(taskId).
|
Pk(taskId).
|
||||||
@@ -188,10 +253,11 @@ func (this *NodeTaskDAO) UpdateNodeTaskDone(tx *dbs.Tx, taskId int64, isOk bool,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找正在更新的集群IDs
|
// FindAllDoingTaskClusterIds 查找正在更新的集群IDs
|
||||||
func (this *NodeTaskDAO) FindAllDoingTaskClusterIds(tx *dbs.Tx) ([]int64, error) {
|
func (this *NodeTaskDAO) FindAllDoingTaskClusterIds(tx *dbs.Tx, role string) ([]int64, error) {
|
||||||
ones, _, err := this.Query(tx).
|
ones, _, err := this.Query(tx).
|
||||||
Result("DISTINCT(clusterId) AS clusterId").
|
Result("DISTINCT(clusterId) AS clusterId").
|
||||||
|
Attr("role", role).
|
||||||
Where("(nodeId=0 OR (isDone=0 OR (isDone=1 AND isOk=0)))").
|
Where("(nodeId=0 OR (isDone=0 OR (isDone=1 AND isOk=0)))").
|
||||||
FindOnes()
|
FindOnes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -204,9 +270,10 @@ func (this *NodeTaskDAO) FindAllDoingTaskClusterIds(tx *dbs.Tx) ([]int64, error)
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询某个集群下所有的任务
|
// FindAllDoingNodeTasksWithClusterId 查询某个集群下所有的任务
|
||||||
func (this *NodeTaskDAO) FindAllDoingNodeTasksWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NodeTask, err error) {
|
func (this *NodeTaskDAO) FindAllDoingNodeTasksWithClusterId(tx *dbs.Tx, role string, clusterId int64) (result []*NodeTask, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0).
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
@@ -218,22 +285,43 @@ func (this *NodeTaskDAO) FindAllDoingNodeTasksWithClusterId(tx *dbs.Tx, clusterI
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有正在执行的任务
|
// FindAllDoingNodeIds 查询有任务的节点IDs
|
||||||
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx) (bool, error) {
|
func (this *NodeTaskDAO) FindAllDoingNodeIds(tx *dbs.Tx, role string) ([]int64, error) {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
Result("DISTINCT(nodeId) AS nodeId").
|
||||||
|
Attr("role", role).
|
||||||
|
Gt("nodeId", 0).
|
||||||
|
Attr("isDone", false).
|
||||||
|
Attr("isNotified", 0).
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result []int64
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*NodeTask).NodeId))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExistsDoingNodeTasks 检查是否有正在执行的任务
|
||||||
|
func (this *NodeTaskDAO) ExistsDoingNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0).
|
||||||
Exist()
|
Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否有错误的任务
|
// ExistsErrorNodeTasks 是否有错误的任务
|
||||||
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx) (bool, error) {
|
func (this *NodeTaskDAO) ExistsErrorNodeTasks(tx *dbs.Tx, role string) (bool, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Where("(isDone=1 AND isOk=0)").
|
Where("(isDone=1 AND isOk=0)").
|
||||||
Exist()
|
Exist()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除任务
|
// DeleteNodeTask 删除任务
|
||||||
func (this *NodeTaskDAO) DeleteNodeTask(tx *dbs.Tx, taskId int64) error {
|
func (this *NodeTaskDAO) DeleteNodeTask(tx *dbs.Tx, taskId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(taskId).
|
Pk(taskId).
|
||||||
@@ -241,17 +329,19 @@ func (this *NodeTaskDAO) DeleteNodeTask(tx *dbs.Tx, taskId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算正在执行的任务
|
// CountDoingNodeTasks 计算正在执行的任务
|
||||||
func (this *NodeTaskDAO) CountDoingNodeTasks(tx *dbs.Tx) (int64, error) {
|
func (this *NodeTaskDAO) CountDoingNodeTasks(tx *dbs.Tx, role string) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Attr("isDone", 0).
|
Attr("isDone", 0).
|
||||||
|
Attr("role", role).
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找需要通知的任务
|
// FindNotifyingNodeTasks 查找需要通知的任务
|
||||||
func (this *NodeTaskDAO) FindNotifyingNodeTasks(tx *dbs.Tx, size int64) (result []*NodeTask, err error) {
|
func (this *NodeTaskDAO) FindNotifyingNodeTasks(tx *dbs.Tx, role string, size int64) (result []*NodeTask, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
|
Attr("role", role).
|
||||||
Gt("nodeId", 0).
|
Gt("nodeId", 0).
|
||||||
Attr("isNotified", 0).
|
Attr("isNotified", 0).
|
||||||
Attr("isDone", 0).
|
Attr("isDone", 0).
|
||||||
@@ -261,7 +351,7 @@ func (this *NodeTaskDAO) FindNotifyingNodeTasks(tx *dbs.Tx, size int64) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置任务已通知
|
// UpdateTasksNotified 设置任务已通知
|
||||||
func (this *NodeTaskDAO) UpdateTasksNotified(tx *dbs.Tx, taskIds []int64) error {
|
func (this *NodeTaskDAO) UpdateTasksNotified(tx *dbs.Tx, taskIds []int64) error {
|
||||||
if len(taskIds) == 0 {
|
if len(taskIds) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -10,7 +11,7 @@ func TestNodeTaskDAO_CreateNodeTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedNodeTaskDAO.CreateNodeTask(tx, 1, 2, NodeTaskTypeConfigChanged)
|
err := SharedNodeTaskDAO.CreateNodeTask(tx, nodeconfigs.NodeRoleNode, 1, 2, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -21,7 +22,7 @@ func TestNodeTaskDAO_CreateClusterTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedNodeTaskDAO.CreateClusterTask(tx, 1, NodeTaskTypeConfigChanged)
|
err := SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, 1, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -32,7 +33,7 @@ func TestNodeTaskDAO_ExtractClusterTask(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedNodeTaskDAO.ExtractClusterTask(tx, 1, NodeTaskTypeConfigChanged)
|
err := SharedNodeTaskDAO.ExtractNodeClusterTask(tx, 1, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 节点同步任务
|
// NodeTask 节点同步任务
|
||||||
type NodeTask struct {
|
type NodeTask struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
|
Role string `field:"role"` // 节点角色
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
Type string `field:"type"` // 任务类型
|
Type string `field:"type"` // 任务类型
|
||||||
@@ -16,6 +17,7 @@ type NodeTask struct {
|
|||||||
|
|
||||||
type NodeTaskOperator struct {
|
type NodeTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
|
Role interface{} // 节点角色
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId interface{} // 集群ID
|
||||||
Type interface{} // 任务类型
|
Type interface{} // 任务类型
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ func (this *NodeThresholdDAO) FireNodeThreshold(tx *dbs.Tx, role string, nodeId
|
|||||||
body = strings.Replace(body, "${item.name}", itemName, -1)
|
body = strings.Replace(body, "${item.name}", itemName, -1)
|
||||||
body = strings.Replace(body, "${value}", fmt.Sprintf("%.2f", paramValue), -1)
|
body = strings.Replace(body, "${value}", fmt.Sprintf("%.2f", paramValue), -1)
|
||||||
}
|
}
|
||||||
err = SharedMessageDAO.CreateNodeMessage(tx, clusterId, nodeId, MessageTypeThresholdSatisfied, MessageLevelWarning, subject, body, maps.Map{}.AsJSON())
|
err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeThresholdSatisfied, MessageLevelWarning, subject, body, maps.Map{}.AsJSON())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -95,10 +96,18 @@ func (this *NodeValueDAO) ListValues(tx *dbs.Tx, role string, nodeId int64, item
|
|||||||
func (this *NodeValueDAO) ListValuesWithClusterId(tx *dbs.Tx, role string, clusterId int64, item string, key string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
|
func (this *NodeValueDAO) ListValuesWithClusterId(tx *dbs.Tx, role string, clusterId int64, item string, key string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
Attr("role", role).
|
Attr("role", role).
|
||||||
Attr("clusterId", clusterId).
|
|
||||||
Attr("item", item).
|
Attr("item", item).
|
||||||
Result("AVG(JSON_EXTRACT(value, '$." + key + "')) AS value, MIN(createdAt) AS createdAt")
|
Result("AVG(JSON_EXTRACT(value, '$." + key + "')) AS value, MIN(createdAt) AS createdAt")
|
||||||
|
|
||||||
|
switch role {
|
||||||
|
case nodeconfigs.NodeRoleNode:
|
||||||
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)")
|
||||||
|
query.Param("clusterId", clusterId).
|
||||||
|
Param("clusterIdString", types.String(clusterId))
|
||||||
|
default:
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
switch timeRange {
|
switch timeRange {
|
||||||
// TODO 支持更多的时间范围
|
// TODO 支持更多的时间范围
|
||||||
case nodeconfigs.NodeValueRangeMinute:
|
case nodeconfigs.NodeValueRangeMinute:
|
||||||
@@ -140,7 +149,6 @@ func (this *NodeValueDAO) ListValuesForUserNodes(tx *dbs.Tx, item string, key st
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ListValuesForNSNodes 列出用户节点相关的平均数据
|
// ListValuesForNSNodes 列出用户节点相关的平均数据
|
||||||
func (this *NodeValueDAO) ListValuesForNSNodes(tx *dbs.Tx, item string, key string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
|
func (this *NodeValueDAO) ListValuesForNSNodes(tx *dbs.Tx, item string, key string, timeRange nodeconfigs.NodeValueRange) (result []*NodeValue, err error) {
|
||||||
query := this.Query(tx).
|
query := this.Query(tx).
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func TestNodeValueDAO_CreateValue(t *testing.T) {
|
|||||||
m := maps.Map{
|
m := maps.Map{
|
||||||
"hello": "world12344",
|
"hello": "world12344",
|
||||||
}
|
}
|
||||||
err := dao.CreateValue(nil, nodeconfigs.NodeRoleNode, 1, "test", m.AsJSON(), time.Now().Unix())
|
err := dao.CreateValue(nil, 1, nodeconfigs.NodeRoleNode, 1, "test", m.AsJSON(), time.Now().Unix())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,25 @@ package models
|
|||||||
|
|
||||||
// NSAccessLog 域名服务访问日志
|
// NSAccessLog 域名服务访问日志
|
||||||
type NSAccessLog struct {
|
type NSAccessLog struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||||
DomainId uint32 `field:"domainId"` // 域名ID
|
DomainId uint32 `field:"domainId"` // 域名ID
|
||||||
RecordId uint32 `field:"recordId"` // 记录ID
|
RecordId uint32 `field:"recordId"` // 记录ID
|
||||||
Content string `field:"content"` // 访问数据
|
Content string `field:"content"` // 访问数据
|
||||||
RequestId string `field:"requestId"` // 请求ID
|
RequestId string `field:"requestId"` // 请求ID
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
RemoteAddr string `field:"remoteAddr"` // IP
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSAccessLogOperator struct {
|
type NSAccessLogOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId interface{} // 节点ID
|
||||||
DomainId interface{} // 域名ID
|
DomainId interface{} // 域名ID
|
||||||
RecordId interface{} // 记录ID
|
RecordId interface{} // 记录ID
|
||||||
Content interface{} // 访问数据
|
Content interface{} // 访问数据
|
||||||
RequestId interface{} // 请求ID
|
RequestId interface{} // 请求ID
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
|
RemoteAddr interface{} // IP
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSAccessLogOperator() *NSAccessLogOperator {
|
func NewNSAccessLogOperator() *NSAccessLogOperator {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package nameservers
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -127,6 +128,22 @@ func (this *NSClusterDAO) FindAllEnabledClusters(tx *dbs.Tx) (result []*NSCluste
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindAllEnabledClusterIds 获取所有集群IDs
|
||||||
|
func (this *NSClusterDAO) FindAllEnabledClusterIds(tx *dbs.Tx) ([]int64, error) {
|
||||||
|
ones, err := this.Query(tx).
|
||||||
|
State(NSClusterStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var result = []int64{}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, int64(one.(*NSCluster).Id))
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateClusterAccessLog 设置访问日志
|
// UpdateClusterAccessLog 设置访问日志
|
||||||
func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error {
|
func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -143,3 +160,16 @@ func (this *NSClusterDAO) FindClusterAccessLog(tx *dbs.Tx, clusterId int64) ([]b
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
return []byte(accessLog), err
|
return []byte(accessLog), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindClusterGrantId 查找集群的认证ID
|
||||||
|
func (this *NSClusterDAO) FindClusterGrantId(tx *dbs.Tx, clusterId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("grantId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更改
|
||||||
|
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, NSNodeTaskTypeConfigChanged)
|
||||||
|
}
|
||||||
6
internal/db/models/ns_cluster_dao_test.go
Normal file
6
internal/db/models/ns_cluster_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package models
|
||||||
|
|
||||||
// NSCluster 域名服务器集群
|
// NSCluster 域名服务器集群
|
||||||
type NSCluster struct {
|
type NSCluster struct {
|
||||||
@@ -8,6 +8,7 @@ type NSCluster struct {
|
|||||||
InstallDir string `field:"installDir"` // 安装目录
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
AccessLog string `field:"accessLog"` // 访问日志配置
|
AccessLog string `field:"accessLog"` // 访问日志配置
|
||||||
|
GrantId uint32 `field:"grantId"` // 授权ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type NSClusterOperator struct {
|
type NSClusterOperator struct {
|
||||||
@@ -17,6 +18,7 @@ type NSClusterOperator struct {
|
|||||||
InstallDir interface{} // 安装目录
|
InstallDir interface{} // 安装目录
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
AccessLog interface{} // 访问日志配置
|
AccessLog interface{} // 访问日志配置
|
||||||
|
GrantId interface{} // 授权ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNSClusterOperator() *NSClusterOperator {
|
func NewNSClusterOperator() *NSClusterOperator {
|
||||||
1
internal/db/models/ns_cluster_model_ext.go
Normal file
1
internal/db/models/ns_cluster_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package nameservers
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -133,9 +133,9 @@ func (this *NSNodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, in
|
|||||||
case configutils.BoolStateAll:
|
case configutils.BoolStateAll:
|
||||||
// 所有
|
// 所有
|
||||||
case configutils.BoolStateYes:
|
case configutils.BoolStateYes:
|
||||||
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
|
||||||
case configutils.BoolStateNo:
|
case configutils.BoolStateNo:
|
||||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||||
}
|
}
|
||||||
if len(keyword) > 0 {
|
if len(keyword) > 0 {
|
||||||
query.Where("(name LIKE :keyword)").
|
query.Where("(name LIKE :keyword)").
|
||||||
@@ -166,9 +166,9 @@ func (this *NSNodeDAO) ListAllEnabledNodesMatch(tx *dbs.Tx, clusterId int64, ins
|
|||||||
case configutils.BoolStateAll:
|
case configutils.BoolStateAll:
|
||||||
// 所有
|
// 所有
|
||||||
case configutils.BoolStateYes:
|
case configutils.BoolStateYes:
|
||||||
query.Where("JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60")
|
query.Where("(isActive=1 AND JSON_EXTRACT(status, '$.isActive') AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')<=60)")
|
||||||
case configutils.BoolStateNo:
|
case configutils.BoolStateNo:
|
||||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
query.Where("(isActive=0 OR status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||||
}
|
}
|
||||||
|
|
||||||
if clusterId > 0 {
|
if clusterId > 0 {
|
||||||
@@ -213,7 +213,7 @@ func (this *NSNodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, cluste
|
|||||||
secret := rands.String(32)
|
secret := rands.String(32)
|
||||||
|
|
||||||
// 保存API Token
|
// 保存API Token
|
||||||
err = models.SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleDNS)
|
err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleDNS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -280,7 +280,7 @@ func (this *NSNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindNodeInstallStatus 查询节点的安装状态
|
// FindNodeInstallStatus 查询节点的安装状态
|
||||||
func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*models.NodeInstallStatus, error) {
|
func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*NodeInstallStatus, error) {
|
||||||
node, err := this.Query(tx).
|
node, err := this.Query(tx).
|
||||||
Pk(nodeId).
|
Pk(nodeId).
|
||||||
Result("installStatus", "isInstalled").
|
Result("installStatus", "isInstalled").
|
||||||
@@ -295,10 +295,10 @@ func (this *NSNodeDAO) FindNodeInstallStatus(tx *dbs.Tx, nodeId int64) (*models.
|
|||||||
installStatus := node.(*NSNode).InstallStatus
|
installStatus := node.(*NSNode).InstallStatus
|
||||||
isInstalled := node.(*NSNode).IsInstalled == 1
|
isInstalled := node.(*NSNode).IsInstalled == 1
|
||||||
if len(installStatus) == 0 {
|
if len(installStatus) == 0 {
|
||||||
return models.NewNodeInstallStatus(), nil
|
return NewNodeInstallStatus(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
status := &models.NodeInstallStatus{}
|
status := &NodeInstallStatus{}
|
||||||
err = json.Unmarshal([]byte(installStatus), status)
|
err = json.Unmarshal([]byte(installStatus), status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -383,16 +383,38 @@ func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.
|
|||||||
|
|
||||||
config := &dnsconfigs.NSNodeConfig{
|
config := &dnsconfigs.NSNodeConfig{
|
||||||
Id: int64(node.Id),
|
Id: int64(node.Id),
|
||||||
|
NodeId: node.UniqueId,
|
||||||
|
Secret: node.Secret,
|
||||||
ClusterId: int64(node.ClusterId),
|
ClusterId: int64(node.ClusterId),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cluster.AccessLog) > 0 {
|
// 访问日志
|
||||||
ref := &dnsconfigs.AccessLogRef{}
|
// 全局配置
|
||||||
err = json.Unmarshal([]byte(cluster.AccessLog), ref)
|
{
|
||||||
|
globalValue, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeNSAccessLogSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.AccessLogRef = ref
|
if len(globalValue) > 0 {
|
||||||
|
var ref = &dnsconfigs.NSAccessLogRef{}
|
||||||
|
err = json.Unmarshal(globalValue, ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.AccessLogRef = ref
|
||||||
|
}
|
||||||
|
|
||||||
|
// 集群配置
|
||||||
|
if len(cluster.AccessLog) > 0 {
|
||||||
|
ref := &dnsconfigs.NSAccessLogRef{}
|
||||||
|
err = json.Unmarshal([]byte(cluster.AccessLog), ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if ref.IsPrior {
|
||||||
|
config.AccessLogRef = ref
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
@@ -406,6 +428,120 @@ func (this *NSNodeDAO) FindNodeClusterId(tx *dbs.Tx, nodeId int64) (int64, error
|
|||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindNodeActive 检查节点活跃状态
|
||||||
|
func (this *NSNodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
|
||||||
|
isActive, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Result("isActive").
|
||||||
|
FindIntCol(0)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return isActive == 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeActive 修改节点活跃状态
|
||||||
|
func (this *NSNodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
|
||||||
|
if nodeId <= 0 {
|
||||||
|
return errors.New("invalid nodeId")
|
||||||
|
}
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("isActive", isActive).
|
||||||
|
Set("statusIsNotified", false).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeConnectedAPINodes 修改当前连接的API节点
|
||||||
|
func (this *NSNodeDAO) UpdateNodeConnectedAPINodes(tx *dbs.Tx, nodeId int64, apiNodeIds []int64) error {
|
||||||
|
if nodeId <= 0 {
|
||||||
|
return errors.New("invalid nodeId")
|
||||||
|
}
|
||||||
|
|
||||||
|
op := NewNSNodeOperator()
|
||||||
|
op.Id = nodeId
|
||||||
|
|
||||||
|
if len(apiNodeIds) > 0 {
|
||||||
|
apiNodeIdsJSON, err := json.Marshal(apiNodeIds)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err)
|
||||||
|
}
|
||||||
|
op.ConnectedAPINodes = apiNodeIdsJSON
|
||||||
|
} else {
|
||||||
|
op.ConnectedAPINodes = "[]"
|
||||||
|
}
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllNotifyingInactiveNodesWithClusterId 取得某个集群所有等待通知离线离线的节点
|
||||||
|
func (this *NSNodeDAO) FindAllNotifyingInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int64) (result []*NSNode, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(NSNodeStateEnabled).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
Attr("isOn", true). // 只监控启用的节点
|
||||||
|
Attr("isInstalled", true). // 只监控已经安装的节点
|
||||||
|
Attr("isActive", false). // 当前已经离线的
|
||||||
|
Attr("statusIsNotified", false).
|
||||||
|
Result("id", "name").
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeStatusIsNotified 设置状态已经通知
|
||||||
|
func (this *NSNodeDAO) UpdateNodeStatusIsNotified(tx *dbs.Tx, nodeId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("statusIsNotified", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllNodeIdsMatch 匹配节点并返回节点ID
|
||||||
|
func (this *NSNodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, isOn configutils.BoolState) (result []int64, err error) {
|
||||||
|
query := this.Query(tx)
|
||||||
|
query.State(NSNodeStateEnabled)
|
||||||
|
if clusterId > 0 {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
|
if isOn == configutils.BoolStateYes {
|
||||||
|
query.Attr("isOn", true)
|
||||||
|
} else if isOn == configutils.BoolStateNo {
|
||||||
|
query.Attr("isOn", false)
|
||||||
|
}
|
||||||
|
query.Result("id")
|
||||||
|
ones, _, err := query.FindOnes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, one.GetInt64("id"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeInstallStatus 修改节点的安装状态
|
||||||
|
func (this *NSNodeDAO) UpdateNodeInstallStatus(tx *dbs.Tx, nodeId int64, status *NodeInstallStatus) error {
|
||||||
|
if status == nil {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("installStatus", "null").
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(status)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Pk(nodeId).
|
||||||
|
Set("installStatus", string(data)).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
|
||||||
// TODO 先什么都不做
|
// TODO 先什么都不做
|
||||||
6
internal/db/models/ns_node_dao_test.go
Normal file
6
internal/db/models/ns_node_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
44
internal/db/models/ns_node_model.go
Normal file
44
internal/db/models/ns_node_model.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// NSNode 域名服务器节点
|
||||||
|
type NSNode struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
|
Name string `field:"name"` // 节点名称
|
||||||
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
|
Status string `field:"status"` // 运行状态
|
||||||
|
UniqueId string `field:"uniqueId"` // 节点ID
|
||||||
|
Secret string `field:"secret"` // 密钥
|
||||||
|
IsUp uint8 `field:"isUp"` // 是否运行
|
||||||
|
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
|
||||||
|
InstallStatus string `field:"installStatus"` // 安装状态
|
||||||
|
InstallDir string `field:"installDir"` // 安装目录
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
IsActive uint8 `field:"isActive"` // 是否活跃
|
||||||
|
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
|
||||||
|
ConnectedAPINodes string `field:"connectedAPINodes"` // 当前连接的API节点
|
||||||
|
}
|
||||||
|
|
||||||
|
type NSNodeOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
AdminId interface{} // 管理员ID
|
||||||
|
ClusterId interface{} // 集群ID
|
||||||
|
Name interface{} // 节点名称
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Status interface{} // 运行状态
|
||||||
|
UniqueId interface{} // 节点ID
|
||||||
|
Secret interface{} // 密钥
|
||||||
|
IsUp interface{} // 是否运行
|
||||||
|
IsInstalled interface{} // 是否已安装
|
||||||
|
InstallStatus interface{} // 安装状态
|
||||||
|
InstallDir interface{} // 安装目录
|
||||||
|
State interface{} // 状态
|
||||||
|
IsActive interface{} // 是否活跃
|
||||||
|
StatusIsNotified interface{} // 活跃状态已经通知
|
||||||
|
ConnectedAPINodes interface{} // 当前连接的API节点
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNSNodeOperator() *NSNodeOperator {
|
||||||
|
return &NSNodeOperator{}
|
||||||
|
}
|
||||||
@@ -1,21 +1,20 @@
|
|||||||
package nameservers
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DecodeInstallStatus 安装状态
|
// DecodeInstallStatus 安装状态
|
||||||
func (this *NSNode) DecodeInstallStatus() (*models.NodeInstallStatus, error) {
|
func (this *NSNode) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
||||||
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
|
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
|
||||||
return models.NewNodeInstallStatus(), nil
|
return NewNodeInstallStatus(), nil
|
||||||
}
|
}
|
||||||
status := &models.NodeInstallStatus{}
|
status := &NodeInstallStatus{}
|
||||||
err := json.Unmarshal([]byte(this.InstallStatus), status)
|
err := json.Unmarshal([]byte(this.InstallStatus), status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.NewNodeInstallStatus(), err
|
return NewNodeInstallStatus(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果N秒钟没有更新状态,则认为不在运行
|
// 如果N秒钟没有更新状态,则认为不在运行
|
||||||
@@ -38,12 +38,12 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// Init 初始化
|
||||||
func (this *OriginDAO) Init() {
|
func (this *OriginDAO) Init() {
|
||||||
_ = this.DAOObject.Init()
|
_ = this.DAOObject.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableOrigin 启用条目
|
||||||
func (this *OriginDAO) EnableOrigin(tx *dbs.Tx, id int64) error {
|
func (this *OriginDAO) EnableOrigin(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -52,7 +52,7 @@ func (this *OriginDAO) EnableOrigin(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableOrigin 禁用条目
|
||||||
func (this *OriginDAO) DisableOrigin(tx *dbs.Tx, originId int64) error {
|
func (this *OriginDAO) DisableOrigin(tx *dbs.Tx, originId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(originId).
|
Pk(originId).
|
||||||
@@ -65,7 +65,7 @@ func (this *OriginDAO) DisableOrigin(tx *dbs.Tx, originId int64) error {
|
|||||||
return this.NotifyUpdate(tx, originId)
|
return this.NotifyUpdate(tx, originId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledOrigin 查找启用中的条目
|
||||||
func (this *OriginDAO) FindEnabledOrigin(tx *dbs.Tx, id int64) (*Origin, error) {
|
func (this *OriginDAO) FindEnabledOrigin(tx *dbs.Tx, id int64) (*Origin, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -77,7 +77,7 @@ func (this *OriginDAO) FindEnabledOrigin(tx *dbs.Tx, id int64) (*Origin, error)
|
|||||||
return result.(*Origin), err
|
return result.(*Origin), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据主键查找名称
|
// FindOriginName 根据主键查找名称
|
||||||
func (this *OriginDAO) FindOriginName(tx *dbs.Tx, id int64) (string, error) {
|
func (this *OriginDAO) FindOriginName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -85,7 +85,7 @@ func (this *OriginDAO) FindOriginName(tx *dbs.Tx, id int64) (string, error) {
|
|||||||
FindStringCol("")
|
FindStringCol("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建源站
|
// CreateOrigin 创建源站
|
||||||
func (this *OriginDAO) CreateOrigin(tx *dbs.Tx, adminId int64, userId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) (originId int64, err error) {
|
func (this *OriginDAO) CreateOrigin(tx *dbs.Tx, adminId int64, userId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) (originId int64, err error) {
|
||||||
op := NewOriginOperator()
|
op := NewOriginOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
@@ -139,7 +139,7 @@ func (this *OriginDAO) CreateOrigin(tx *dbs.Tx, adminId int64, userId int64, nam
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改源站
|
// UpdateOrigin 修改源站
|
||||||
func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) error {
|
func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) error {
|
||||||
if originId <= 0 {
|
if originId <= 0 {
|
||||||
return errors.New("invalid originId")
|
return errors.New("invalid originId")
|
||||||
@@ -196,7 +196,7 @@ func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, add
|
|||||||
return this.NotifyUpdate(tx, originId)
|
return this.NotifyUpdate(tx, originId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将源站信息转换为配置
|
// ComposeOriginConfig 将源站信息转换为配置
|
||||||
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64) (*serverconfigs.OriginConfig, error) {
|
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64) (*serverconfigs.OriginConfig, error) {
|
||||||
origin, err := this.FindEnabledOrigin(tx, originId)
|
origin, err := this.FindEnabledOrigin(tx, originId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -328,7 +328,7 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64) (*serverc
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *OriginDAO) NotifyUpdate(tx *dbs.Tx, originId int64) error {
|
func (this *OriginDAO) NotifyUpdate(tx *dbs.Tx, originId int64) error {
|
||||||
reverseProxyId, err := SharedReverseProxyDAO.FindReverseProxyContainsOriginId(tx, originId)
|
reverseProxyId, err := SharedReverseProxyDAO.FindReverseProxyContainsOriginId(tx, originId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -385,5 +385,15 @@ func (this *ReverseProxyDAO) NotifyUpdate(tx *dbs.Tx, reverseProxyId int64) erro
|
|||||||
if serverId > 0 {
|
if serverId > 0 {
|
||||||
return SharedServerDAO.NotifyUpdate(tx, serverId)
|
return SharedServerDAO.NotifyUpdate(tx, serverId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// locations
|
||||||
|
locationId, err := SharedHTTPLocationDAO.FindEnabledLocationIdWithReverseProxyId(tx, reverseProxyId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if locationId > 0 {
|
||||||
|
return SharedHTTPLocationDAO.NotifyUpdate(tx, locationId)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
@@ -710,21 +711,24 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
|||||||
|
|
||||||
// FindAllEnabledServersWithNode 获取节点中的所有服务
|
// FindAllEnabledServersWithNode 获取节点中的所有服务
|
||||||
func (this *ServerDAO) FindAllEnabledServersWithNode(tx *dbs.Tx, nodeId int64) (result []*Server, err error) {
|
func (this *ServerDAO) FindAllEnabledServersWithNode(tx *dbs.Tx, nodeId int64) (result []*Server, err error) {
|
||||||
// 节点所在集群
|
// 节点所在主集群
|
||||||
clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
|
clusterIds, err := SharedNodeDAO.FindEnabledAndOnNodeClusterIds(tx, nodeId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if clusterId <= 0 {
|
for _, clusterId := range clusterIds {
|
||||||
return nil, nil
|
ones, err := this.Query(tx).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
State(ServerStateEnabled).
|
||||||
|
AscPk().
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
result = append(result, one.(*Server))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Attr("clusterId", clusterId).
|
|
||||||
State(ServerStateEnabled).
|
|
||||||
AscPk().
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -772,8 +776,8 @@ func (this *ServerDAO) FindServerNodeFilters(tx *dbs.Tx, serverId int64) (isOk b
|
|||||||
return true, int64(server.ClusterId), nil
|
return true, int64(server.ClusterId), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComposeServerConfig 构造服务的Config
|
// ComposeServerConfigWithServerId 构造服务的Config
|
||||||
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverconfigs.ServerConfig, error) {
|
func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int64) (*serverconfigs.ServerConfig, error) {
|
||||||
server, err := this.FindEnabledServer(tx, serverId)
|
server, err := this.FindEnabledServer(tx, serverId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -781,9 +785,18 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
if server == nil {
|
if server == nil {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
return this.ComposeServerConfig(tx, server)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComposeServerConfig 构造服务的Config
|
||||||
|
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server) (*serverconfigs.ServerConfig, error) {
|
||||||
|
if server == nil {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
config := &serverconfigs.ServerConfig{}
|
config := &serverconfigs.ServerConfig{}
|
||||||
config.Id = serverId
|
config.Id = int64(server.Id)
|
||||||
|
config.ClusterId = int64(server.ClusterId)
|
||||||
config.Type = server.Type
|
config.Type = server.Type
|
||||||
config.IsOn = server.IsOn == 1
|
config.IsOn = server.IsOn == 1
|
||||||
config.Name = server.Name
|
config.Name = server.Name
|
||||||
@@ -792,7 +805,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// ServerNames
|
// ServerNames
|
||||||
if len(server.ServerNames) > 0 && server.ServerNames != "null" {
|
if len(server.ServerNames) > 0 && server.ServerNames != "null" {
|
||||||
serverNames := []*serverconfigs.ServerNameConfig{}
|
serverNames := []*serverconfigs.ServerNameConfig{}
|
||||||
err = json.Unmarshal([]byte(server.ServerNames), &serverNames)
|
err := json.Unmarshal([]byte(server.ServerNames), &serverNames)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -820,7 +833,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// HTTP
|
// HTTP
|
||||||
if len(server.Http) > 0 && server.Http != "null" {
|
if len(server.Http) > 0 && server.Http != "null" {
|
||||||
httpConfig := &serverconfigs.HTTPProtocolConfig{}
|
httpConfig := &serverconfigs.HTTPProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Http), httpConfig)
|
err := json.Unmarshal([]byte(server.Http), httpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -830,7 +843,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// HTTPS
|
// HTTPS
|
||||||
if len(server.Https) > 0 && server.Https != "null" {
|
if len(server.Https) > 0 && server.Https != "null" {
|
||||||
httpsConfig := &serverconfigs.HTTPSProtocolConfig{}
|
httpsConfig := &serverconfigs.HTTPSProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Https), httpsConfig)
|
err := json.Unmarshal([]byte(server.Https), httpsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -852,7 +865,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// TCP
|
// TCP
|
||||||
if len(server.Tcp) > 0 && server.Tcp != "null" {
|
if len(server.Tcp) > 0 && server.Tcp != "null" {
|
||||||
tcpConfig := &serverconfigs.TCPProtocolConfig{}
|
tcpConfig := &serverconfigs.TCPProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Tcp), tcpConfig)
|
err := json.Unmarshal([]byte(server.Tcp), tcpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -862,7 +875,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// TLS
|
// TLS
|
||||||
if len(server.Tls) > 0 && server.Tls != "null" {
|
if len(server.Tls) > 0 && server.Tls != "null" {
|
||||||
tlsConfig := &serverconfigs.TLSProtocolConfig{}
|
tlsConfig := &serverconfigs.TLSProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Tls), tlsConfig)
|
err := json.Unmarshal([]byte(server.Tls), tlsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -884,7 +897,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// Unix
|
// Unix
|
||||||
if len(server.Unix) > 0 && server.Unix != "null" {
|
if len(server.Unix) > 0 && server.Unix != "null" {
|
||||||
unixConfig := &serverconfigs.UnixProtocolConfig{}
|
unixConfig := &serverconfigs.UnixProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Unix), unixConfig)
|
err := json.Unmarshal([]byte(server.Unix), unixConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -894,7 +907,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// UDP
|
// UDP
|
||||||
if len(server.Udp) > 0 && server.Udp != "null" {
|
if len(server.Udp) > 0 && server.Udp != "null" {
|
||||||
udpConfig := &serverconfigs.UDPProtocolConfig{}
|
udpConfig := &serverconfigs.UDPProtocolConfig{}
|
||||||
err = json.Unmarshal([]byte(server.Udp), udpConfig)
|
err := json.Unmarshal([]byte(server.Udp), udpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -915,7 +928,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
// ReverseProxy
|
// ReverseProxy
|
||||||
if IsNotNull(server.ReverseProxy) {
|
if IsNotNull(server.ReverseProxy) {
|
||||||
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||||
err = json.Unmarshal([]byte(server.ReverseProxy), reverseProxyRef)
|
err := json.Unmarshal([]byte(server.ReverseProxy), reverseProxyRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -930,12 +943,31 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, serverId int64) (*serverc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WAF策略
|
||||||
|
clusterId := int64(server.ClusterId)
|
||||||
|
httpFirewallPolicyId, err := SharedNodeClusterDAO.FindClusterHTTPFirewallPolicyId(tx, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if httpFirewallPolicyId > 0 {
|
||||||
|
config.HTTPFirewallPolicyId = httpFirewallPolicyId
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缓存策略
|
||||||
|
httpCachePolicyId, err := SharedNodeClusterDAO.FindClusterHTTPCachePolicyId(tx, clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if httpCachePolicyId > 0 {
|
||||||
|
config.HTTPCachePolicyId = httpCachePolicyId
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenewServerConfig 更新服务的Config配置
|
// RenewServerConfig 更新服务的Config配置
|
||||||
func (this *ServerDAO) RenewServerConfig(tx *dbs.Tx, serverId int64, updateMd5 bool) (isChanged bool, err error) {
|
func (this *ServerDAO) RenewServerConfig(tx *dbs.Tx, serverId int64, updateMd5 bool) (isChanged bool, err error) {
|
||||||
serverConfig, err := this.ComposeServerConfig(tx, serverId)
|
serverConfig, err := this.ComposeServerConfigWithServerId(tx, serverId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -1239,11 +1271,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if oldClusterId > 0 {
|
if oldClusterId > 0 {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, oldClusterId, NodeTaskTypeConfigChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, oldClusterId, NodeTaskTypeIPItemChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, oldClusterId, NodeTaskTypeIPItemChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1254,11 +1286,11 @@ func (this *ServerDAO) UpdateUserServersClusterId(tx *dbs.Tx, userId int64, oldC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if newClusterId > 0 {
|
if newClusterId > 0 {
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, newClusterId, NodeTaskTypeConfigChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, NodeTaskTypeConfigChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = SharedNodeTaskDAO.CreateClusterTask(tx, newClusterId, NodeTaskTypeIPItemChanged)
|
err = SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, newClusterId, NodeTaskTypeIPItemChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1406,7 +1438,7 @@ func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
|||||||
if clusterId == 0 {
|
if clusterId == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeConfigChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, NodeTaskTypeConfigChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyDNSUpdate 通知DNS更新
|
// NotifyDNSUpdate 通知DNS更新
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfig(tx, 1)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -22,7 +22,7 @@ func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
|||||||
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfig(tx, 14)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
|||||||
func TestServerDAO_UpdateServerConfig(t *testing.T) {
|
func TestServerDAO_UpdateServerConfig(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfig(tx, 1)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -134,5 +134,16 @@ func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log(exist)
|
t.Log(exist)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerDAO_FindAllEnabledServersWithNode(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
servers, err := SharedServerDAO.FindAllEnabledServersWithNode(nil, 48)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, server := range servers {
|
||||||
|
t.Log("serverId:", server.Id, "clusterId:", server.ClusterId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user