[Excution] - base function start -2

This commit is contained in:
IceDerce
2023-06-20 16:43:33 +08:00
parent 6f655a772d
commit 4bdd97ca73
5 changed files with 141 additions and 72 deletions

View File

@@ -7,9 +7,9 @@ type BaseFunc interface {
} }
type AgentOsOperator struct { type AgentOsOperator struct {
InstallCommandPrefix string `json:"install_command_prefix",comment:"apt-get install or yum install"` InstallCommandPrefix []string `json:"install_command_prefix",comment:"apt-get install or yum install"`
RemoveCommandPrefix string `json:"remove_command_prefix",comment:"apt-get remove or yum remove"` RemoveCommandPrefix []string `json:"remove_command_prefix",comment:"apt-get remove or yum remove"`
CanAccessInternet bool `json:"can_access_internet",comment:"是否可以访问公网"` CanAccessInternet bool `json:"can_access_internet",comment:"是否可以访问公网"`
@@ -31,6 +31,9 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string
case "modifyHostname": case "modifyHostname":
multiLineCommand = op.modifyHostname(funcArgs) multiLineCommand = op.modifyHostname(funcArgs)
break break
case "enableSwap":
multiLineCommand = op.enableSwap()
break
case "disableSwap": case "disableSwap":
multiLineCommand = op.disableSwap() multiLineCommand = op.disableSwap()
break break
@@ -72,11 +75,15 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string
} }
log.DebugF("multiLineCommand are => %v", multiLineCommand)
// exec the command here // exec the command here
result, _ := MultiLineCommandExecutor(multiLineCommand) for _, singleLineCommand := range multiLineCommand {
ReadTimeCommandExecutor(singleLineCommand)
}
// 归一化处理 // 归一化处理
return strings.Join(result, "") return strings.Join([]string{}, "")
} }
func (op *AgentOsOperator) shutdownFirewall() [][]string { func (op *AgentOsOperator) shutdownFirewall() [][]string {
@@ -105,6 +112,24 @@ func (op *AgentOsOperator) modifyHostname(args []string) [][]string {
return [][]string{} return [][]string{}
} }
func (op *AgentOsOperator) enableSwap() [][]string {
enableSwapFunc := [][]string{
{
"cp",
"-f",
"/etc/fstab_back",
"/etc/fstab",
},
{
"cat",
"/etc/fstab",
},
}
return enableSwapFunc
}
func (op *AgentOsOperator) disableSwap() [][]string { func (op *AgentOsOperator) disableSwap() [][]string {
disableSwapFunc := [][]string{ disableSwapFunc := [][]string{
@@ -116,16 +141,12 @@ func (op *AgentOsOperator) disableSwap() [][]string {
"cp", "cp",
"-f", "-f",
"/etc/fstab", "/etc/fstab",
"/etc/fstab_bak", "/etc/fstab_back",
}, },
{ {
"cat", "sed",
"/etc/fstab_bak", "-i",
"|", "/swap/d",
"grep",
"-v",
"swap",
">",
"/etc/fstab", "/etc/fstab",
}, },
} }
@@ -135,9 +156,7 @@ func (op *AgentOsOperator) disableSwap() [][]string {
func (op *AgentOsOperator) removeDocker() [][]string { func (op *AgentOsOperator) removeDocker() [][]string {
removeDockerFunc := [][]string{ removeDockerLine := append(op.RemoveCommandPrefix, []string{
{
op.RemoveCommandPrefix,
"docker", "docker",
"docker-client", "docker-client",
"docker-client-latest", "docker-client-latest",
@@ -152,7 +171,10 @@ func (op *AgentOsOperator) removeDocker() [][]string {
"kubelet", "kubelet",
"kubeadm", "kubeadm",
"kubectl", "kubectl",
}, }...)
removeDockerFunc := [][]string{
removeDockerLine,
} }
return removeDockerFunc return removeDockerFunc
@@ -164,24 +186,64 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string {
installDockerFunc := op.removeDocker() installDockerFunc := op.removeDocker()
if op.IsOsTypeUbuntu { if op.IsOsTypeUbuntu {
//
installFirstLine := append(op.InstallCommandPrefix, []string{
"apt-transport-https",
"ca-certificates",
"curl",
"gnupg-agent",
"software-properties-common",
}...)
if op.IsAgentInnerWall {
// inner gfw
installDockerFunc = append(installDockerFunc, [][]string{ installDockerFunc = append(installDockerFunc, [][]string{
installFirstLine,
{ {
op.InstallCommandPrefix, "curl",
"apt-transport-https ca-certificates curl gnupg-agent software-properties-common", "-o",
"/etc/docker/docker-utsc.gpg",
"https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg",
}, },
{ {
"apt-key", "apt-key",
"add", "add",
"-", "/etc/docker/docker-utsc.gpg",
"$(curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg)",
}, },
}...) }...)
} else { } else {
// outside world
installDockerFunc = append(installDockerFunc, [][]string{ installDockerFunc = append(installDockerFunc, [][]string{
installFirstLine,
{ {
op.InstallCommandPrefix, "curl",
"yum-utils device-mapper-persistent-data lvm2", "-o",
"/etc/docker/docker-utsc.gpg",
"https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg",
}, },
{
"apt-key",
"add",
"/etc/docker/docker-utsc.gpg",
},
}...)
}
// look for specific docker-version to install
} else {
installFirstLine := append(op.InstallCommandPrefix,
[]string{
"yum-utils",
"device-mapper-persistent-data",
"lvm2",
}...,
)
if op.IsAgentInnerWall {
// inner gfw
installDockerFunc = append(installDockerFunc, [][]string{
installFirstLine,
{ {
"yum-config-manager", "yum-config-manager",
"--add-repo", "--add-repo",
@@ -195,6 +257,10 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string {
}, },
{}, {},
}...) }...)
} else {
// outside world
}
} }
return installDockerFunc return installDockerFunc

View File

@@ -3,8 +3,10 @@ package executor
import "testing" import "testing"
var agentOP = AgentOsOperator{ var agentOP = AgentOsOperator{
InstallCommandPrefix: "apt-get install", InstallCommandPrefix: []string{
RemoveCommandPrefix: "apt-get remove", "apt-get", "install", "-y",
},
RemoveCommandPrefix: []string{"/usr/bin/apt", "remove", "-y"},
CanAccessInternet: true, CanAccessInternet: true,
IsOsTypeUbuntu: true, IsOsTypeUbuntu: true,
IsAgentInnerWall: true, IsAgentInnerWall: true,
@@ -12,10 +14,11 @@ var agentOP = AgentOsOperator{
func TestBaseFunc(t *testing.T) { func TestBaseFunc(t *testing.T) {
agentOP.Exec("shutdownFirewall") //agentOP.Exec("shutdownFirewall")
agentOP.Exec("modifyHostname") //agentOP.Exec("modifyHostname")
agentOP.Exec("disableSwap") //agentOP.Exec("disableSwap")
//agentOP.Exec("enableSwap")
//agentOP.Exec("removeDocker")
agentOP.Exec("installDocker") agentOP.Exec("installDocker")
agentOP.Exec("removeDocker")
} }

View File

@@ -102,7 +102,6 @@ func SingleLineCommandExecutor(singleLineCommand []string) ([]string, error) {
var result []string var result []string
for scanner.Scan() { for scanner.Scan() {
result = append(result, scanner.Text()) result = append(result, scanner.Text())
} }
if err != nil { if err != nil {

View File

@@ -12,22 +12,22 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
panic(err) log.ErrorF("command %v stdout error => %v", singleLineCommand, err)
} }
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
panic(err) log.ErrorF("command %v stderr error => %v", singleLineCommand, err)
} }
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
panic(err) log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
} }
go copyOutput(stdout) go copyOutput(stdout)
go copyOutput(stderr) go copyOutput(stderr)
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
panic(err) log.ErrorF("command %v result error => %v", singleLineCommand, err)
} }
} }

View File

@@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("io.wdd.server.mapper") @MapperScan("io.wdd.server.mapper")
public class ServerApplication { public class ServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args); SpringApplication.run(ServerApplication.class, args);
} }