[ Agent ] [ Executor ] - 增加Pipeline部分的代码

This commit is contained in:
zeaslity
2023-10-18 09:26:58 +08:00
parent 0e8f633166
commit be01eb28a9
8 changed files with 192 additions and 94 deletions

View File

@@ -518,80 +518,66 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
return false, l
}
executor, resultLog := AllCompleteExecutor([][]string{
{
"rm",
"-rf",
"/etc/apt/keyrings/docker.gpg",
},
{
"mkdir",
"-p",
"/etc/apt/keyrings",
},
})
if !executor {
return false, append(resultLog, "创建docker-key gpg文件失败")
}
dockerGPGFilePath := "/etc/apt/keyrings/docker.gpg"
dockerGPGSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg"
dockerAPTSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu"
// 安装镜像的证书
if op.IsAgentInnerWall {
// 国内 使用 UTSC 的源
ok, l2 := AllCommandExecutor([]string{
"curl",
"-o",
dockerGPGFilePath,
"https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg",
})
if !ok {
return false, l2
}
if !BasicFileExistAndNotNull(dockerGPGFilePath) {
return false, []string{"添加gpg失败"}
}
resultOk, log2 := AllCompleteExecutor([][]string{
{
"gpg",
"--dearmor",
"-o",
dockerGPGFilePath,
},
{
"chmod",
"a+r",
dockerGPGFilePath,
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu " + op.AgentOSReleaseCode + " stable",
},
})
if !resultOk {
return false, log2
}
} else {
if !op.IsAgentInnerWall {
// outside world
// 国内 使用 UTSC 的源
ok, l2 := AllCommandExecutor([]string{
"curl",
dockerGPGSource = "https://download.docker.com/linux/ubuntu/gpg"
dockerAPTSource = "https://download.docker.com/linux/ubuntu"
}
ok, l2 := AllCommandExecutor([]string{
"curl",
"-fsSL",
dockerGPGSource,
"-o",
dockerGPGFilePath,
})
if !ok {
return false, append(l2, "下载docker gpg文件失败", dockerGPGSource, dockerAPTSource)
}
if !BasicFileExistAndNotNull(dockerGPGFilePath) {
return false, []string{"添加gpg失败"}
}
resultOk, log2 := AllCompleteExecutor([][]string{
{
"gpg",
"--dearmor",
"-o",
dockerGPGFilePath,
"https://download.docker.com/linux/ubuntu/gpg",
})
if !ok {
return false, l2
}
if !BasicFileExistAndNotNull(dockerGPGFilePath) {
return false, []string{"添加gpg失败"}
}
resultOk, log2 := AllCompleteExecutor([][]string{
{
"gpg",
"--dearmor",
"-o",
dockerGPGFilePath,
},
{
"chmod",
"a+r",
dockerGPGFilePath,
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] https://download.docker.com/linux/ubuntu " + op.AgentOSReleaseCode + " stable",
},
})
if !resultOk {
return false, log2
}
},
{
"chmod",
"a+r",
dockerGPGFilePath,
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable",
},
})
if !resultOk {
return false, append(log2, "添加APT源失败")
}
// look for specific docker-version to install
@@ -607,7 +593,7 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode
}
resultOk, log2 := AllCommandExecutor([]string{
resultOk, log2 = AllCommandExecutor([]string{
"docker-ce=" + specificDockerVersion,
"docker-ce-cli=" + specificDockerVersion,
"containerd.io",
@@ -1177,7 +1163,7 @@ func (op *AgentOsOperator) installZSHExec() (bool, []string) {
executor, log2 := AllCompleteExecutor([][]string{
{
"chomd",
"chmod",
"+x",
"/root/wdd/zsh-install.sh",
},

View File

@@ -1,6 +1,9 @@
package executor
import "os/exec"
import (
"fmt"
"os/exec"
)
var RemoveForcePrefix = []string{"rm", "-rf"}
@@ -59,14 +62,22 @@ func BasicFileExists(filename string) bool {
}
}
// BasicFileExistAndNotNull 文件不为空返回true 文件为空返回false
func BasicFileExistAndNotNull(filename string) bool {
cmd := exec.Command("test", "-z", filename)
cmd := exec.Command("test", "-s", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s 不存在", filename)
log.DebugF("文件 %s 为空", filename)
return false
} else {
return true
}
}
func BasicPrettyPrint(resultOk bool, resultLog []string) {
fmt.Printf("resultOK is => %#v\n", resultOk)
for _, s := range resultLog {
fmt.Println(s)
}
}

View File

@@ -2,9 +2,11 @@ package executor
import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"strings"
)
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
@@ -100,6 +102,39 @@ func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
return resultSlice, resultError
}
func PipelineCommandExecutor() {
cmd1 := exec.Command("ps", "aux")
cmd2 := exec.Command("grep", "apipe")
var outputBuf1 bytes.Buffer
cmd1.Stdout = &outputBuf1
if err := cmd1.Start(); err != nil {
fmt.Printf("Error: The first command can not be startup %s\n", err)
return
}
if err := cmd1.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the first command: %s\n", err)
return
}
cmd2.Stdin = &outputBuf1
var outputBuf2 bytes.Buffer
cmd2.Stdout = &outputBuf2
if err := cmd2.Start(); err != nil {
fmt.Printf("Error: The second command can not be startup: %s\n", err)
return
}
if err := cmd2.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the second command: %s\n", err)
return
}
s := outputBuf2.String()
split := strings.Split(s, "\n")
BasicPrettyPrint(true, split)
}
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
func PureResultSingleExecute(singleCommand []string) (resultOK bool) {

View File

@@ -40,3 +40,9 @@ func TestPureResultSingleExecute(t *testing.T) {
PureResultSingleExecute(closeSELinux)
}
func TestPipelineCommandExecutor(t *testing.T) {
PipelineCommandExecutor()
}