[Agent] [Executor] 优化Functional Executor
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -81,60 +80,6 @@ func (op *AgentOsOperator) Deploy(appFuncName string, funcArgs ...string) (bool,
|
||||
return resultOK, result
|
||||
}
|
||||
|
||||
// 通用命令脚本
|
||||
|
||||
// BaseCommandExists 判定命令是否存在
|
||||
func BaseCommandExists(commandName string) bool {
|
||||
|
||||
cmd := exec.Command("command", "-v", commandName)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.DebugF("指令 %s 不存在", commandName)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// BaseCommandExistsBatch 判定批量命令是否存在
|
||||
func BaseCommandExistsBatch(commandNameList []string) (bool, string) {
|
||||
|
||||
for _, commandName := range commandNameList {
|
||||
if !BaseCommandExists(commandName) {
|
||||
return false, commandName
|
||||
}
|
||||
}
|
||||
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// BaseReplace 基础替换命令
|
||||
func BaseReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
cmd := exec.Command("sed", "-i", "s/"+origin+"/"+replace+"/g", filename)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.DebugF("文件 %s [%s] => [%s] 错误!", filename, origin, replace)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func BaseFileExists(filename string) bool {
|
||||
|
||||
cmd := exec.Command("test", "-f", filename)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.DebugF("文件 %s 不存在!", filename)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// 通用命令脚本
|
||||
|
||||
func (op *AgentOsOperator) deployMinio(funcArgs []string) (bool, []string) {
|
||||
minioTemplateFileName := "minio-docker-compose.yaml"
|
||||
result := append(AppExecuteErrorLogPrefix, "部署MINIO")
|
||||
@@ -142,7 +87,7 @@ func (op *AgentOsOperator) deployMinio(funcArgs []string) (bool, []string) {
|
||||
// 逻辑为接收执行,但是会报错
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"docker-compose",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -162,7 +107,7 @@ func (op *AgentOsOperator) deployMinio(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(minioTemplateFileName, "A1C2IP", funcArgs[0]) {
|
||||
if !BasicReplace(minioTemplateFileName, "A1C2IP", funcArgs[0]) {
|
||||
result = append(result, "替换IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -194,7 +139,7 @@ func (op *AgentOsOperator) deployNFS(funcArgs []string) (bool, []string) {
|
||||
nfsDataPath := "/nfsdata"
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -220,7 +165,7 @@ func (op *AgentOsOperator) deployNFS(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(nfsTemplateFile, "N1C2IP", funcArgs[0]) {
|
||||
if !BasicReplace(nfsTemplateFile, "N1C2IP", funcArgs[0]) {
|
||||
result = append(result, "替换IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -248,7 +193,7 @@ func (op *AgentOsOperator) testNFS(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "测试NFS部署")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -266,7 +211,7 @@ func (op *AgentOsOperator) testNFS(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(nfsTemplateFile, "N1C2IP", funcArgs[0]) {
|
||||
if !BasicReplace(nfsTemplateFile, "N1C2IP", funcArgs[0]) {
|
||||
result = append(result, "替换IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -283,7 +228,7 @@ func (op *AgentOsOperator) testNFS(funcArgs []string) (bool, []string) {
|
||||
}
|
||||
|
||||
// 测试文件是否存在
|
||||
if !BaseFileExists("/nfsdata/test-pod*/NFS-CREATE-SUCCESS") {
|
||||
if !BasicFileExists("/nfsdata/test-pod*/NFS-CREATE-SUCCESS") {
|
||||
result = append(result, "NFS 测试功能 异常!!")
|
||||
return false, result
|
||||
}
|
||||
@@ -300,7 +245,7 @@ func (op *AgentOsOperator) deployPVC(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署PVC")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -318,7 +263,7 @@ func (op *AgentOsOperator) deployPVC(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(pvcTemplateFile, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(pvcTemplateFile, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -346,7 +291,7 @@ func (op *AgentOsOperator) deployMySQL(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 MySQL !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -364,11 +309,11 @@ func (op *AgentOsOperator) deployMySQL(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(mysqlTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(mysqlTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(mysqlTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(mysqlTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -421,7 +366,7 @@ func (op *AgentOsOperator) deployMiddlewares(funcArgs []string) (bool, []string)
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 所有的中间件 !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -439,11 +384,11 @@ func (op *AgentOsOperator) deployMiddlewares(funcArgs []string) (bool, []string)
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(middlewaresTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(middlewaresTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(middlewaresTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(middlewaresTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -472,7 +417,7 @@ func (op *AgentOsOperator) deployRedis(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 redis !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -490,11 +435,11 @@ func (op *AgentOsOperator) deployRedis(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(redisTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(redisTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(redisTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(redisTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -523,7 +468,7 @@ func (op *AgentOsOperator) deployIngress(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 Ingress !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -541,15 +486,15 @@ func (op *AgentOsOperator) deployIngress(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(ingressTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(ingressTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(ingressTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(ingressTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(ingressTemplate, "A1C1JS", funcArgs[1]) {
|
||||
if !BasicReplace(ingressTemplate, "A1C1JS", funcArgs[1]) {
|
||||
result = append(result, "替换A1C1JS信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -577,7 +522,7 @@ func (op *AgentOsOperator) deployFront(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 前端服务 !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -595,11 +540,11 @@ func (op *AgentOsOperator) deployFront(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(fontTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(fontTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(fontTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(fontTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -628,7 +573,7 @@ func (op *AgentOsOperator) initMinio(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "进行MINIO初始化服务 !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -646,15 +591,15 @@ func (op *AgentOsOperator) initMinio(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(initMinioTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(initMinioTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(initMinioTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(initMinioTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(initMinioTemplate, "M2D2IP", funcArgs[1]) {
|
||||
if !BasicReplace(initMinioTemplate, "M2D2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -683,7 +628,7 @@ func (op *AgentOsOperator) deploySRS(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "开始部署SRS服务!")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -701,19 +646,19 @@ func (op *AgentOsOperator) deploySRS(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(srsTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(srsTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(srsTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(srsTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(srsTemplate, "A1C1JS", funcArgs[1]) {
|
||||
if !BasicReplace(srsTemplate, "A1C1JS", funcArgs[1]) {
|
||||
result = append(result, "替换A1C1JS信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(srsTemplate, "M2D2IP", funcArgs[1]) {
|
||||
if !BasicReplace(srsTemplate, "M2D2IP", funcArgs[1]) {
|
||||
result = append(result, "替换M2D2IP信息")
|
||||
return false, result
|
||||
}
|
||||
@@ -765,7 +710,7 @@ func (op *AgentOsOperator) deployBackend(funcArgs []string) (bool, []string) {
|
||||
result := append(AppExecuteErrorLogPrefix, "部署 后端 服务 !")
|
||||
|
||||
// 环境判定
|
||||
commandExist, commandName := BaseCommandExistsBatch([]string{
|
||||
commandExist, commandName := BasicCommandExistsBatch([]string{
|
||||
"kubectl",
|
||||
})
|
||||
if !commandExist {
|
||||
@@ -783,11 +728,11 @@ func (op *AgentOsOperator) deployBackend(funcArgs []string) (bool, []string) {
|
||||
return false, result
|
||||
}
|
||||
// 根据参数 A1C2IP 替换
|
||||
if !BaseReplace(backendTemplate, "SUPREME", funcArgs[0]) {
|
||||
if !BasicReplace(backendTemplate, "SUPREME", funcArgs[0]) {
|
||||
result = append(result, "替换SUPREME信息")
|
||||
return false, result
|
||||
}
|
||||
if !BaseReplace(backendTemplate, "A1C2IP", funcArgs[1]) {
|
||||
if !BasicReplace(backendTemplate, "A1C2IP", funcArgs[1]) {
|
||||
result = append(result, "替换A1C2IP信息")
|
||||
return false, result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user