From 7d6a6d50a0ec060589a16e55ea917355b23cf21d Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 28 Nov 2023 11:30:44 +0800 Subject: [PATCH] [ Agent ] [ App ] - fix mysql init bugs - 3 --- agent-go/executor/HarborExecutor.go | 2 +- agent-go/executor/MySqlFunction.go | 32 ++++++++++++++++++++++--- agent-go/executor/MySqlFunction_test.go | 13 ++++++++++ agent-go/go.mod | 14 ++++++++++- agent-go/go.sum | 6 +++-- 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 agent-go/executor/MySqlFunction_test.go diff --git a/agent-go/executor/HarborExecutor.go b/agent-go/executor/HarborExecutor.go index 908b0f8..cb9bf55 100644 --- a/agent-go/executor/HarborExecutor.go +++ b/agent-go/executor/HarborExecutor.go @@ -392,7 +392,7 @@ func (hOp *HarborOperator) SyncStatusExec(funcArgs []string) (bool, []string) { log.InfoF("[sync status]- status are => %v", string(bytes)) // report status return false, []string{ - fmt.Sprintf("[sync status] - not complete ! progress is => %s %", + fmt.Sprintf("[sync status] - not complete ! progress is => %s %%", strconv.FormatFloat(float64(execution.Succeed)/float64(execution.Total)*100, 'f', 2, 64)), } } diff --git a/agent-go/executor/MySqlFunction.go b/agent-go/executor/MySqlFunction.go index 70258d4..2122032 100644 --- a/agent-go/executor/MySqlFunction.go +++ b/agent-go/executor/MySqlFunction.go @@ -4,15 +4,16 @@ import ( "bufio" "database/sql" "fmt" + _ "github.com/go-sql-driver/mysql" "os" ) func MysqlSqlFileLoad(jackeyLoveIp string, jackeyLoveFileList []string) (bool, []string) { - dsn := "root:QzfXQhd3bQ@tcp(" + jackeyLoveIp + ":33306)" + dsn := "root:QzfXQhd3bQ@tcp(" + jackeyLoveIp + ":33306)/" db, err := sql.Open("mysql", dsn) if err != nil { - errConnect := fmt.Sprintf("[MysqlSqlFileLoad]- mysql connection error ! please check ! => %s ", dsn) + errConnect := fmt.Sprintf("[MysqlSqlFileLoad]- mysql connection error ! please check ! => %s error is %s ", dsn, err.Error()) log.Error(errConnect) return false, []string{ errConnect, @@ -34,7 +35,7 @@ func MysqlSqlFileLoad(jackeyLoveIp string, jackeyLoveFileList []string) (bool, [ // 打开 SQL 文件 jackeyLove, err := os.Open(jackeyLoveFile) if err != nil { - log.ErrorF("[MysqlSqlFileLoad] - failed to load jackeyLoveFile => %s", jackeyLove) + log.ErrorF("[MysqlSqlFileLoad] - failed to load jackeyLoveFile => %s", jackeyLove.Name()) } defer jackeyLove.Close() @@ -71,3 +72,28 @@ func MysqlSqlFileLoad(jackeyLoveIp string, jackeyLoveFileList []string) (bool, [ return true, append(jackeyLoveFileList, "[MysqlSqlFileLoad] all file loaded !") } + +func MySqlConnection(jackeyLoveIp string, jackeyLovePort string) (bool, []string) { + + dsn := "root:QzfXQhd3bQ@tcp(" + jackeyLoveIp + ":33306)/" + db, err := sql.Open("mysql", dsn) + if err != nil { + errConnect := fmt.Sprintf("[MysqlSqlFileLoad]- mysql connection error ! please check ! => %s error is %s ", dsn, err.Error()) + log.Error(errConnect) + return false, []string{ + errConnect, + } + } + defer db.Close() + + // 确保数据库连接是有效的 + if err := db.Ping(); err != nil { + errConnect := "[MysqlSqlFileLoad]- mysql ping error ! please check !" + log.Error(errConnect) + return false, []string{ + errConnect, + } + } + + return true, nil +} diff --git a/agent-go/executor/MySqlFunction_test.go b/agent-go/executor/MySqlFunction_test.go new file mode 100644 index 0000000..2cbedf2 --- /dev/null +++ b/agent-go/executor/MySqlFunction_test.go @@ -0,0 +1,13 @@ +package executor + +import "testing" + +func TestMySqlConnection(t *testing.T) { + + connection, strings := MySqlConnection("10.250.0.126", "33306") + if !connection { + t.Error(strings) + } + + println("success!") +} diff --git a/agent-go/go.mod b/agent-go/go.mod index 424d4f2..4716417 100644 --- a/agent-go/go.mod +++ b/agent-go/go.mod @@ -3,13 +3,16 @@ module agent-go go 1.18 require ( + github.com/go-sql-driver/mysql v1.7.1 github.com/magiconair/properties v1.8.7 github.com/mittwald/goharbor-client/v5 v5.4.2 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.v3 v3.0.1 + k8s.io/api v0.25.2 k8s.io/apimachinery v0.26.2 k8s.io/client-go v0.25.2 ) @@ -22,6 +25,16 @@ require ( github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect + github.com/go-openapi/analysis v0.21.4 // indirect + github.com/go-openapi/errors v0.20.3 // indirect + github.com/go-openapi/jsonpointer v0.19.6 // indirect + github.com/go-openapi/jsonreference v0.20.2 // indirect + github.com/go-openapi/loads v0.21.2 // indirect + github.com/go-openapi/runtime v0.25.0 // indirect + github.com/go-openapi/spec v0.20.8 // indirect + github.com/go-openapi/strfmt v0.21.3 // indirect + github.com/go-openapi/swag v0.22.3 // indirect + github.com/go-openapi/validate v0.22.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/goharbor/harbor/src v0.0.0-20230220075213-6015b3efa7d0 // indirect github.com/golang/protobuf v1.5.2 // indirect @@ -68,7 +81,6 @@ require ( 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/api v0.25.2 // indirect k8s.io/klog/v2 v2.90.1 // indirect k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect diff --git a/agent-go/go.sum b/agent-go/go.sum index 9370750..e426b2a 100644 --- a/agent-go/go.sum +++ b/agent-go/go.sum @@ -115,6 +115,8 @@ github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-openapi/validate v0.22.1 h1:G+c2ub6q47kfX1sOBLwIQwzBVt8qmOAARyo/9Fqs9NU= github.com/go-openapi/validate v0.22.1/go.mod h1:rjnrwK57VJ7A8xqfpAOEKRH8yQSGUriMu5/zuPSQ1hg= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= @@ -318,8 +320,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= -github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= -github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= +github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM= +github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=