From 4217a2fd0ddd9d3da5ff5af5266a8208ea8c849f Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 28 Nov 2023 11:05:39 +0800 Subject: [PATCH] [ Agent ] [ App ] - fix mysql init bugs - 2 --- agent-go/executor/AppFunction.go | 20 ++++++++++++++++++-- agent-go/executor/MySqlFunction.go | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/agent-go/executor/AppFunction.go b/agent-go/executor/AppFunction.go index d263000..1d6ee45 100644 --- a/agent-go/executor/AppFunction.go +++ b/agent-go/executor/AppFunction.go @@ -5,7 +5,9 @@ import ( "net" "os" "path/filepath" + "regexp" "sort" + "strconv" "strings" "time" ) @@ -542,7 +544,8 @@ func (op *AgentOsOperator) loadMysqlInitScript(funcArgs []string) (bool, []strin } } - sort.Strings(jackeyLoveFileList) + // sort for numeric order + sortFileNames(jackeyLoveFileList) log.InfoF("[loadMysqlInitScript] - all jackey love files are => %v", jackeyLoveFileList) // dispatch mysql execution command @@ -553,7 +556,7 @@ func (op *AgentOsOperator) loadMysqlInitScript(funcArgs []string) (bool, []strin "[loadMysqlInitScript]- ip config error !", } } - load, result := MysqlSqlFileLoad(jackeyLoveFileList) + load, result := MysqlSqlFileLoad(jackeyLoveIP, jackeyLoveFileList) if !load { return false, result } @@ -563,6 +566,19 @@ func (op *AgentOsOperator) loadMysqlInitScript(funcArgs []string) (bool, []strin } } +func sortFileNames(fileNames []string) { + re := regexp.MustCompile(`(\d+)_`) + sort.Slice(fileNames, func(i, j int) bool { + numStr1 := re.FindStringSubmatch(fileNames[i]) + numStr2 := re.FindStringSubmatch(fileNames[j]) + + num1, _ := strconv.Atoi(numStr1[1]) + num2, _ := strconv.Atoi(numStr2[1]) + + return num1 < num2 + }) +} + func (op *AgentOsOperator) checkMySQL(funcArgs []string) (bool, []string) { if !K8sCheckPodStatusTimeout("helm-mysql-0", funcArgs[0], 180) { diff --git a/agent-go/executor/MySqlFunction.go b/agent-go/executor/MySqlFunction.go index 5619946..70258d4 100644 --- a/agent-go/executor/MySqlFunction.go +++ b/agent-go/executor/MySqlFunction.go @@ -7,9 +7,9 @@ import ( "os" ) -func MysqlSqlFileLoad(jackeyLoveFileList []string) (bool, []string) { +func MysqlSqlFileLoad(jackeyLoveIp string, jackeyLoveFileList []string) (bool, []string) { - dsn := "root:QzfXQhd3bQ@tcp(127.0.0.1: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)