[agent-wdd] 完成Excecutor和Operator部分,完成base tools部分
This commit is contained in:
110
agent-wdd/op/Operator.go
Normal file
110
agent-wdd/op/Operator.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package op
|
||||
|
||||
import (
|
||||
"agent-wdd/config"
|
||||
"agent-wdd/log"
|
||||
"agent-wdd/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PackageOperator struct {
|
||||
installPrefix []string `json:"install" yaml:"install"` // 安装前缀
|
||||
removePrefix []string `json:"remove" yaml:"remove"` // 移除前缀
|
||||
upgradePrefix []string `json:"upgrade" yaml:"upgrade"` // 升级前缀
|
||||
initCommand []string
|
||||
}
|
||||
|
||||
var (
|
||||
AgentPackOperator = &PackageOperator{}
|
||||
aptPackageOperator = &PackageOperator{
|
||||
installPrefix: []string{
|
||||
"apt-get", "install", "--allow-downgrades", "-y",
|
||||
},
|
||||
removePrefix: []string{
|
||||
"apt", "remove", "-y",
|
||||
},
|
||||
upgradePrefix: []string{},
|
||||
initCommand: []string{
|
||||
"apt-get", "update",
|
||||
},
|
||||
}
|
||||
yumPackageOperator = &PackageOperator{
|
||||
installPrefix: []string{
|
||||
"yum", "install", "-y",
|
||||
},
|
||||
removePrefix: []string{
|
||||
"yum", "remove", "-y",
|
||||
},
|
||||
upgradePrefix: []string{},
|
||||
}
|
||||
)
|
||||
|
||||
func (op *PackageOperator) Install(tools []string) {
|
||||
// 判定本机的包管理Operator
|
||||
generatePackageOperator()
|
||||
|
||||
// install seperately
|
||||
for _, tool := range tools {
|
||||
ok, result := SingleLineCommandExecutor(append(AgentPackOperator.installPrefix, tool))
|
||||
if !ok {
|
||||
log.Error("[install] failed! => %s", tool)
|
||||
utils.BeautifulPrint(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (op *PackageOperator) PackageInit() {
|
||||
log.Info("PackageInit !")
|
||||
|
||||
// 判定本机的包管理Operator
|
||||
generatePackageOperator()
|
||||
|
||||
// package init
|
||||
os := config.ConfigCache.Agent.OS
|
||||
osFamily := strings.ToLower(os.OsFamily)
|
||||
|
||||
if strings.Contains(osFamily, "ubuntu") || strings.Contains(osFamily, "debian") {
|
||||
ok, resultLog := SingleLineCommandExecutor(aptPackageOperator.initCommand)
|
||||
if !ok {
|
||||
log.Error("APT init failed! please check !")
|
||||
utils.BeautifulPrint(resultLog)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (op *PackageOperator) Remove(tools []string) {
|
||||
// 判定本机的包管理Operator
|
||||
generatePackageOperator()
|
||||
|
||||
// install seperately
|
||||
for _, tool := range tools {
|
||||
ok, result := SingleLineCommandExecutor(append(AgentPackOperator.removePrefix, tool))
|
||||
if !ok {
|
||||
log.Error("[remove] failed! => %s", tool)
|
||||
utils.BeautifulPrint(result)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func generatePackageOperator() {
|
||||
|
||||
// cache return
|
||||
if AgentPackOperator.initCommand != nil {
|
||||
return
|
||||
}
|
||||
// 检查本机是否存在Os的信息
|
||||
os := config.ConfigCache.Agent.OS
|
||||
if os.Hostname == "" {
|
||||
os.Gather()
|
||||
|
||||
os.SaveConfig()
|
||||
}
|
||||
|
||||
if os.IsUbuntuType {
|
||||
AgentPackOperator = aptPackageOperator
|
||||
} else {
|
||||
AgentPackOperator = yumPackageOperator
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user