2021-05-08 18:00:33 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/internal/machine/api/vo"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/machine/domain/repository"
|
|
|
|
|
|
"mayfly-go/internal/machine/infrastructure/machine"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/pkg/gormx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type Machine interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.Machine]
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Save(*entity.Machine) error
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 测试机器连接
|
2023-10-26 17:15:49 +08:00
|
|
|
|
TestConn(me *entity.Machine) error
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 调整机器状态
|
2023-10-26 17:15:49 +08:00
|
|
|
|
ChangeStatus(id uint64, status int8) error
|
2022-04-27 10:59:02 +08:00
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
|
Count(condition *entity.MachineQuery) int64
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Delete(id uint64) error
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 分页获取机器信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取机器连接
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetCli(id uint64) (*machine.Cli, error)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取ssh隧道机器连接
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetSshTunnelMachine(id int) (*machine.SshTunnelMachine, error)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func newMachineApp(machineRepo repository.Machine, authCertApp AuthCert) Machine {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
app := &machineAppImpl{
|
2023-03-06 16:59:57 +08:00
|
|
|
|
authCertApp: authCertApp,
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
app.Repo = machineRepo
|
|
|
|
|
|
return app
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type machineAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Machine, repository.Machine]
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
authCertApp AuthCert
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分页获取机器信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error) {
|
|
|
|
|
|
return m.GetRepo().GetMachineList(condition, pageParam, toEntity, orderBy...)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
|
func (m *machineAppImpl) Count(condition *entity.MachineQuery) int64 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return m.GetRepo().Count(condition)
|
2021-09-11 14:04:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) Save(me *entity.Machine) error {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
|
2023-03-17 09:46:41 +08:00
|
|
|
|
if me.SshTunnelMachineId > 0 {
|
|
|
|
|
|
oldMachine.SshTunnelMachineId = me.SshTunnelMachineId
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
err := m.GetBy(oldMachine)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
me.PwdEncrypt()
|
|
|
|
|
|
if me.Id == 0 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
|
return errorx.NewBiz("该机器信息已存在")
|
|
|
|
|
|
}
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 新增机器,默认启用状态
|
|
|
|
|
|
me.Status = entity.MachineStatusEnable
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return m.Insert(me)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果存在该库,则校验修改的库是否为该库
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err == nil && oldMachine.Id != me.Id {
|
|
|
|
|
|
return errorx.NewBiz("该机器信息已存在")
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭连接
|
|
|
|
|
|
machine.DeleteCli(me.Id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return m.UpdateById(me)
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) TestConn(me *entity.Machine) error {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
me.Id = 0
|
2023-10-26 17:15:49 +08:00
|
|
|
|
mi, err := m.toMachineInfo(me)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return machine.TestConn(*mi, func(u uint64) (*machine.Info, error) {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
return m.toMachineInfoById(u)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
})
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) error {
|
2022-04-27 10:59:02 +08:00
|
|
|
|
if status == entity.MachineStatusDisable {
|
|
|
|
|
|
// 关闭连接
|
|
|
|
|
|
machine.DeleteCli(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
machine := new(entity.Machine)
|
|
|
|
|
|
machine.Id = id
|
|
|
|
|
|
machine.Status = status
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return m.UpdateById(machine)
|
2022-04-27 10:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 根据条件获取机器信息
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) Delete(id uint64) error {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 关闭连接
|
2021-09-08 17:55:57 +08:00
|
|
|
|
machine.DeleteCli(id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.Tx(
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine表信息
|
2023-07-20 22:41:13 +08:00
|
|
|
|
return gormx.DeleteByIdWithDb(db, new(entity.Machine), id)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine_file
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.DeleteByWithDb(db, &entity.MachineFile{MachineId: id})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine_script
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.DeleteByWithDb(db, &entity.MachineScript{MachineId: id})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) GetCli(machineId uint64) (*machine.Cli, error) {
|
|
|
|
|
|
return machine.GetCli(machineId, func(mid uint64) (*machine.Info, error) {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
return m.toMachineInfoById(mid)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) (*machine.SshTunnelMachine, error) {
|
|
|
|
|
|
return machine.GetSshTunnelMachine(machineId, func(mid uint64) (*machine.Info, error) {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
return m.toMachineInfoById(mid)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 生成机器信息,根据授权凭证id填充用户密码等
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) toMachineInfoById(machineId uint64) (*machine.Info, error) {
|
|
|
|
|
|
me, err := m.GetById(new(entity.Machine), machineId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errorx.NewBiz("机器信息不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
if me.Status != entity.MachineStatusEnable {
|
|
|
|
|
|
return nil, errorx.NewBiz("该机器已被停用")
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
return m.toMachineInfo(me)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) (*machine.Info, error) {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
mi := new(machine.Info)
|
|
|
|
|
|
mi.Id = me.Id
|
|
|
|
|
|
mi.Name = me.Name
|
|
|
|
|
|
mi.Ip = me.Ip
|
|
|
|
|
|
mi.Port = me.Port
|
|
|
|
|
|
mi.Username = me.Username
|
|
|
|
|
|
mi.TagId = me.TagId
|
|
|
|
|
|
mi.TagPath = me.TagPath
|
|
|
|
|
|
mi.EnableRecorder = me.EnableRecorder
|
|
|
|
|
|
mi.SshTunnelMachineId = me.SshTunnelMachineId
|
|
|
|
|
|
|
|
|
|
|
|
if me.UseAuthCert() {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
ac, err := m.authCertApp.GetById(new(entity.AuthCert), uint64(me.AuthCertId))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errorx.NewBiz("授权凭证信息已不存在,请重新关联")
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
mi.AuthMethod = ac.AuthMethod
|
|
|
|
|
|
ac.PwdDecrypt()
|
|
|
|
|
|
mi.Password = ac.Password
|
|
|
|
|
|
mi.Passphrase = ac.Passphrase
|
|
|
|
|
|
} else {
|
|
|
|
|
|
mi.AuthMethod = entity.AuthCertAuthMethodPassword
|
|
|
|
|
|
if me.Id != 0 {
|
|
|
|
|
|
me.PwdDecrypt()
|
|
|
|
|
|
}
|
|
|
|
|
|
mi.Password = me.Password
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return mi, nil
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|