[ Agent ] [ App ] - add mysql init

This commit is contained in:
zeaslity
2023-11-27 11:09:44 +08:00
parent 95a2da2059
commit 2b53a2d62a
4 changed files with 154 additions and 17 deletions

View File

@@ -3,11 +3,16 @@ package executor
import ( import (
"fmt" "fmt"
"net" "net"
"os"
"path/filepath"
"sort"
"strings"
"time" "time"
) )
type OctopusFunc interface { type OctopusFunc interface {
Command(baseFuncName string, funcArgs ...string) []string Command(baseFuncName string, funcArgs ...string) []string
Exec(baseFuncName string, funcArgs ...string) (bool, []string) Exec(baseFuncName string, funcArgs ...string) (bool, []string)
Deploy(appFuncName string, funcArgs ...string) (bool, []string) Deploy(appFuncName string, funcArgs ...string) (bool, []string)
@@ -484,7 +489,75 @@ func (op *AgentOsOperator) deployMySQL(funcArgs []string) (bool, []string) {
} }
func (op *AgentOsOperator) loadMysqlInitScript(funcArgs []string) (bool, []string) { func (op *AgentOsOperator) loadMysqlInitScript(funcArgs []string) (bool, []string) {
return true, nil
// download offline sql list
if len(funcArgs) <= 7 {
return false, []string{
"[loadMysqlInitScript]- MySQL初始化参数有误 无法进行初始化",
}
}
jackeyLove := funcArgs[8]
if !strings.HasSuffix(jackeyLove, "tar.gz") {
return false, []string{
"[loadMysqlInitScript]- jackeyLove 有误!",
}
}
jackeyLoveLocalPrefix := "/root/wdd/jackeylove/"
BasicCreateFolder(jackeyLoveLocalPrefix)
ok, resultLog := BasicDownloadFile(op.OssOfflinePrefix+"jackeyLove", jackeyLoveLocalPrefix+jackeyLove)
if !ok {
return false, append(resultLog, "[loadMysqlInitScript]- jackeyLove 下载失败!")
}
// unzip
if !PureResultSingleExecute([]string{
"tar",
"-vxf",
jackeyLoveLocalPrefix + jackeyLove,
"-C",
jackeyLoveLocalPrefix,
}) {
return false, []string{
"[loadMysqlInitScript]- jackeyLove unzip error ",
}
}
// list all sql file and sort and convert to []string
files, err := os.ReadDir(jackeyLoveLocalPrefix)
if err != nil {
return false, []string{
"[loadMysqlInitScript]- read unzipped jackeylove error ",
}
}
var jackeyLoveFileList []string
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".sql" {
jackeyLoveFileList = append(jackeyLoveFileList, file.Name())
}
}
sort.Strings(jackeyLoveFileList)
log.InfoF("[loadMysqlInitScript] - all jackey love files are => %v", jackeyLoveFileList)
// dispatch mysql execution command
jackeyLoveIP := funcArgs[0]
parseIP := net.ParseIP(jackeyLoveIP)
if parseIP == nil {
return false, []string{
"[loadMysqlInitScript]- ip config error ",
}
}
load, result := MysqlSqlFileLoad(jackeyLoveFileList)
if !load {
return false, result
}
return true, []string{
"[loadMysqlInitScript] - execute success !",
}
} }
func (op *AgentOsOperator) checkMySQL(funcArgs []string) (bool, []string) { func (op *AgentOsOperator) checkMySQL(funcArgs []string) (bool, []string) {
@@ -520,7 +593,7 @@ func (op *AgentOsOperator) deployMiddlewares(funcArgs []string) (bool, []string)
ip := net.ParseIP(funcArgs[0]) ip := net.ParseIP(funcArgs[0])
if ip == nil { if ip == nil {
return false, append(result, return false, append(result,
"ip args error !") "[deployMiddlewares] - ip args error !")
} }
if !BasicReplace(k8sMiddlewaresYamlFile, "A1C2IP", funcArgs[0]) { if !BasicReplace(k8sMiddlewaresYamlFile, "A1C2IP", funcArgs[0]) {
result = append(result, "替换A1C2IP信息") result = append(result, "替换A1C2IP信息")
@@ -534,7 +607,7 @@ func (op *AgentOsOperator) deployMiddlewares(funcArgs []string) (bool, []string)
// 替换版本号 // 替换版本号
if !BasicReplace(k8sMiddlewaresYamlFile, "KIMMY", funcArgs[6]) { if !BasicReplace(k8sMiddlewaresYamlFile, "KIMMY", funcArgs[6]) {
log.WarnF("[deployPVC] - pvc config version replace error , expected => %s", funcArgs[6]) log.WarnF("[deployMiddlewares] - pvc config version replace error , expected => %s", funcArgs[6])
} }
// 启动服务 // 启动服务
@@ -553,14 +626,14 @@ func (op *AgentOsOperator) deployMiddlewares(funcArgs []string) (bool, []string)
for _, podName := range podNameList { for _, podName := range podNameList {
if !K8sCheckPodStatusTimeout(podName, funcArgs[1], 180) { if !K8sCheckPodStatusTimeout(podName, funcArgs[1], 180) {
return false, []string{ return false, []string{
fmt.Sprintf("Namepsace [%s] Pod [%s] 启动失败!", funcArgs[1], podName), fmt.Sprintf("[deployMiddlewares] - Namepsace [%s] Pod [%s] 启动失败!", funcArgs[1], podName),
} }
} }
} }
// 成功启动 // 成功启动
return true, append(podNameList, return true, append(podNameList,
"中间件部署成功!", "[deployMiddlewares] - 中间件部署成功!",
) )
} }

View File

@@ -0,0 +1,74 @@
package executor
import (
"bufio"
"database/sql"
"fmt"
"os"
)
func MysqlSqlFileLoad(jackeyLoveFileList []string) (bool, []string) {
dsn := "root:QzfXQhd3bQ@tcp(127.0.0.1:33306)"
db, err := sql.Open("mysql", dsn)
if err != nil {
errConnect := fmt.Sprintf("[MysqlSqlFileLoad]- mysql connection error ! please check ! => %s ", dsn)
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,
}
}
for _, jackeyLoveFile := range jackeyLoveFileList {
// 打开 SQL 文件
jackeyLove, err := os.Open(jackeyLoveFile)
if err != nil {
log.ErrorF("[MysqlSqlFileLoad] - failed to load jackeyLoveFile => %s", jackeyLove)
}
defer jackeyLove.Close()
// 逐行读取 SQL 文件并执行
scanner := bufio.NewScanner(jackeyLove)
for scanner.Scan() {
sqlStatement := scanner.Text()
// 这里可以添加逻辑来忽略空行或注释行
if sqlStatement == "" || sqlStatement[:2] == "--" {
continue
}
// 执行 SQL 语句
_, err := db.Exec(sqlStatement)
if err != nil {
executeError := fmt.Sprintf("[MysqlSqlFileLoad] - jackeyLoveFile %s 执行出错: %s, 错误信息: %s", jackeyLove.Name(), sqlStatement, err.Error())
log.Error(executeError)
return false, []string{
executeError,
}
}
}
// 检查扫描过程中是否有错误
if err := scanner.Err(); err != nil {
executeError := fmt.Sprintf("[MysqlSqlFileLoad] - jackeyLoveFile %s 文件加载错误! 错误信息: %s", jackeyLove.Name(), err.Error())
log.Error(executeError)
return false, []string{
executeError,
}
}
log.DebugF("[MysqlSqlFileLoad] - jackeyLoveFile %s execute success !", jackeyLove.Name())
}
return true, append(jackeyLoveFileList, "[MysqlSqlFileLoad] all file loaded !")
}

View File

@@ -8,7 +8,6 @@ require (
github.com/panjf2000/ants/v2 v2.7.2 github.com/panjf2000/ants/v2 v2.7.2
github.com/shirou/gopsutil/v3 v3.23.3 github.com/shirou/gopsutil/v3 v3.23.3
github.com/spf13/viper v1.15.0 github.com/spf13/viper v1.15.0
github.com/streadway/amqp v1.0.0
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.26.2 k8s.io/apimachinery v0.26.2
@@ -23,16 +22,6 @@ require (
github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // 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/gogo/protobuf v1.3.2 // indirect
github.com/goharbor/harbor/src v0.0.0-20230220075213-6015b3efa7d0 // indirect github.com/goharbor/harbor/src v0.0.0-20230220075213-6015b3efa7d0 // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect

View File

@@ -23,7 +23,8 @@ public class AppFuncScheduler {
CoreProjectService coreProjectService; CoreProjectService coreProjectService;
/** /**
* 参数固定顺序为 A1C2IP SUPREME N1C2IP A1C1IP A1C1JS M2D2IP KIMMY * 参数固定顺序为 A1C2IP SUPREME N1C2IP A1C1IP A1C1JS M2D2IP KIMMY JACLOVE
* 1 2 3 4 5 6 7 8
* *
* @param projectDeployContext * @param projectDeployContext
* @param projectInfoPO * @param projectInfoPO