[ Cmii ] [ Octopus ] - 优化项目结构

This commit is contained in:
zeaslity
2024-03-19 16:04:09 +08:00
committed by zeaslity
parent 4ca8d77e74
commit 6b4616690c
80 changed files with 979 additions and 1025 deletions

View File

@@ -1,90 +0,0 @@
// Copyright 2013-2022 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package assert provides helper functions for testing.
package assert
import (
"fmt"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
// skip defines the default call depth
const skip = 2
// Equal asserts that got and want are equal as defined by
// reflect.DeepEqual. The test fails with msg if they are not equal.
func Equal(t *testing.T, got, want interface{}, msg ...string) {
if x := equal(2, got, want, msg...); x != "" {
fmt.Println(x)
t.Fail()
}
}
func equal(skip int, got, want interface{}, msg ...string) string {
if !reflect.DeepEqual(got, want) {
return fail(skip, "got %v want %v %s", got, want, strings.Join(msg, " "))
}
return ""
}
// Panic asserts that function fn() panics.
// It assumes that recover() either returns a string or
// an error and fails if the message does not match
// the regular expression in 'matches'.
func Panic(t *testing.T, fn func(), matches string) {
if x := doesPanic(2, fn, matches); x != "" {
fmt.Println(x)
t.Fail()
}
}
func doesPanic(skip int, fn func(), expr string) (err string) {
defer func() {
r := recover()
if r == nil {
err = fail(skip, "did not panic")
return
}
var v string
switch r.(type) {
case error:
v = r.(error).Error()
case string:
v = r.(string)
}
err = matches(skip, v, expr)
}()
fn()
return ""
}
// Matches asserts that a value matches a given regular expression.
func Matches(t *testing.T, value, expr string) {
if x := matches(2, value, expr); x != "" {
fmt.Println(x)
t.Fail()
}
}
func matches(skip int, value, expr string) string {
ok, err := regexp.MatchString(expr, value)
if err != nil {
return fail(skip, "invalid pattern %q. %s", expr, err)
}
if !ok {
return fail(skip, "got %s which does not match %s", value, expr)
}
return ""
}
func fail(skip int, format string, args ...interface{}) string {
_, file, line, _ := runtime.Caller(skip)
return fmt.Sprintf("\t%s:%d: %s\n", filepath.Base(file), line, fmt.Sprintf(format, args...))
}

View File

@@ -1,55 +0,0 @@
// Copyright 2013-2022 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package assert
import "testing"
func TestEqualEquals(t *testing.T) {
if got, want := equal(2, "a", "a"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestEqualFails(t *testing.T) {
if got, want := equal(2, "a", "b"), "\tassert_test.go:16: got a want b \n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanics(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, ""), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndMatches(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "foo"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndDoesNotMatch(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "bar"), "\tassert.go:62: got foo which does not match bar\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndDoesNotPanic(t *testing.T) {
if got, want := doesPanic(2, func() {}, "bar"), "\tassert.go:65: did not panic\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestMatchesMatches(t *testing.T) {
if got, want := matches(2, "aaa", "a"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestMatchesDoesNotMatch(t *testing.T) {
if got, want := matches(2, "aaa", "b"), "\tassert_test.go:52: got aaa which does not match b\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

View File

@@ -2,7 +2,7 @@ package executor
import (
"testing"
"wdd.io/agent-go/assert"
"wdd.io/agent-common/assert"
"wdd.io/agent-go/register"
)

View File

@@ -1,9 +1,9 @@
package executor
import (
"github.com/magiconair/properties/assert"
"strconv"
"testing"
"wdd.io/agent-common/assert"
)
var emptyFilePath = "/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123"

View File

@@ -7,7 +7,7 @@ import (
"os/exec"
"strconv"
"strings"
logger2 "wdd.io/agent-go/logger"
"wdd.io/agent-common/logger"
)
type ExecutionMessage struct {
@@ -21,7 +21,7 @@ type ExecutionMessage struct {
ResultKey string `json:"resultKey"`
}
var log = logger2.Log
var log = logger.Log
var AgentOsOperatorCache = &AgentOsOperator{}

View File

@@ -3,7 +3,7 @@ package g
import (
"github.com/panjf2000/ants/v2"
"github.com/spf13/viper"
logger2 "wdd.io/agent-go/logger"
"wdd.io/agent-common/logger"
)
type Global struct {
@@ -27,7 +27,7 @@ const (
var pool, _ = ants.NewPool(
20,
ants.WithNonblocking(false),
ants.WithLogger(logger2.Log),
ants.WithLogger(logger.Log),
ants.WithMaxBlockingTasks(10),
ants.WithDisablePurge(true),
ants.WithPreAlloc(true),

View File

@@ -1,24 +1,22 @@
module wdd.io/agent-go
go 1.21
toolchain go1.21.5
go 1.22.1
require (
github.com/magiconair/properties v1.8.7
github.com/mittwald/goharbor-client/v5 v5.5.3
github.com/panjf2000/ants/v2 v2.7.2
github.com/shirou/gopsutil/v3 v3.23.3
github.com/spf13/viper v1.15.0
github.com/streadway/amqp v1.1.0
go.uber.org/zap v1.24.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
wdd.io/agent-common v0.0.0
)
replace wdd.io/agent-common => ../agent-common
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -48,6 +46,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -70,8 +69,8 @@ require (
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
@@ -82,6 +81,7 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect

View File

@@ -43,8 +43,6 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@@ -384,15 +382,12 @@ go.opentelemetry.io/otel/sdk v1.11.1 h1:F7KmQgoHljhUuJyA+9BiU+EkJfyX5nVVF4wyzWZp
go.opentelemetry.io/otel/sdk v1.11.1/go.mod h1:/l3FE4SupHJ12TduVjUkZtlfFqDCQJlOlithYrdktys=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=

View File

@@ -1,85 +0,0 @@
package logger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger struct represents a zap-based logger.
type Logger struct {
*zap.Logger
}
var Log, _ = NewLogger()
// NewLogger creates a new Logger instance.
func NewLogger() (*Logger, error) {
config := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
OutputPaths: []string{"stdout"}, // 输出到控制台
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
//CallerKey: "caller",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
//EncodeCaller: zapcore.ShortCallerEncoder,
},
Development: true,
}
logger, err := config.Build()
if err != nil {
return nil, err
}
return &Logger{logger}, nil
}
func (l *Logger) Printf(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf("%s ==> %v", msg, args))
}
// Debug logs a debug message.
func (l *Logger) Debug(msg string, fields ...zap.Field) {
l.Logger.Debug(msg, fields...)
}
func (l *Logger) DebugF(msg string, args ...interface{}) {
l.Logger.Debug(fmt.Sprintf(msg, args...))
}
// Info logs an info message.
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.Logger.Info(msg, fields...)
}
// InfoF logs an info message with format
func (l *Logger) InfoF(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(msg, args...))
}
// Warn logs a warning message.
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.Logger.Warn(msg, fields...)
}
func (l *Logger) WarnF(msg string, args ...interface{}) {
l.Logger.Warn(fmt.Sprintf(msg, args...))
}
// Error logs an error message.
func (l *Logger) ErrorF(msg string, args ...interface{}) {
l.Logger.Error(fmt.Sprintf(msg, args...))
}
// Fatal logs a fatal message and exits the program with a non-zero status code.
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
l.Logger.Fatal(msg, fields...)
}

View File

@@ -3,12 +3,12 @@ package main
import (
"flag"
"fmt"
"wdd.io/agent-common/logger"
"wdd.io/agent-go/g"
logger2 "wdd.io/agent-go/logger"
"wdd.io/agent-go/register"
)
var log = logger2.Log
var log = logger.Log
func main() {

View File

@@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"strings"
"wdd.io/agent-common/utils"
"wdd.io/agent-go/executor"
"wdd.io/agent-go/g"
"wdd.io/agent-go/status"
"wdd.io/agent-go/utils"
)
type IOctopusMessage interface {

View File

@@ -5,8 +5,8 @@ import (
"github.com/streadway/amqp"
"strings"
"sync"
"wdd.io/agent-common/logger"
"wdd.io/agent-go/g"
logger2 "wdd.io/agent-go/logger"
)
type RabbitMQ interface {
@@ -45,7 +45,7 @@ type ConnectProperty struct {
TopicKey string
}
var log = logger2.Log
var log = logger.Log
// 定义全局唯一的 Singleton 实例
var instance *amqp.Connection

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"testing"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
func TestGetDiskStatus(t *testing.T) {

View File

@@ -3,7 +3,7 @@ package status
import (
"github.com/shirou/gopsutil/v3/docker"
"strings"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
type DockerMetric struct {

View File

@@ -2,7 +2,7 @@ package status
import (
"testing"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
func TestGetHostInfo(t *testing.T) {

View File

@@ -10,7 +10,7 @@ import (
"strconv"
"strings"
"time"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
type NetworkMetric struct {

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"testing"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
func TestMatchNetInterfaceRight(t *testing.T) {

View File

@@ -3,20 +3,20 @@ package status
import (
"fmt"
"time"
"wdd.io/agent-common/logger"
"wdd.io/agent-go/g"
logger2 "wdd.io/agent-go/logger"
)
var log = logger2.Log
var log = logger.Log
var pool = g.G.P
type StatusMessage struct {
/**
* which kind of status should be return
"PING";
* METRIC => short time message
* ALL => all agent status message
* */
* which kind of status should be return
"PING";
* METRIC => short time message
* ALL => all agent status message
* */
StatusType string `json:"statusType,omitempty"`
MetricRepeatCount int `json:"metricRepeatCount,omitempty"`
metricRepeatPinch int `json:"metricRepeatPinch,omitempty"`

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"testing"
"time"
"wdd.io/agent-go/utils"
"wdd.io/agent-common/utils"
)
func TestConvertToFormat(t *testing.T) {

View File

@@ -1,22 +0,0 @@
package utils
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
func MaxInt32(x, y int32) int32 {
if x > y {
return x
}
return y
}
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}

View File

@@ -1,49 +0,0 @@
package utils
import (
"encoding/json"
"fmt"
"wdd.io/agent-go/logger"
)
var log = logger.Log
func BeautifulPrint(object interface{}) {
bytes, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
}
fmt.Println()
fmt.Println(string(bytes))
fmt.Println()
}
func BeautifulPrintToString(object interface{}) string {
bytes, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
}
return string(bytes)
}
func BeautifulPrintListWithTitle(contend []string, title string) {
fmt.Println()
fmt.Println(fmt.Sprintf("content tile is => %s", title))
for _, line := range contend {
bytes, _ := json.MarshalIndent(line, "", " ")
fmt.Println(string(bytes))
}
fmt.Println("---------- end -----------")
}
func SplitLinePrint() {
fmt.Println()
fmt.Println()
fmt.Println()
}

View File

@@ -1,29 +0,0 @@
package utils
import (
"time"
)
// ParseDateTimeTime 输出系统时间的格式为"2006-01-02 15:04:05"形式的时间字符串
func ParseDateTimeTime() string {
now := time.Now()
/*loc := time.FixedZone("UTC+8", 8*60*60) // 创建东八区时区对象
localTime := now.In(loc) // 转换为东八区时间*/
return now.Format(time.DateTime)
}
// ParseISOLocalDateTime 时间格式为2023-08-11T10:48:15+08:00
func ParseISOLocalDateTime() string {
now := time.Now()
return now.Format(time.RFC3339)
}
func TimeSplitFormatString() string {
now := time.Now()
formattedTime := now.Format("2006-01-02-15-04-05")
return formattedTime
}