diff --git a/.run/go build agent-go.run.xml b/.run/go build agent-go.run.xml index c29c0f4..2bb4a79 100644 --- a/.run/go build agent-go.run.xml +++ b/.run/go build agent-go.run.xml @@ -3,6 +3,7 @@ factoryName="Go Application" nameIsGenerated="true"> + diff --git a/agent-go/executor/BasicFunction.go b/agent-go/executor/BasicFunction.go index af9b43c..3363144 100644 --- a/agent-go/executor/BasicFunction.go +++ b/agent-go/executor/BasicFunction.go @@ -87,6 +87,46 @@ func BasicPrettyPrint(resultOk bool, resultLog []string) { } } +func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) { + + if !strings.HasSuffix(serviceName, ".service") { + serviceName = serviceName + ".service" + } + + // 检查是否存在 + execute := PureResultSingleExecute( + []string{ + "systemctl", + "status", + "-q", + serviceName, + }) + if !execute { + return true, []string{ + serviceName, + "该服务器不存在!", + } + } + + // 关闭 + var shutDownCommand = [][]string{ + { + "systemctl", + "stop", + serviceName, + }, + { + "systemctl", + "disable", + serviceName, + }, + } + + resultOK := PureResultSingleExecuteBatch(shutDownCommand) + + return resultOK, resultLog +} + func BasicTransPipelineCommand(pipelineString string) (pipelineCommand [][]string) { var pipelineCommandC [][]string diff --git a/agent-go/executor/BasicFunction_test.go b/agent-go/executor/BasicFunction_test.go index de704a9..3e6a77a 100644 --- a/agent-go/executor/BasicFunction_test.go +++ b/agent-go/executor/BasicFunction_test.go @@ -37,3 +37,13 @@ func TestBasicFileExistFalse(t *testing.T) { assert.Equal(t, exists, false, "文件不存在,应该返回false!") } + +func TestBasicSystemdShutdown(t *testing.T) { + + ok, resultLog := BasicSystemdShutdown("docker.service") + //ok, resultLog := BasicSystemdShutdown("docker") + //ok, resultLog := BasicSystemdShutdown("fwd") + + t.Logf("result ok is %v resultLog is %v", ok, resultLog) + +} diff --git a/agent-go/executor/FunctionalExecutor_test.go b/agent-go/executor/FunctionalExecutor_test.go index 702e139..b331f90 100644 --- a/agent-go/executor/FunctionalExecutor_test.go +++ b/agent-go/executor/FunctionalExecutor_test.go @@ -3,6 +3,7 @@ package executor import ( "bytes" "fmt" + "os" "os/exec" "strings" "testing" @@ -117,3 +118,51 @@ func TestPipelineCommandExecutor(t *testing.T) { t.Logf("command result are 下 \n") BasicPrettyPrint(ok, resultLog) } + +func TestSimple(t *testing.T) { + + for i := 0; i < 10; i++ { + var output bytes.Buffer + + fmt.Printf("output address is => %p \n", &output) + + } + fmt.Println() + var output bytes.Buffer + fmt.Printf("out is => %#v\n", output) + + command := exec.Command("ls") + command.StdoutPipe() + +} + +func TestFileBasedPipe(t *testing.T) { + reader, writer, err := os.Pipe() + if err != nil { + fmt.Printf("Error: Couldn't create the named pipe: %s\n", err) + } + go func() { + + output := make([]byte, 100) + n, err := reader.Read(output) + if err != nil { + fmt.Printf("Error: Couldn't read data from the named pipe: %s\n", err) + } + fmt.Printf("Read %d byte(s). [file-based pipe]\n", n) + + fmt.Printf("Read content are => %s\n", string(output)) + + }() + + input := make([]byte, 26) + for i := 65; i <= 90; i++ { + input[i-65] = byte(i) + } + n, err := writer.Write(input) + if err != nil { + fmt.Printf("Error: Couldn't write data to the named pipe: %s\n", err) + } + fmt.Printf("Written %d byte(s). [file-based pipe]\n", n) + //time.Sleep(200 * time.Millisecond) + +}