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"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
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 {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 根据条件获取账号信息
|
|
|
|
|
|
GetMachine(condition *entity.Machine, cols ...string) error
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
Save(*entity.Machine)
|
|
|
|
|
|
|
|
|
|
|
|
// 测试机器连接
|
|
|
|
|
|
TestConn(me *entity.Machine)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 调整机器状态
|
|
|
|
|
|
ChangeStatus(id uint64, status int8)
|
|
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
|
Count(condition *entity.MachineQuery) int64
|
2021-09-11 14:04:09 +08:00
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
Delete(id uint64)
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 根据id获取
|
|
|
|
|
|
GetById(id uint64, cols ...string) *entity.Machine
|
|
|
|
|
|
|
|
|
|
|
|
// 分页获取机器信息列表
|
2023-07-01 14:34:42 +08:00
|
|
|
|
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]vo.MachineVO, orderBy ...string) *model.PageResult[*[]vo.MachineVO]
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取机器连接
|
|
|
|
|
|
GetCli(id uint64) *machine.Cli
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取ssh隧道机器连接
|
2023-03-06 16:59:57 +08:00
|
|
|
|
GetSshTunnelMachine(id int) *machine.SshTunnelMachine
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func newMachineApp(machineRepo repository.Machine, authCertApp AuthCert) Machine {
|
|
|
|
|
|
return &machineAppImpl{
|
|
|
|
|
|
machineRepo: machineRepo,
|
|
|
|
|
|
authCertApp: authCertApp,
|
|
|
|
|
|
}
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type machineAppImpl struct {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
machineRepo repository.Machine
|
2023-03-06 16:59:57 +08:00
|
|
|
|
authCertApp AuthCert
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分页获取机器信息列表
|
2023-07-01 14:34:42 +08:00
|
|
|
|
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]vo.MachineVO, orderBy ...string) *model.PageResult[*[]vo.MachineVO] {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
return m.machineRepo.GetMachineList(condition, pageParam, toEntity, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
|
func (m *machineAppImpl) Count(condition *entity.MachineQuery) int64 {
|
2021-09-11 14:04:09 +08:00
|
|
|
|
return m.machineRepo.Count(condition)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *machineAppImpl) Save(me *entity.Machine) {
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
err := m.GetMachine(oldMachine)
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
me.PwdEncrypt()
|
|
|
|
|
|
if me.Id == 0 {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
biz.IsTrue(err != nil, "该机器信息已存在")
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 新增机器,默认启用状态
|
|
|
|
|
|
me.Status = entity.MachineStatusEnable
|
2021-07-28 18:03:19 +08:00
|
|
|
|
m.machineRepo.Create(me)
|
2023-03-06 16:59:57 +08:00
|
|
|
|
return
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果存在该库,则校验修改的库是否为该库
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭连接
|
|
|
|
|
|
machine.DeleteCli(me.Id)
|
|
|
|
|
|
m.machineRepo.UpdateById(me)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *machineAppImpl) TestConn(me *entity.Machine) {
|
|
|
|
|
|
me.Id = 0
|
|
|
|
|
|
// 测试连接
|
|
|
|
|
|
biz.ErrIsNilAppendErr(machine.TestConn(*m.toMachineInfo(me), func(u uint64) *machine.Info {
|
|
|
|
|
|
return m.toMachineInfoById(u)
|
|
|
|
|
|
}), "该机器无法连接: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-27 10:59:02 +08:00
|
|
|
|
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
|
|
|
|
|
|
if status == entity.MachineStatusDisable {
|
|
|
|
|
|
// 关闭连接
|
|
|
|
|
|
machine.DeleteCli(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
machine := new(entity.Machine)
|
|
|
|
|
|
machine.Id = id
|
|
|
|
|
|
machine.Status = status
|
|
|
|
|
|
m.machineRepo.UpdateById(machine)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 根据条件获取机器信息
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *machineAppImpl) Delete(id uint64) {
|
|
|
|
|
|
// 关闭连接
|
2021-09-08 17:55:57 +08:00
|
|
|
|
machine.DeleteCli(id)
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.Tx(
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine表信息
|
|
|
|
|
|
return db.Delete(new(entity.Machine), "id = ?", id).Error
|
|
|
|
|
|
},
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine_file
|
|
|
|
|
|
machineFile := &entity.MachineFile{MachineId: id}
|
|
|
|
|
|
return db.Where(machineFile).Delete(machineFile).Error
|
|
|
|
|
|
},
|
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
|
// 删除machine_script
|
|
|
|
|
|
machineScript := &entity.MachineScript{MachineId: id}
|
|
|
|
|
|
return db.Where(machineScript).Delete(machineScript).Error
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据条件获取机器信息
|
|
|
|
|
|
func (m *machineAppImpl) GetMachine(condition *entity.Machine, cols ...string) error {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
return m.machineRepo.GetMachine(condition, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
return m.machineRepo.GetById(id, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func (m *machineAppImpl) GetCli(machineId uint64) *machine.Cli {
|
|
|
|
|
|
cli, err := machine.GetCli(machineId, func(mid uint64) *machine.Info {
|
|
|
|
|
|
return m.toMachineInfoById(mid)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
|
2021-05-08 18:00:33 +08:00
|
|
|
|
return cli
|
|
|
|
|
|
}
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) *machine.SshTunnelMachine {
|
|
|
|
|
|
sshTunnel, err := machine.GetSshTunnelMachine(machineId, func(mid uint64) *machine.Info {
|
|
|
|
|
|
return m.toMachineInfoById(mid)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
|
|
|
|
|
|
return sshTunnel
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 生成机器信息,根据授权凭证id填充用户密码等
|
|
|
|
|
|
func (m *machineAppImpl) toMachineInfoById(machineId uint64) *machine.Info {
|
|
|
|
|
|
me := m.GetById(machineId)
|
|
|
|
|
|
biz.IsTrue(me.Status == entity.MachineStatusEnable, "该机器已被停用")
|
|
|
|
|
|
|
|
|
|
|
|
return m.toMachineInfo(me)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
|
|
|
|
|
|
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() {
|
|
|
|
|
|
ac := m.authCertApp.GetById(uint64(me.AuthCertId))
|
|
|
|
|
|
biz.NotNil(ac, "授权凭证信息已不存在,请重新关联")
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
return mi
|
|
|
|
|
|
}
|