[ Agent ] [ Executor ] - 调整Executor部分的代码

This commit is contained in:
zeaslity
2023-10-11 11:24:27 +08:00
parent 346512e770
commit 62712dfbce
4 changed files with 155 additions and 19 deletions

View File

@@ -6,7 +6,8 @@ import (
) )
type OctopusFunc interface { type OctopusFunc interface {
Exec(baseFuncName string, funcArgs ...string) []string Command(baseFuncName string, funcArgs ...string) []string
Exec(baseFuncName string, funcArgs ...string) (bool, []string)
Deploy(appFuncName string, funcArgs ...string) (bool, []string) Deploy(appFuncName string, funcArgs ...string) (bool, []string)
} }

View File

@@ -8,7 +8,12 @@ import (
) )
type BaseFunc interface { type BaseFunc interface {
Exec(baseFuncName string, funcArgs ...string) []string
// Command 返回基础函数的命令行
Command(baseFuncName string, funcArgs ...string) []string
// Exec 执行命令行,只返回正确结果 || 错误,日志
Exec(baseFuncName string, funcArgs ...string) (bool, []string)
} }
type AgentOsOperator struct { type AgentOsOperator struct {
@@ -32,8 +37,69 @@ type AgentOsOperator struct {
OssOfflinePrefix string OssOfflinePrefix string
} }
// Exec 执行基础功能函数 func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) (bool, []string) {
func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []string {
resultOk := false
var errorLog []string
switch baseFuncName {
case "shutdownFirewall":
resultOk, errorLog = op.shutdownFirewallExec()
break
case "modifyHostname":
resultOk, errorLog = op.modifyHostnameExec(funcArgs)
break
case "enableSwap":
resultOk, errorLog = op.enableSwapExec()
break
case "disableSwap":
resultOk, errorLog = op.disableSwapExec()
break
case "installDocker":
resultOk, errorLog = op.installDockerExec(funcArgs)
break
case "removeDocker":
resultOk, errorLog = op.removeDockerExec()
break
case "removeDockerCompose":
resultOk, errorLog = op.removeDockerComposeExec()
break
case "installDockerCompose":
resultOk, errorLog = op.installDockerComposeExec()
break
case "modifyDockerConfig":
resultOk, errorLog = op.modifyDockerConfigExec(funcArgs)
break
case "installHelm":
resultOk, errorLog = op.installHelmExec()
break
case "installHarbor":
resultOk, errorLog = op.installHarborExec()
break
case "chronyToPublicNTP":
resultOk, errorLog = op.chronyToPublicNTPExec()
break
case "chronyToMaster":
resultOk, errorLog = op.chronyToMasterExec(funcArgs)
break
case "installZSH":
resultOk, errorLog = op.installZSHExec()
break
case "modifySshPort":
resultOk, errorLog = op.modifySshPortExec(funcArgs)
break
case "openBBR":
resultOk, errorLog = op.openBBRExec()
break
default:
resultOk, errorLog = op.okExec(funcArgs)
}
return resultOk, errorLog
}
func (op *AgentOsOperator) Command(baseFuncName string, funcArgs ...string) []string {
var multiLineCommand [][]string var multiLineCommand [][]string
@@ -135,11 +201,21 @@ func (op *AgentOsOperator) shutdownFirewall() [][]string {
return shutdownFunc return shutdownFunc
} }
func (op *AgentOsOperator) shutdownFirewallExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) modifyHostname(args []string) [][]string { func (op *AgentOsOperator) modifyHostname(args []string) [][]string {
return [][]string{} return [][]string{}
} }
func (op *AgentOsOperator) modifyHostnameExec(args []string) (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) enableSwap() [][]string { func (op *AgentOsOperator) enableSwap() [][]string {
enableSwapFunc := [][]string{ enableSwapFunc := [][]string{
@@ -158,6 +234,10 @@ func (op *AgentOsOperator) enableSwap() [][]string {
return enableSwapFunc return enableSwapFunc
} }
func (op *AgentOsOperator) enableSwapExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) disableSwap() [][]string { func (op *AgentOsOperator) disableSwap() [][]string {
disableSwapFunc := [][]string{ disableSwapFunc := [][]string{
@@ -182,6 +262,10 @@ func (op *AgentOsOperator) disableSwap() [][]string {
return disableSwapFunc return disableSwapFunc
} }
func (op *AgentOsOperator) disableSwapExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) removeDocker() [][]string { func (op *AgentOsOperator) removeDocker() [][]string {
removeDockerLine := append(op.RemoveCommandPrefix, []string{ removeDockerLine := append(op.RemoveCommandPrefix, []string{
@@ -210,6 +294,10 @@ func (op *AgentOsOperator) removeDocker() [][]string {
return removeDockerFunc return removeDockerFunc
} }
func (op *AgentOsOperator) removeDockerExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) installDocker(args []string) [][]string { func (op *AgentOsOperator) installDocker(args []string) [][]string {
// remove docker all staff // remove docker all staff
@@ -323,6 +411,10 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string {
return installDockerFunc return installDockerFunc
} }
func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) removeDockerCompose() [][]string { func (op *AgentOsOperator) removeDockerCompose() [][]string {
installDockerComposeFunc := [][]string{ installDockerComposeFunc := [][]string{
@@ -335,6 +427,10 @@ func (op *AgentOsOperator) removeDockerCompose() [][]string {
return installDockerComposeFunc return installDockerComposeFunc
} }
func (op *AgentOsOperator) removeDockerComposeExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) installDockerCompose() [][]string { func (op *AgentOsOperator) installDockerCompose() [][]string {
installDockerComposeFunc := [][]string{ installDockerComposeFunc := [][]string{
@@ -347,6 +443,9 @@ func (op *AgentOsOperator) installDockerCompose() [][]string {
return installDockerComposeFunc return installDockerComposeFunc
} }
func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) installHelm() [][]string { func (op *AgentOsOperator) installHelm() [][]string {
installHelmFunc := [][]string{ installHelmFunc := [][]string{
{ {
@@ -422,6 +521,10 @@ func (op *AgentOsOperator) installHelm() [][]string {
return installHelmFunc return installHelmFunc
} }
func (op *AgentOsOperator) installHelmExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string {
harborIPAddr := args[0] + ":8033" harborIPAddr := args[0] + ":8033"
@@ -454,6 +557,10 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string {
return modifyDockerConfigFunc return modifyDockerConfigFunc
} }
func (op *AgentOsOperator) modifyDockerConfigExec(args []string) (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) installHarbor() [][]string { func (op *AgentOsOperator) installHarbor() [][]string {
installHarborFunc := [][]string{ installHarborFunc := [][]string{
@@ -520,6 +627,10 @@ func (op *AgentOsOperator) installHarbor() [][]string {
return installHarborFunc return installHarborFunc
} }
func (op *AgentOsOperator) installHarborExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) chronyToPublicNTP() [][]string { func (op *AgentOsOperator) chronyToPublicNTP() [][]string {
serverIPInV4 := op.AgentServerInfo.ServerIPInV4 serverIPInV4 := op.AgentServerInfo.ServerIPInV4
@@ -603,6 +714,10 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string {
return chronyToPublicNTPFunc return chronyToPublicNTPFunc
} }
func (op *AgentOsOperator) chronyToPublicNTPExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) chronyToMaster(args []string) [][]string { func (op *AgentOsOperator) chronyToMaster(args []string) [][]string {
masterInnerIP := args[0] masterInnerIP := args[0]
@@ -640,6 +755,10 @@ func (op *AgentOsOperator) chronyToMaster(args []string) [][]string {
return chronyToMasterFunc return chronyToMasterFunc
} }
func (op *AgentOsOperator) chronyToMasterExec(args []string) (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) installZSH() [][]string { func (op *AgentOsOperator) installZSH() [][]string {
installZSHFunc := [][]string{ installZSHFunc := [][]string{
@@ -767,19 +886,35 @@ func (op *AgentOsOperator) installZSH() [][]string {
return installZSHFunc return installZSHFunc
} }
func (op *AgentOsOperator) installZSHExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) modifySshPort(args []string) [][]string { func (op *AgentOsOperator) modifySshPort(args []string) [][]string {
return [][]string{} return [][]string{}
} }
func (op *AgentOsOperator) modifySshPortExec(args []string) (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) openBBR() [][]string { func (op *AgentOsOperator) openBBR() [][]string {
return [][]string{} return [][]string{}
} }
func (op *AgentOsOperator) openBBRExec() (bool, []string) {
return false, nil
}
func (op *AgentOsOperator) ok(args []string) [][]string { func (op *AgentOsOperator) ok(args []string) [][]string {
log.InfoF("base function is ok , args are => " + strings.Join(args, " ")) log.InfoF("base function is ok , args are => " + strings.Join(args, " "))
return [][]string{ return [][]string{
{"ifconfig"}, {"ifconfig"},
} }
} }
func (op *AgentOsOperator) okExec(args []string) (bool, []string) {
return false, nil
}

View File

@@ -43,18 +43,18 @@ var agentOP = &AgentOsOperator{
func TestBaseFunc(t *testing.T) { func TestBaseFunc(t *testing.T) {
//agentOP.Exec("shutdownFirewall") //agentOP.Command("shutdownFirewall")
//agentOP.Exec("modifyHostname") //agentOP.Command("modifyHostname")
//agentOP.Exec("disableSwap") //agentOP.Command("disableSwap")
//agentOP.Exec("enableSwap") //agentOP.Command("enableSwap")
//agentOP.Exec("removeDocker") //agentOP.Command("removeDocker")
agentOP.Exec("installDocker", "20") agentOP.Command("installDocker", "20")
//agentOP.Exec("removeDockerCompose") //agentOP.Command("removeDockerCompose")
//agentOP.Exec("installDockerCompose") //agentOP.Command("installDockerCompose")
//agentOP.Exec("installHelm") //agentOP.Command("installHelm")
//agentOP.Exec("installHarbor") //agentOP.Command("installHarbor")
//agentOP.Exec("chronyToPublicNTP") //agentOP.Command("chronyToPublicNTP")
//agentOP.Exec("chronyToMaster", "192.168.0.8") //agentOP.Command("chronyToMaster", "192.168.0.8")
//agentOP.Exec("installZSH") //agentOP.Command("installZSH")
} }

View File

@@ -32,7 +32,7 @@ func Execute(em *ExecutionMessage) ([]string, error) {
if strings.HasPrefix(em.Type, "BASE") { if strings.HasPrefix(em.Type, "BASE") {
// base function // base function
resultLog = AgentOsOperatorCache.Exec(em.FuncContent[0], em.FuncContent[1:]...) resultLog = AgentOsOperatorCache.Command(em.FuncContent[0], em.FuncContent[1:]...)
err = nil err = nil
} else if strings.HasPrefix(em.Type, "APP") { } else if strings.HasPrefix(em.Type, "APP") {
@@ -100,7 +100,7 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) ([]string, error) {
output, err = command.CombinedOutput() output, err = command.CombinedOutput()
if err != nil { if err != nil {
log.ErrorF("Pipeline Command Exec Error => %v", err.Error()) log.ErrorF("Pipeline Command Command Error => %v", err.Error())
// 收集错误的信息 // 收集错误的信息
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓", string(output)) resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓", string(output))