diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index 1737231..a8d8250 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -1,6 +1,9 @@ package executor -import "strings" +import ( + "agent-go/register" + "strings" +) type BaseFunc interface { Exec(baseFuncName string, funcArgs ...string) string @@ -15,11 +18,13 @@ type AgentOsOperator struct { IsOsTypeUbuntu bool `json:"is_os_type_ubuntu",comment:"主机操作系统是否为ubuntu系列"` - IsAgentInnerWall bool `json:"is_agent_inner_wall", comment:"主机是否身处国内"` + IsAgentInnerWall bool `json:"is_agent_inner_wall",comment:"主机是否身处国内"` AgentArch string `json:"agent_arch",comment:"主机的CPU架构,可选为amd64 arm64"` AgentOSReleaseCode string `json:"agent_os_release_code",comment:"主机操作系统的发行版代号, focal之类的"` + + AgentServerInfo register.AgentServerInfo `json:"agent_server_info"` } // Exec 执行基础功能函数 @@ -51,7 +56,7 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string multiLineCommand = op.installDockerCompose() break case "modifyDockerConfig": - multiLineCommand = op.modifyDockerConfig() + multiLineCommand = op.modifyDockerConfig(funcArgs) break case "installHelm": multiLineCommand = op.installHelm() @@ -161,20 +166,22 @@ func (op *AgentOsOperator) disableSwap() [][]string { func (op *AgentOsOperator) removeDocker() [][]string { removeDockerLine := append(op.RemoveCommandPrefix, []string{ - "docker", - "docker-client", - "docker-client-latest", + "docker-ce", + "docker.io", "docker-ce-cli", - "docker-common", - "docker-latest", - "docker-latest-logrotate", - "docker-logrotate", - "docker-selinux", - "docker-engine-selinux", - "docker-engine", - "kubelet", - "kubeadm", - "kubectl", + //"docker", + //"docker-common", + //"docker-latest", + //"docker-latest-logrotate", + //"docker-logrotate", + //"docker-selinux", + //"docker-engine-selinux", + //"docker-engine", + //"kubelet", + //"kubeadm", + //"kubectl", + //"docker-client", + //"docker-client-latest", }...) removeDockerFunc := [][]string{ @@ -206,13 +213,13 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string { { "curl", "-o", - "/etc/docker/docker-utsc.gpg", + "/usr/share/keyrings/docker-utsc.gpg", "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg", }, { "apt-key", "add", - "/etc/docker/docker-utsc.gpg", + "/usr/share/keyrings/docker-utsc.gpg", }, { "add-apt-repository", @@ -226,13 +233,13 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string { { "curl", "-o", - "/etc/docker/docker.gpg", + "/usr/share/keyrings/docker.gpg", "https://download.docker.com/linux/ubuntu/gpg ", }, { "apt-key", "add", - "/etc/docker/docker.gpg", + "/usr/share/keyrings/docker.gpg", }, { "add-apt-repository", @@ -247,9 +254,9 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string { var specificDockerVersion string // hard code here 5:20.10.10~3-0~ubuntu-focal if strings.HasPrefix(args[0], "19") { - specificDockerVersion = "5:19.03.15~3-0~ubuntu-focal" + specificDockerVersion = "5:19.03.15~3-0~ubuntu-" + op.AgentOSReleaseCode } else { - specificDockerVersion = "5:20.10.10~3-0~ubuntu-focal" + specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode } installDockerFunc = append(installDockerFunc, @@ -297,39 +304,388 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string { return installDockerFunc } +func (op *AgentOsOperator) removeDockerCompose() [][]string { + + installDockerComposeFunc := [][]string{ + append( + op.RemoveCommandPrefix, + "docker-compose", + ), + } + + return installDockerComposeFunc +} + func (op *AgentOsOperator) installDockerCompose() [][]string { - return [][]string{} + installDockerComposeFunc := [][]string{ + append( + op.InstallCommandPrefix, + "docker-compose", + ), + } + + return installDockerComposeFunc } func (op *AgentOsOperator) installHelm() [][]string { + var installHelmFunc [][]string - return [][]string{} + if op.IsOsTypeUbuntu { + installHelmFunc = [][]string{ + + { + "curl", + "-o", + "/usr/share/keyrings/helm.gpg", + "https://baltocdn.com/helm/signing.asc", + }, + { + "apt-key", + "add", + "/usr/share/keyrings/helm.gpg", + }, + { + "add-apt-repository", + "https://baltocdn.com/helm/stable/debian/ all main", + }, + { + "apt-get", + "update", + }, + append(op.InstallCommandPrefix, "helm"), + } + } else { + log.ErrorF("Operation OS is CentOS, Helm not installed!") + } + + return installHelmFunc } -func (op *AgentOsOperator) modifyDockerConfig() [][]string { +func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { - return [][]string{} + harborIPAddr := args[0] + ":8033" + + modifyDockerConfigFunc := [][]string{ + { + "mv", + "/etc/docker/daemon.json", + "/etc/docker/daemon.backup.json", + }, + { + "wget", + "https://oss2.demo.uavcmlc.com:18000/wangdada/daemon-config.json", + "-O", + "/etc/docker/daemon.json", + }, + { + "sed", + "-i", + "s/$DockerRegisterDomain/" + harborIPAddr + "/g", + "/etc/docker/daemon.json", + }, + { + "systemctl", + "restart", + "docker.service", + }, + } + + return modifyDockerConfigFunc } func (op *AgentOsOperator) installHarbor(args []string) [][]string { - return [][]string{} + installHarborFunc := [][]string{ + { + "wget", + "--no-check-certificate", + "https://oss2.demo.uavcmlc.com:18000/wangdada/harbor-offline-installer-v2.1.0.tgz", + "-O", + "harbor-offline-installer-v2.1.0.tgz", + }, + { + "tar", + "-zvxf", + "harbor-offline-installer-v2.1.0.tgz", + }, + { + "rm", + "-rf", + "./harbor/harbor.yml", + }, + { + "wget", + "--no-check-certificate", + "https://oss2.demo.uavcmlc.com:18000/wangdada/harbor.yml", + "-O", + "./harbor/harbor.yml", + }, + { + "sed", + "-i", + "s/$HarborHostName/" + op.AgentServerInfo.ServerIPInV4 + "/g", + "./harbor/harbor.yml", + }, + { + "sed", + "-i", + "s/$HarborHostPort/8033/g", + "./harbor/harbor.yml", + }, + { + "sed", + "-i", + "s/$HarborHostPort/V2ryStr@ngPss/g", + "./harbor/harbor.yml", + }, + { + "./harbor/install.sh", + "--with-chartmuseum", + }, + } + + return installHarborFunc } func (op *AgentOsOperator) chronyToPublicNTP() [][]string { - return [][]string{} + serverIPInV4 := op.AgentServerInfo.ServerIPInV4 + internalIPCIDR := strings.Join(strings.Split(serverIPInV4, ".")[:2], ".") + ".0.0/16" + + chronyToPublicNTPFunc := [][]string{ + append( + op.InstallCommandPrefix, + "chrony", + ), + { + "systemctl", + "enable", + "chronyd", + }, + { + "systemctl", + "start", + "chronyd", + }, + } + + var chronyFile string + if op.IsOsTypeUbuntu { + chronyFile = "/etc/chrony/chrony.conf" + } else { + chronyFile = "/etc/chrony.conf" + } + + chronyToPublicNTPFunc = append(chronyToPublicNTPFunc, + [][]string{ + { + "sed", + "-i", + "$ a allow " + internalIPCIDR, + chronyFile, + }, + { + "sed", + "-i", + "s/server 0.centos.pool.ntp.org iburst/server ntp2.aliyun.com iburst/g", + chronyFile, + }, + { + "systemctl", + "restart", + "chronyd", + }, + { + "sleep", + "2", + }, + { + "chronyc", + "-n", + "sources", + "-v", + "chronyc", + "tracking", + }, + { + "timedatectl", + "set-timezone", + "Asia/Shanghai", + }, + { + "timedatectl", + "set-ntp", + "true", + }, + { + "systemctl", + "restart", + "rsyslog", + }, + }..., + ) + + return chronyToPublicNTPFunc } func (op *AgentOsOperator) chronyToMaster(args []string) [][]string { + masterInnerIP := args[0] - return [][]string{} + chronyToMasterFunc := [][]string{ + { + "sed", + "-i", + "$ a NTP=" + masterInnerIP, + "/etc/systemd/timesyncd.conf", + }, + { + "systemctl", + "daemon-reload", + }, + { + "systemctl", + "restart", + "systemd-timesyncd.service", + }, + { + "sleep", + "3", + }, + { + "timedatectl", + "show-timesync", + "--all", + }, + { + "timedatectl", + "status", + }, + } + + return chronyToMasterFunc } func (op *AgentOsOperator) installZSH() [][]string { - return [][]string{} + installZSHFunc := [][]string{ + append( + op.InstallCommandPrefix, + "zsh", + "git", + ), + } + + if op.IsAgentInnerWall { + installZSHFunc = append( + installZSHFunc, + [][]string{ + { + "wget", + "https://cdn.jsdelivr.net/gh/robbyrussell/oh-my-zsh@master/tools/install.sh", + "-O", + "zsh-install.sh", + }, + }..., + ) + + } else { + installZSHFunc = append( + installZSHFunc, + [][]string{ + { + "wget", + "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh", + "-O", + "zsh-install.sh", + }, + }..., + ) + } + + // install + installZSHFunc = append( + installZSHFunc, + [][]string{ + { + "chmod", + "+x", + "zsh-install.sh", + }, + { + "sh", + "-c", + "./zsh-install.sh", + }, + }..., + ) + + // modify ZSH + if !op.IsAgentInnerWall { + installZSHFunc = append( + installZSHFunc, + [][]string{ + { + "git", + "clone", + "https://github.com.cnpmjs.org/zsh-users/zsh-autosuggestions", + "~/.oh-my-zsh/plugins/zsh-autosuggestions", + }, + { + "git", + "clone", + "https://github.com.cnpmjs.org/zsh-users/zsh-syntax-highlighting.git", + "~/.oh-my-zsh/plugins/zsh-syntax-highlighting", + }, + { + "wget", + "https://b2.107421.xyz/oh-my-zsh-plugins-list.txt", + "-O", + "oh-my-zsh-plugins-list.txt", + }, + { + "wget", + "-c", + "-i", + "./oh-my-zsh-plugins-list.txt", + "-P", + "~/.oh-my-zsh/plugins/", + }, + { + "sed", + "-i", + "s/robbyrussell/agnoster/g", + "~/.zshrc", + }, + { + "sed", + "-i", + "s/^# DISABLE_AUTO_UPDATE=\"true\"/DISABLE_AUTO_UPDATE=\"true\"/g", + "~/.zshrc", + }, + { + "sed", + "-i", + "s/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting command-not-found z themes)/g", + "~/.zshrc", + }, + { + "source", + "~/.zshrc", + }, + { + "chsh", + "-s", + "/bin/zsh", + }, + { + "zsh", + }, + }..., + ) + } + + return installZSHFunc } func (op *AgentOsOperator) modifySshPort(args []string) [][]string { @@ -343,7 +699,8 @@ func (op *AgentOsOperator) openBBR() [][]string { } func (op *AgentOsOperator) ok(args []string) [][]string { + log.InfoF("base function is ok , args are => " + strings.Join(args, " ")) return [][]string{ - {"base function is ok , args are => " + strings.Join(args, " ")}, + {"ifconfig"}, } } diff --git a/agent-go/executor/BaseFunction_test.go b/agent-go/executor/BaseFunction_test.go index 37920f3..77de6e1 100644 --- a/agent-go/executor/BaseFunction_test.go +++ b/agent-go/executor/BaseFunction_test.go @@ -6,10 +6,12 @@ var agentOP = AgentOsOperator{ InstallCommandPrefix: []string{ "apt-get", "install", "-y", }, - RemoveCommandPrefix: []string{"/usr/bin/apt", "remove", "-y"}, + RemoveCommandPrefix: []string{"apt", "remove", "-y"}, CanAccessInternet: true, IsOsTypeUbuntu: true, IsAgentInnerWall: true, + AgentArch: "amd64", + AgentOSReleaseCode: "focal", } func TestBaseFunc(t *testing.T) { @@ -19,6 +21,7 @@ func TestBaseFunc(t *testing.T) { //agentOP.Exec("disableSwap") //agentOP.Exec("enableSwap") //agentOP.Exec("removeDocker") - agentOP.Exec("installDocker") + //agentOP.Exec("installDocker", "20") + agentOP.Exec("installDockerCompose") } diff --git a/agent-go/executor/script/1_node_important.sh b/agent-go/executor/script/1_node_important.sh index 3d879b2..47335f6 100644 --- a/agent-go/executor/script/1_node_important.sh +++ b/agent-go/executor/script/1_node_important.sh @@ -1290,7 +1290,7 @@ modifyZSH() { colorEcho ${GREEN} "zsh应该已经安装成功!!!" colorEcho ${BLUE} "开始修改zsh的相关配置信息,使其更加好用…………" echo "" - cat >~/oh-my-zsh-plugins-list.txt <oh-my-zsh-plugins-list.txt <