Files
ProjectOctopus/agent-go/executor/BasicFunction.go

247 lines
4.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package executor
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
var RemoveForcePrefix = []string{"rm", "-rf"}
// BasicCommandExists 判定命令是否存在
func BasicCommandExists(commandName string) bool {
cmd := exec.Command("command", "-v", commandName)
err := cmd.Run()
if err != nil {
log.DebugF("指令 %s 不存在", commandName)
return false
} else {
return true
}
}
// BasicCommandExistsBatch 判定批量命令是否存在
func BasicCommandExistsBatch(commandNameList []string) (bool, string) {
for _, commandName := range commandNameList {
if !BasicCommandExists(commandName) {
return false, commandName
}
}
return true, ""
}
// BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool {
// 暂不添加
//if !BasicFileExistAndNotNull(filename) {
// log.DebugF("文件替换")
//}
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
}
}
// BasicFileExists 检测文件是否存在
func BasicFileExists(filename string) bool {
cmd := exec.Command("test", "-f", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s 不存在!", filename)
return false
} else {
return true
}
}
// BasicFileExistAndNotNull 文件不为空返回true 文件为空返回false
func BasicFileExistAndNotNull(filename string) bool {
cmd := exec.Command("test", "-s", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s 为空!", filename)
return false
} else {
return true
}
}
// BasicPrettyPrint 打印执行结果
func BasicPrettyPrint(resultOk bool, resultLog []string) {
fmt.Printf("resultOK is => %#v\n", resultOk)
if resultLog == nil || len(resultLog) == 0 {
return
}
for _, s := range resultLog {
fmt.Println(s)
}
}
// BasicSystemdShutdown 使用Systemd关闭服务,会判定服务器是否存在
func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) {
if !strings.HasSuffix(serviceName, ".service") {
serviceName = serviceName + ".service"
}
systemdFilePath := []string{
"/lib/systemd/system/",
"/etc/systemd/system",
}
// 检查是否存在
var existService bool
for _, s := range systemdFilePath {
resultOk, _ := PipelineCommandExecutor(
[][]string{
{
"ls",
s,
},
{
"grep",
serviceName,
},
})
if resultOk {
existService = true
}
}
if !existService {
return true, []string{
serviceName,
"该服务不存在!",
}
}
// 关闭
var shutDownCommand = [][]string{
{
"systemctl",
"stop",
serviceName,
},
{
"sleep",
"1",
},
{
"systemctl",
"disable",
serviceName,
},
}
resultOK := PureResultSingleExecuteBatch(shutDownCommand)
return resultOK, resultLog
}
// BasicSystemdUp 使用Systemd启动服务,会判定服务器是否存在
func BasicSystemdUp(serviceName string) (ok bool, resultLog []string) {
if !strings.HasSuffix(serviceName, ".service") {
serviceName = serviceName + ".service"
}
systemdFilePath := []string{
"/lib/systemd/system/",
"/etc/systemd/system",
}
// 检查是否存在
var existService bool
for _, s := range systemdFilePath {
resultOk, _ := PipelineCommandExecutor(
[][]string{
{"ls",
s,
},
{
"grep",
serviceName,
},
})
if resultOk {
existService = true
}
}
if !existService {
return true, []string{
serviceName,
"该服务不存在!",
}
}
// 关闭
var shutDownCommand = [][]string{
{
"systemctl",
"start",
serviceName,
},
{
"sleep",
"1",
},
{
"systemctl",
"enable",
serviceName,
},
}
resultOK := PureResultSingleExecuteBatch(shutDownCommand)
return resultOK, resultLog
}
// BasicTransPipelineCommand 转换手写的管道命令为可执行的形式
func BasicTransPipelineCommand(pipelineString string) (pipelineCommand [][]string) {
var pipelineCommandC [][]string
split := strings.Split(pipelineString, "|")
if len(split) == 0 {
log.WarnF("输入的pipelineString有误 %s", pipelineString)
return pipelineCommandC
}
for _, s := range split {
// 去除掉无效的空行
singleCommand := strings.Split(s, " ")
var realSingleCommand []string
for i := range singleCommand {
if singleCommand[i] != "" {
realSingleCommand = append(realSingleCommand, singleCommand[i])
}
}
pipelineCommandC = append(pipelineCommandC, realSingleCommand)
}
return pipelineCommandC
}
func BasicConvertBufferToSlice(outputBuffer bytes.Buffer) (resultLog []string) {
s := outputBuffer.String()
split := strings.Split(s, "\n")
return split
}