112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package op
|
|
|
|
import (
|
|
"agent-wdd/config"
|
|
"agent-wdd/log"
|
|
"agent-wdd/utils"
|
|
"strings"
|
|
)
|
|
|
|
type PackageOperator struct {
|
|
installPrefix []string // 安装前缀
|
|
removePrefix []string // 移除前缀
|
|
upgradePrefix []string // 升级前缀
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// PackageInit 初始化包管理器, 如果本机是ubuntu或者debian, 则使用apt, 否则使用yum
|
|
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
|
|
}
|
|
}
|