[ 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

1
.gitignore vendored
View File

@@ -31,3 +31,4 @@ build/
### VS Code ###
.vscode/
/agent-go/output/

View File

@@ -518,55 +518,42 @@ 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 的源
dockerGPGSource = "https://download.docker.com/linux/ubuntu/gpg"
dockerAPTSource = "https://download.docker.com/linux/ubuntu"
}
ok, l2 := AllCommandExecutor([]string{
"curl",
"-fsSL",
dockerGPGSource,
"-o",
dockerGPGFilePath,
"https://download.docker.com/linux/ubuntu/gpg",
})
if !ok {
return false, l2
return false, append(l2, "下载docker gpg文件失败", dockerGPGSource, dockerAPTSource)
}
if !BasicFileExistAndNotNull(dockerGPGFilePath) {
return false, []string{"添加gpg失败"}
@@ -586,12 +573,11 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] https://download.docker.com/linux/ubuntu " + op.AgentOSReleaseCode + " stable",
"deb [arch=" + op.AgentArch + "] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable",
},
})
if !resultOk {
return false, log2
}
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()
}

View File

@@ -17,7 +17,7 @@ var Log, _ = NewLogger()
func NewLogger() (*Logger, error) {
config := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
OutputPaths: []string{"stdout"}, // 输出到控制台
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{

34
agent-go/main-prod.go Normal file
View File

@@ -0,0 +1,34 @@
package main
//import (
// "agent-go/g"
// logger2 "agent-go/logger"
// "agent-go/register"
// "flag"
// "fmt"
//)
//
//var log = logger2.Log
//
//func main() {
//
// // 解析命令行参数
// var version string
// var agentServerInfoConf string
// flag.StringVar(&version, "version", "", "config file version")
// flag.StringVar(&agentServerInfoConf, "agentServerInfoConf", "", "agent server info conf file")
// flag.Parse()
// // 读取对应版本的配置文件
// filename := fmt.Sprintf("octopus-agent-%s.yaml", version)
// println("config file name is => " + filename)
// println("agent server info file is => " + agentServerInfoConf)
//
// // 初始化Nacos的连接配置
// g.G.AgentConfig = register.ParseConfiguration(filename)
//
// // 执行初始化之策工作
// businessForeverChan := INIT(agentServerInfoConf)
//
// // 永远等待 runtime的队列消息
// <-businessForeverChan
//}

View File

@@ -1,34 +1,59 @@
package main
import (
"agent-go/g"
"agent-go/executor"
logger2 "agent-go/logger"
"agent-go/register"
"flag"
"fmt"
)
var log = logger2.Log
func main() {
// 解析命令行参数
var version string
var agentServerInfoConf string
flag.StringVar(&version, "version", "", "config file version")
flag.StringVar(&agentServerInfoConf, "agentServerInfoConf", "", "agent server info conf file")
flag.Parse()
// 读取对应版本的配置文件
filename := fmt.Sprintf("octopus-agent-%s.yaml", version)
println("config file name is => " + filename)
println("agent server info file is => " + agentServerInfoConf)
var agentOP = &executor.AgentOsOperator{
InstallCommandPrefix: []string{
"apt-get", "install", "-y",
},
RemoveCommandPrefix: []string{"apt", "remove", "-y"},
CanAccessInternet: true,
IsOsTypeUbuntu: true,
IsAgentInnerWall: false,
AgentArch: "amd64",
AgentOSReleaseCode: "focal",
AgentServerInfo: &register.AgentServerInfo{
ServerName: "",
ServerIPPbV4: "158.247.241.43",
ServerIPInV4: "10.0.4.6",
ServerIPPbV6: "",
ServerIPInV6: "",
Location: "",
Provider: "",
ManagePort: "",
CPUCore: "",
CPUBrand: "",
OSInfo: "",
OSKernelInfo: "",
TCPControl: "",
Virtualization: "",
IoSpeed: "",
MemoryTotal: "",
DiskTotal: "",
DiskUsage: "",
Comment: "",
MachineID: "",
AgentVersion: "",
TopicName: "",
},
OssOfflinePrefix: "https://oss-s1.107421.xyz/",
}
// 初始化Nacos的连接配置
g.G.AgentConfig = register.ParseConfiguration(filename)
//executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall"))
//executor.BasicPrettyPrint(agentOP.Exec("modifyHostname", "seoul-amd64-01"))
//executor.BasicPrettyPrint(agentOP.Exec("disableSwap"))
executor.BasicPrettyPrint(agentOP.Exec("removeDocker"))
executor.BasicPrettyPrint(agentOP.Exec("installDocker", "20"))
executor.BasicPrettyPrint(agentOP.Exec("removeDockerCompose"))
executor.BasicPrettyPrint(agentOP.Exec("installDockerCompose"))
//executor.BasicPrettyPrint(agentOP.Exec("installZSH"))
// 执行初始化之策工作
businessForeverChan := INIT(agentServerInfoConf)
// 永远等待 runtime的队列消息
<-businessForeverChan
}