[agent-go] [Bastion] - basic accomplished !
This commit is contained in:
@@ -3,6 +3,7 @@ package a_executor
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/net/proxy"
|
||||
"io"
|
||||
@@ -10,6 +11,8 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -26,6 +29,7 @@ func BasicCommandExists(commandName string) bool {
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// BasicCommandExistsBatch 判定批量命令是否存在
|
||||
@@ -43,7 +47,7 @@ func BasicCommandExistsBatch(commandNameList []string) (bool, string) {
|
||||
// BasicCommandExistByPath 根据路径判定 命令是否存在
|
||||
func BasicCommandExistByPath(commandName string) bool {
|
||||
|
||||
if BasicFileExistInFolder(commandName, "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "bin", "/snap/bin") {
|
||||
if BasicFileExistInFolder(commandName, "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin", "/snap/bin") {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -53,7 +57,14 @@ func BasicCommandExistByPath(commandName string) bool {
|
||||
// BasicFileExistInFolder 查询fileName是否存在于目录folderList中,如果存在返回true,不存在返回false
|
||||
func BasicFileExistInFolder(fileName string, folderList ...string) bool {
|
||||
|
||||
if fileName == "" {
|
||||
return false // If the file name is empty, we cannot check for existence.
|
||||
}
|
||||
|
||||
for _, folder := range folderList {
|
||||
if folder == "" {
|
||||
continue // Skip empty folders.
|
||||
}
|
||||
if BasicFolderExists(folder) {
|
||||
// is folder
|
||||
ok, _ := PipelineCommandExecutor([][]string{
|
||||
@@ -71,22 +82,6 @@ func BasicFileExistInFolder(fileName string, folderList ...string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
} else {
|
||||
// it's a file
|
||||
ok, _ := PipelineCommandExecutor([][]string{
|
||||
{
|
||||
"echo",
|
||||
folder,
|
||||
},
|
||||
{
|
||||
"grep",
|
||||
"-c",
|
||||
fileName,
|
||||
},
|
||||
})
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,16 +256,16 @@ func BasicDockerImageExists(imageName, imageVersion string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func BasicInstallSoftwares(installPrefix []string, isStrict bool, softwares ...string) (bool, []string) {
|
||||
func BasicInstallSoftware(installPrefix []string, isStrict bool, softwares ...string) (bool, []string) {
|
||||
|
||||
var installLog []string
|
||||
|
||||
for _, software := range softwares {
|
||||
log.DebugF("[BasicInstallSoftwares] - going to install [ %s ]", software)
|
||||
log.DebugF("[BasicInstallSoftware] - going to install [ %s ]", software)
|
||||
|
||||
if !PureResultSingleExecute(append(installPrefix, software)) {
|
||||
|
||||
failedInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install failed !", software)
|
||||
failedInstall := fmt.Sprintf("[BasicInstallSoftware] - software of [ %s ] install failed !", software)
|
||||
installLog = append(installLog, failedInstall)
|
||||
|
||||
if isStrict {
|
||||
@@ -278,16 +273,90 @@ func BasicInstallSoftwares(installPrefix []string, isStrict bool, softwares ...s
|
||||
}
|
||||
}
|
||||
|
||||
successInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install success !", software)
|
||||
successInstall := fmt.Sprintf("[BasicInstallSoftware] - software of [ %s ] install success !", software)
|
||||
installLog = append(installLog, successInstall)
|
||||
}
|
||||
|
||||
return true, installLog
|
||||
}
|
||||
|
||||
// BasicReplace 基础替换命令
|
||||
// BasicReplace 将文件中origin字段全部替换为replace
|
||||
func BasicReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
if !BasicFileExistAndNotNull(filename) {
|
||||
log.WarnF("文件替换 文件不存在 %s", filename)
|
||||
return false
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
file, err := os.OpenFile(filename, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("error opening file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// 创建一个临时文件用于存储替换后的内容
|
||||
tempFilename := filename + ".tmp"
|
||||
tempFile, err := os.Create(tempFilename)
|
||||
if err != nil {
|
||||
fmt.Printf("error creating temp file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// 使用正则表达式替换,以支持多种替换场景(如部分词、多行等)
|
||||
re := regexp.MustCompile(fmt.Sprintf("(\\b%s\\b)|(^%s)|(%s$)", origin, origin, replace))
|
||||
newLine := re.ReplaceAllString(line, replace)
|
||||
|
||||
// 写入替换后的内容到临时文件
|
||||
_, err := tempFile.WriteString(newLine + "\n")
|
||||
if err != nil {
|
||||
fmt.Printf("error writing to temp file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否有错误发生
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Printf("error reading from file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
log.ErrorF("error closing file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
err = tempFile.Close()
|
||||
if err != nil {
|
||||
log.ErrorF("error closing file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// 移动文件名,指向新的替换后的文件
|
||||
err = os.Remove(filename)
|
||||
if err != nil {
|
||||
log.ErrorF("error removing file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
err = os.Rename(tempFilename, filename)
|
||||
if err != nil {
|
||||
fmt.Printf("error renaming file: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// 返回成功标志
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// BasicReplaceBySed 基础替换命令
|
||||
func BasicReplaceBySed(filename string, origin string, replace string) bool {
|
||||
|
||||
// 暂不添加
|
||||
//if !BasicFileExistAndNotNull(filename) {
|
||||
// log.DebugF("文件替换")
|
||||
@@ -305,57 +374,91 @@ func BasicReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
func BasicRemoveFolderComplete(folderName string) bool {
|
||||
|
||||
if !BasicFileExists(folderName) || !BasicFolderExists(folderName) {
|
||||
log.DebugF("[BasicRemoveFolderComplete] - file or folder of [%s] not exists !", folderName)
|
||||
return true
|
||||
}
|
||||
|
||||
cmd := exec.Command("rm", "-rf", folderName)
|
||||
err := cmd.Run()
|
||||
err := filepath.Walk(folderName,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return os.Remove(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.DebugF("删除 %s 失败!", folderName)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
err = os.RemoveAll(folderName)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
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
|
||||
if filename == "" {
|
||||
return false // An empty string does not correspond to a valid file.
|
||||
}
|
||||
_, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *os.PathError:
|
||||
// If the error is because the path doesn't exist, return false.
|
||||
if errors.Is(err.(*os.PathError).Err, os.ErrNotExist) {
|
||||
log.DebugF("文件 %s 不存在!", filename)
|
||||
return false
|
||||
}
|
||||
case nil:
|
||||
return true
|
||||
default:
|
||||
// Handle other types of errors as appropriate for your use case.
|
||||
return false
|
||||
}
|
||||
}
|
||||
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)
|
||||
// Check if the file exists
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
log.DebugF("文件 %s 不存在!", filename)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
// Open the file for reading
|
||||
file, err := os.Open(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
log.DebugF("文件 %s 打开有误!", filename)
|
||||
return false // Handle error according to your needs
|
||||
}
|
||||
|
||||
// Get the file size
|
||||
info, _ := file.Stat()
|
||||
size := info.Size()
|
||||
|
||||
// Check if the file is not empty
|
||||
return size > 0
|
||||
}
|
||||
|
||||
func BasicFolderExists(folderName string) bool {
|
||||
|
||||
cmd := exec.Command("test", "-d", folderName)
|
||||
err := cmd.Run()
|
||||
fi, err := os.Stat(folderName)
|
||||
if err != nil {
|
||||
log.DebugF("目录 %s 不存在!", folderName)
|
||||
if os.IsNotExist(err) {
|
||||
// 目标路径不存在
|
||||
log.WarnF("[BasicFolderExists] - 目录 %s 不存在!", folderName)
|
||||
return false
|
||||
}
|
||||
// 其他错误
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
// 判断是否为目录
|
||||
return fi.IsDir()
|
||||
}
|
||||
|
||||
func BasicCreateFolder(folderName string) bool {
|
||||
|
||||
Reference in New Issue
Block a user