128 lines
5.3 KiB
Go
128 lines
5.3 KiB
Go
package agent
|
||
|
||
import (
|
||
"fmt"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/utils"
|
||
"gorm.io/gorm"
|
||
"time"
|
||
)
|
||
|
||
const (
|
||
OS_WINDOWS = "Windows"
|
||
OS_LINUX = "Linux"
|
||
OS_LINUX_LIST = "Ubuntu,CentOS,KylinOS"
|
||
OS_LINUX_UBUNTU = "Ubuntu"
|
||
OS_LINUX_CENTOS = "CentOS"
|
||
OS_LINUX_KylinOS = "KylinOS"
|
||
)
|
||
|
||
// 小助手信息
|
||
// 表名:perc_agent_info
|
||
type AgentInfo struct {
|
||
Id string `gorm:"column:id;primaryKey" json:"id"` //type:string comment:主键 version:2023-9-11 14:25
|
||
Version string `gorm:"column:version" json:"version" validate:"required"` //type:string comment:版本 version:2023-9-11 14:25
|
||
VersionNum int64 `gorm:"column:version_num" json:"versionNum"` //type:int comment:版本排序号
|
||
OsPlatform string `gorm:"column:os_platform" json:"osPlatform"` //type:string comment:操作系统平台;字典:Windows\Linux
|
||
OsType string `gorm:"column:os_type" json:"osType" validate:"required"` //type:string comment:操作系统类型;字典:Windows\Ubuntu\CentOS\KylinOS version:2023-9-11 14:25
|
||
ArchType string `gorm:"column:arch_type" json:"archType" validate:"required"` //type:string comment:芯片架构类型;字典: X86\ARM version:2023-9-11 14:25
|
||
PackageUrl string `gorm:"column:package_url" json:"packageUrl"` //type:string comment:程序包地址;oss地址 version:2023-9-11 14:25
|
||
BucketName string `gorm:"column:bucket_name" json:"bucketName"` //type:string comment:桶名称
|
||
Filename string `gorm:"column:filename" json:"filename"` //type:string comment:文件名称
|
||
CompressionType string `gorm:"column:compression_type" json:"compressionType"` //type:string comment:压缩格式
|
||
Command string `gorm:"column:command" json:"command"` //type:string comment:启动命令
|
||
Describe string `gorm:"column:describe" json:"describe"` //type:string comment:描述 version:2023-9-11 14:25
|
||
CreateBy string `gorm:"column:create_by" json:"createBy"` //type:string comment:创建人
|
||
CreateTime time.Time `gorm:"column:create_time" json:"createTime"` //type:*time.Time comment:创建时间 version:2023-9-11 14:25
|
||
UpdateTime time.Time `gorm:"column:update_time" json:"updateTime"` //type:*time.Time comment:更新时间
|
||
}
|
||
|
||
func (AgentInfo) TableName() string {
|
||
return "perc_agent_info"
|
||
}
|
||
|
||
func (a *AgentInfo) Create() (int64, error) {
|
||
a.CreateTime = time.Now()
|
||
a.UpdateTime = time.Now()
|
||
a.Id = utils.NewAgentID()
|
||
|
||
tx := model.DB().Create(a)
|
||
|
||
if tx.Error != nil {
|
||
return 0, tx.Error
|
||
}
|
||
return tx.RowsAffected, nil
|
||
}
|
||
|
||
func (a *AgentInfo) CreateBatch(agents []AgentInfo) (int64, error) {
|
||
for i, _ := range agents {
|
||
agents[i].CreateTime = time.Now()
|
||
agents[i].UpdateTime = time.Now()
|
||
agents[i].Id = utils.NewAgentID()
|
||
}
|
||
tx := model.DB().CreateInBatches(agents, len(agents))
|
||
if tx.Error != nil {
|
||
return 0, tx.Error
|
||
}
|
||
return tx.RowsAffected, nil
|
||
}
|
||
|
||
func (a *AgentInfo) Delete() (int64, error) {
|
||
tx := model.DB().Delete(a)
|
||
if tx.Error != nil {
|
||
return 0, tx.Error
|
||
}
|
||
return tx.RowsAffected, nil
|
||
}
|
||
|
||
func (a *AgentInfo) Update(fields []string) (int64, error) {
|
||
a.UpdateTime = time.Now()
|
||
tx := model.DB().Model(&a).Select(fields).Updates(a)
|
||
if tx.Error != nil {
|
||
return 0, tx.Error
|
||
}
|
||
return tx.RowsAffected, nil
|
||
}
|
||
|
||
func (a *AgentInfo) Get(fields map[string]interface{}) (AgentInfo, error) {
|
||
agent := AgentInfo{}
|
||
tx := model.DB().Where(fields).First(&agent)
|
||
if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound {
|
||
return agent, tx.Error
|
||
}
|
||
return agent, nil
|
||
}
|
||
|
||
func (a *AgentInfo) HasNewVersion(fields map[string]interface{}) (bool, error) {
|
||
cout := int64(0)
|
||
tx := model.DB().Model(a).Where("os_type=? and arch_type=? and version_num >=?", fields["os_type"], fields["arch_type"], fields["version_num"]).Count(&cout)
|
||
if tx.Error != nil || cout > int64(0) {
|
||
return true, tx.Error
|
||
}
|
||
return false, nil
|
||
}
|
||
|
||
// 获取最近的版本
|
||
func (a *AgentInfo) ListLastVersion() ([]AgentInfo, error) {
|
||
agents := []AgentInfo{}
|
||
tx := model.DB().Raw("SELECT * FROM perc_agent_info A INNER JOIN (SELECT os_type,arch_type,MAX (version_num) AS version_num FROM perc_agent_info GROUP BY os_type,arch_type) b ON A.arch_type=b.arch_type AND A.os_type=b.os_type AND A.version_num=b.version_num").Find(&agents)
|
||
if tx.Error != nil {
|
||
return nil, tx.Error
|
||
}
|
||
return agents, nil
|
||
}
|
||
|
||
func (a *AgentInfo) Page(page *model.Page[AgentInfo], fields map[string]interface{}) error {
|
||
keyword := fields["keyword"]
|
||
query := model.DB()
|
||
if keyword != "" {
|
||
keyword = fmt.Sprintf("%%%s%%", keyword)
|
||
query = query.Where("concat(version,os_platform,os_type,arch_type,describe) like ?", keyword)
|
||
}
|
||
err := page.SelectPages(query)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|