package executor import ( "agent-go/g" "agent-go/register" "fmt" "strings" ) type BaseFunc interface { Exec(baseFuncName string, funcArgs ...string) []string } type AgentOsOperator struct { 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"` CanAccessInternet bool `json:"can_access_internet",comment:"是否可以访问公网"` IsOsTypeUbuntu bool `json:"is_os_type_ubuntu",comment:"主机操作系统是否为ubuntu系列"` 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 执行基础功能函数 func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []string { var multiLineCommand [][]string switch baseFuncName { case "shutdownFirewall": multiLineCommand = op.shutdownFirewall() break case "modifyHostname": multiLineCommand = op.modifyHostname(funcArgs) break case "enableSwap": multiLineCommand = op.enableSwap() break case "disableSwap": multiLineCommand = op.disableSwap() break case "installDocker": multiLineCommand = op.installDocker(funcArgs) break case "removeDocker": multiLineCommand = op.removeDocker() break case "removeDockerCompose": multiLineCommand = op.removeDockerCompose() break case "installDockerCompose": multiLineCommand = op.installDockerCompose() break case "modifyDockerConfig": multiLineCommand = op.modifyDockerConfig(funcArgs) break case "installHelm": multiLineCommand = op.installHelm() break case "installHarbor": multiLineCommand = op.installHarbor() break case "chronyToPublicNTP": multiLineCommand = op.chronyToPublicNTP() break case "chronyToMaster": multiLineCommand = op.chronyToMaster(funcArgs) break case "installZSH": multiLineCommand = op.installZSH() break case "modifySshPort": multiLineCommand = op.modifySshPort(funcArgs) break case "openBBR": multiLineCommand = op.openBBR() break default: multiLineCommand = op.ok(funcArgs) } log.DebugF("multiLineCommand are => %v", multiLineCommand) var result []string // exec the command here for _, singleLineCommand := range multiLineCommand { result = append(result, ReadTimeCommandExecutor(singleLineCommand)...) } // debug usage //log.DebugF("exec result are => %v", result) for _, logLine := range result { fmt.Println(logLine) } // 归一化处理 return result } func (op *AgentOsOperator) shutdownFirewall() [][]string { shutdownFunc := [][]string{ {"systemctl", "stop", "firewalld"}, {"systemctl", "disable", "firewalld"}, } if !op.IsOsTypeUbuntu { shutdownFunc = append(shutdownFunc, []string{ "sed", "-i", "s/SELINUX=enforcing/SELINUX=disabled/g", "/etc/selinux/config", }, ) } return shutdownFunc } func (op *AgentOsOperator) modifyHostname(args []string) [][]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 { disableSwapFunc := [][]string{ { "swapoff", "-a", }, { "cp", "-f", "/etc/fstab", "/etc/fstab_back", }, { "sed", "-i", "/swap/d", "/etc/fstab", }, } return disableSwapFunc } func (op *AgentOsOperator) removeDocker() [][]string { removeDockerLine := append(op.RemoveCommandPrefix, []string{ "docker-ce", "docker.io", "docker-ce-cli", //"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{ removeDockerLine, } return removeDockerFunc } func (op *AgentOsOperator) installDocker(args []string) [][]string { // remove docker all staff installDockerFunc := op.removeDocker() 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{ installFirstLine, { "curl", "-o", "/usr/share/keyrings/docker-utsc.gpg", "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg", }, { "apt-key", "add", "/usr/share/keyrings/docker-utsc.gpg", }, { "add-apt-repository", "deb [arch=" + op.AgentArch + "] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu " + op.AgentOSReleaseCode + " stable", }, }...) } else { // outside world installDockerFunc = append(installDockerFunc, [][]string{ installFirstLine, { "curl", "-o", "/usr/share/keyrings/docker.gpg", "https://download.docker.com/linux/ubuntu/gpg ", }, { "apt-key", "add", "/usr/share/keyrings/docker.gpg", }, { "add-apt-repository", "deb [arch=" + op.AgentArch + "] https://download.docker.com/linux/ubuntu " + op.AgentOSReleaseCode + " stable", }, }...) } // look for specific docker-version to install installDockerFunc = append(installDockerFunc, []string{"apt-get", "update"}) 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-" + op.AgentOSReleaseCode } else { specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode } installDockerFunc = append(installDockerFunc, append( op.InstallCommandPrefix, "docker-ce="+specificDockerVersion, "docker-ce-cli="+specificDockerVersion, "containerd.io", "docker-compose-plugin", ), ) } 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", "--add-repo", "https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo", }, { "sed ", "-i ", "'s/download.docker.com/mirrors.ustc.edu.cn\\/docker-ce/g' ", "/etc/yum.repos.d/docker-ce.repo", }, {}, }...) } else { // outside world } } return installDockerFunc } func (op *AgentOsOperator) removeDockerCompose() [][]string { installDockerComposeFunc := [][]string{ append( op.RemoveCommandPrefix, "docker-compose", ), } return installDockerComposeFunc } func (op *AgentOsOperator) installDockerCompose() [][]string { installDockerComposeFunc := [][]string{ append( op.InstallCommandPrefix, "docker-compose", ), } return installDockerComposeFunc } func (op *AgentOsOperator) installHelm() [][]string { installHelmFunc := [][]string{ { "mkdir", "-p", "/root/wdd/", }, { "rm", "-rf", "/root/wdd/helm-v*", }, { "rm", "-rf", "/root/wdd/linux-amd64", }, { "wget", "--no-check-certificate", g.BaseFuncOssUrlPrefix + "helm-v3.12.1-linux-amd64.tar.gz", "-O", "/root/wdd/helm-v3.12.1-linux-amd64.tar.gz", }, { "tar", "-zvxf", "/root/wdd/helm-v3.12.1-linux-amd64.tar.gz", }, { "chmod", "+x", "/root/wdd/linux-amd64/helm", }, { "mv", "/root/wdd/linux-amd64/helm", "/usr/local/bin/helm", }, { "helm", "version", }, } /*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(args []string) [][]string { harborIPAddr := args[0] + ":8033" modifyDockerConfigFunc := [][]string{ { "mv", "/etc/docker/daemon.json", "/etc/docker/daemon.backup.json", }, { "wget", g.BaseFuncOssUrlPrefix + "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() [][]string { installHarborFunc := [][]string{ //{ // "mkdir", // "-p", // "/root/wdd/", //}, //{ // "rm", // "-rf", // "/root/wdd/harbor-offline-installer-v2.1.0.tgz", //}, //{ // "wget", // "--no-check-certificate", // g.BaseFuncOssUrlPrefix + "harbor-offline-installer-v2.1.0.tgz", // "-O", // "/root/wdd/harbor-offline-installer-v2.1.0.tgz", //}, { "tar", "-zvxf", "/root/wdd/harbor-offline-installer-v2.1.0.tgz", "-C", "/root/wdd/", }, { "rm", "-rf", "/root/wdd/harbor/harbor.yml", }, { "wget", "--no-check-certificate", g.BaseFuncOssUrlPrefix + "harbor-config-template.yml", "-O", "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostName/" + op.AgentServerInfo.ServerIPInV4 + "/g", "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostPort/8033/g", "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostPort/V2ryStr@ngPss/g", "/root/wdd/harbor/harbor.yml", }, { "/root/wdd/harbor/install.sh", "--with-chartmuseum", }, } return installHarborFunc } func (op *AgentOsOperator) chronyToPublicNTP() [][]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/pool ntp.ubuntu.com 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] 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 { installZSHFunc := [][]string{ { "mkdir", "-p", "/root/wdd/", }, 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", "/root/wdd/zsh-install.sh", }, }..., ) } else { installZSHFunc = append( installZSHFunc, [][]string{ { "wget", "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh", "-O", "/root/wdd/zsh-install.sh", }, }..., ) } // install installZSHFunc = append( installZSHFunc, [][]string{ { "chmod", "+x", "/root/wdd/zsh-install.sh", }, { "sh", "-c", "/root/wdd/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 { return [][]string{} } func (op *AgentOsOperator) openBBR() [][]string { return [][]string{} } func (op *AgentOsOperator) ok(args []string) [][]string { log.InfoF("base function is ok , args are => " + strings.Join(args, " ")) return [][]string{ {"ifconfig"}, } }