[ Agent ] [ Executor ] - 增加Pipeline部分的代码
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -31,3 +31,4 @@ build/
|
|||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
|
/agent-go/output/
|
||||||
|
|||||||
@@ -518,80 +518,66 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
return false, l
|
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"
|
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 {
|
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 {
|
|
||||||
// outside world
|
// outside world
|
||||||
// 国内 使用 UTSC 的源
|
dockerGPGSource = "https://download.docker.com/linux/ubuntu/gpg"
|
||||||
ok, l2 := AllCommandExecutor([]string{
|
dockerAPTSource = "https://download.docker.com/linux/ubuntu"
|
||||||
"curl",
|
}
|
||||||
|
|
||||||
|
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",
|
"-o",
|
||||||
dockerGPGFilePath,
|
dockerGPGFilePath,
|
||||||
"https://download.docker.com/linux/ubuntu/gpg",
|
},
|
||||||
})
|
{
|
||||||
if !ok {
|
"chmod",
|
||||||
return false, l2
|
"a+r",
|
||||||
}
|
dockerGPGFilePath,
|
||||||
if !BasicFileExistAndNotNull(dockerGPGFilePath) {
|
},
|
||||||
return false, []string{"添加gpg失败!"}
|
{
|
||||||
}
|
"add-apt-repository",
|
||||||
|
"deb [arch=" + op.AgentArch + "] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable",
|
||||||
resultOk, log2 := AllCompleteExecutor([][]string{
|
},
|
||||||
{
|
})
|
||||||
"gpg",
|
if !resultOk {
|
||||||
"--dearmor",
|
return false, append(log2, "添加APT源失败!")
|
||||||
"-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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// look for specific docker-version to install
|
// 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
|
specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode
|
||||||
}
|
}
|
||||||
|
|
||||||
resultOk, log2 := AllCommandExecutor([]string{
|
resultOk, log2 = AllCommandExecutor([]string{
|
||||||
"docker-ce=" + specificDockerVersion,
|
"docker-ce=" + specificDockerVersion,
|
||||||
"docker-ce-cli=" + specificDockerVersion,
|
"docker-ce-cli=" + specificDockerVersion,
|
||||||
"containerd.io",
|
"containerd.io",
|
||||||
@@ -1177,7 +1163,7 @@ func (op *AgentOsOperator) installZSHExec() (bool, []string) {
|
|||||||
|
|
||||||
executor, log2 := AllCompleteExecutor([][]string{
|
executor, log2 := AllCompleteExecutor([][]string{
|
||||||
{
|
{
|
||||||
"chomd",
|
"chmod",
|
||||||
"+x",
|
"+x",
|
||||||
"/root/wdd/zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package executor
|
package executor
|
||||||
|
|
||||||
import "os/exec"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
var RemoveForcePrefix = []string{"rm", "-rf"}
|
var RemoveForcePrefix = []string{"rm", "-rf"}
|
||||||
|
|
||||||
@@ -59,14 +62,22 @@ func BasicFileExists(filename string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BasicFileExistAndNotNull 文件不为空返回true 文件为空返回false
|
||||||
func BasicFileExistAndNotNull(filename string) bool {
|
func BasicFileExistAndNotNull(filename string) bool {
|
||||||
|
|
||||||
cmd := exec.Command("test", "-z", filename)
|
cmd := exec.Command("test", "-s", filename)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.DebugF("文件 %s 不存在!", filename)
|
log.DebugF("文件 %s 为空!", filename)
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BasicPrettyPrint(resultOk bool, resultLog []string) {
|
||||||
|
fmt.Printf("resultOK is => %#v\n", resultOk)
|
||||||
|
for _, s := range resultLog {
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package executor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
|
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
|
||||||
@@ -100,6 +102,39 @@ func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
|
|||||||
return resultSlice, resultError
|
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 执行单行命令,忽略输出,只对执行成功与否负责
|
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
|
||||||
func PureResultSingleExecute(singleCommand []string) (resultOK bool) {
|
func PureResultSingleExecute(singleCommand []string) (resultOK bool) {
|
||||||
|
|
||||||
|
|||||||
@@ -40,3 +40,9 @@ func TestPureResultSingleExecute(t *testing.T) {
|
|||||||
PureResultSingleExecute(closeSELinux)
|
PureResultSingleExecute(closeSELinux)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPipelineCommandExecutor(t *testing.T) {
|
||||||
|
|
||||||
|
PipelineCommandExecutor()
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ var Log, _ = NewLogger()
|
|||||||
func NewLogger() (*Logger, error) {
|
func NewLogger() (*Logger, error) {
|
||||||
config := zap.Config{
|
config := zap.Config{
|
||||||
Encoding: "json",
|
Encoding: "json",
|
||||||
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
||||||
OutputPaths: []string{"stdout"}, // 输出到控制台
|
OutputPaths: []string{"stdout"}, // 输出到控制台
|
||||||
ErrorOutputPaths: []string{"stderr"},
|
ErrorOutputPaths: []string{"stderr"},
|
||||||
EncoderConfig: zapcore.EncoderConfig{
|
EncoderConfig: zapcore.EncoderConfig{
|
||||||
|
|||||||
34
agent-go/main-prod.go
Normal file
34
agent-go/main-prod.go
Normal 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
|
||||||
|
//}
|
||||||
@@ -1,34 +1,59 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-go/g"
|
"agent-go/executor"
|
||||||
logger2 "agent-go/logger"
|
logger2 "agent-go/logger"
|
||||||
"agent-go/register"
|
"agent-go/register"
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logger2.Log
|
var log = logger2.Log
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// 解析命令行参数
|
var agentOP = &executor.AgentOsOperator{
|
||||||
var version string
|
InstallCommandPrefix: []string{
|
||||||
var agentServerInfoConf string
|
"apt-get", "install", "-y",
|
||||||
flag.StringVar(&version, "version", "", "config file version")
|
},
|
||||||
flag.StringVar(&agentServerInfoConf, "agentServerInfoConf", "", "agent server info conf file")
|
RemoveCommandPrefix: []string{"apt", "remove", "-y"},
|
||||||
flag.Parse()
|
CanAccessInternet: true,
|
||||||
// 读取对应版本的配置文件
|
IsOsTypeUbuntu: true,
|
||||||
filename := fmt.Sprintf("octopus-agent-%s.yaml", version)
|
IsAgentInnerWall: false,
|
||||||
println("config file name is => " + filename)
|
AgentArch: "amd64",
|
||||||
println("agent server info file is => " + agentServerInfoConf)
|
AgentOSReleaseCode: "focal",
|
||||||
|
AgentServerInfo: ®ister.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的连接配置
|
//executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall"))
|
||||||
g.G.AgentConfig = register.ParseConfiguration(filename)
|
//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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user