2021-05-08 18:00:33 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
2024-04-27 01:35:21 +08:00
|
|
|
|
"mayfly-go/internal/event"
|
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"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"mayfly-go/internal/machine/infrastructure/cache"
|
2023-10-30 17:34:56 +08:00
|
|
|
|
"mayfly-go/internal/machine/mcm"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2024-04-09 12:55:51 +08:00
|
|
|
|
tagentity "mayfly-go/internal/tag/domain/entity"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-12-13 14:01:13 +08:00
|
|
|
|
"mayfly-go/pkg/global"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"mayfly-go/pkg/logx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"mayfly-go/pkg/scheduler"
|
2024-04-17 21:28:28 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
type SaveMachineParam struct {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Machine *entity.Machine
|
|
|
|
|
|
TagCodePaths []string
|
|
|
|
|
|
AuthCerts []*tagentity.ResourceAuthCert
|
2024-04-09 12:55:51 +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
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
SaveMachine(ctx context.Context, param *SaveMachineParam) error
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 测试机器连接
|
2024-04-09 12:55:51 +08:00
|
|
|
|
TestConn(me *entity.Machine, authCert *tagentity.ResourceAuthCert) error
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 调整机器状态
|
2023-11-07 21:05:21 +08:00
|
|
|
|
ChangeStatus(ctx context.Context, id uint64, status int8) error
|
2022-04-27 10:59:02 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Delete(ctx context.Context, 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
|
|
|
|
|
2024-02-23 22:53:17 +08:00
|
|
|
|
// 新建机器客户端连接(需手动调用Close)
|
2024-04-09 12:55:51 +08:00
|
|
|
|
NewCli(authCertName string) (*mcm.Cli, error)
|
2024-02-23 22:53:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取已缓存的机器连接,若不存在则新建客户端连接并缓存,主要用于定时获取状态等(避免频繁创建连接)
|
2023-10-30 17:34:56 +08:00
|
|
|
|
GetCli(id uint64) (*mcm.Cli, error)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// 根据授权凭证获取客户端连接
|
|
|
|
|
|
GetCliByAc(authCertName string) (*mcm.Cli, error)
|
|
|
|
|
|
|
2022-07-23 16:41:04 +08:00
|
|
|
|
// 获取ssh隧道机器连接
|
2023-10-30 17:34:56 +08:00
|
|
|
|
GetSshTunnelMachine(id int) (*mcm.SshTunnelMachine, error)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 定时更新机器状态信息
|
|
|
|
|
|
TimerUpdateStats()
|
|
|
|
|
|
|
|
|
|
|
|
// 获取机器运行时状态信息
|
|
|
|
|
|
GetMachineStats(machineId uint64) (*mcm.Stats, error)
|
2024-04-06 04:03:38 +00:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
ToMachineInfoByAc(ac string) (*mcm.MachineInfo, error)
|
2021-05-08 18:00:33 +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]
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
tagApp tagapp.TagTree `inject:"TagTreeApp"`
|
|
|
|
|
|
resourceAuthCertApp tagapp.ResourceAuthCert `inject:"ResourceAuthCertApp"`
|
2024-01-21 22:52:20 +08:00
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-04-23 11:35:45 +08:00
|
|
|
|
var _ (Machine) = (*machineAppImpl)(nil)
|
|
|
|
|
|
|
2024-01-21 22:52:20 +08:00
|
|
|
|
// 注入MachineRepo
|
|
|
|
|
|
func (m *machineAppImpl) InjectMachineRepo(repo repository.Machine) {
|
|
|
|
|
|
m.Repo = repo
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) SaveMachine(ctx context.Context, param *SaveMachineParam) error {
|
|
|
|
|
|
me := param.Machine
|
2024-04-17 21:28:28 +08:00
|
|
|
|
tagCodePaths := param.TagCodePaths
|
2024-04-09 12:55:51 +08:00
|
|
|
|
authCerts := param.AuthCerts
|
|
|
|
|
|
resourceType := tagentity.TagTypeMachine
|
|
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
if len(authCerts) == 0 {
|
|
|
|
|
|
return errorx.NewBiz("授权凭证信息不能为空")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-11 01:00:09 +08:00
|
|
|
|
oldMachine := &entity.Machine{
|
|
|
|
|
|
Ip: me.Ip,
|
|
|
|
|
|
Port: me.Port,
|
|
|
|
|
|
SshTunnelMachineId: me.SshTunnelMachineId,
|
2023-03-17 09:46:41 +08:00
|
|
|
|
}
|
2023-12-11 01:00:09 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
err := m.GetBy(oldMachine)
|
2023-03-06 16:59:57 +08:00
|
|
|
|
if me.Id == 0 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
|
return errorx.NewBiz("该机器信息已存在")
|
|
|
|
|
|
}
|
2024-04-06 18:19:17 +08:00
|
|
|
|
if m.CountByCond(&entity.Machine{Code: me.Code}) > 0 {
|
|
|
|
|
|
return errorx.NewBiz("该编码已存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-27 10:59:02 +08:00
|
|
|
|
// 新增机器,默认启用状态
|
|
|
|
|
|
me.Status = entity.MachineStatusEnable
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
return m.Tx(ctx, func(ctx context.Context) error {
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return m.Insert(ctx, me)
|
2024-04-12 13:24:20 +08:00
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return m.resourceAuthCertApp.RelateAuthCert(ctx, &tagapp.RelateAuthCertParam{
|
2024-04-06 18:19:17 +08:00
|
|
|
|
ResourceCode: me.Code,
|
2024-04-09 12:55:51 +08:00
|
|
|
|
ResourceType: resourceType,
|
2024-04-12 13:24:20 +08:00
|
|
|
|
AuthCerts: authCerts,
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2024-04-09 12:55:51 +08:00
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return m.tagApp.SaveResourceTag(ctx, &tagapp.SaveResourceTagParam{
|
|
|
|
|
|
ResourceTag: m.genMachineResourceTag(me, authCerts),
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
|
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
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
|
|
|
|
}
|
2023-12-20 23:01:51 +08:00
|
|
|
|
// 如果调整了ssh username等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
|
|
|
|
|
if oldMachine.Code == "" {
|
|
|
|
|
|
oldMachine, _ = m.GetById(new(entity.Machine), me.Id)
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
// 关闭连接
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mcm.DeleteCli(me.Id)
|
2024-04-06 18:19:17 +08:00
|
|
|
|
// 防止误传修改
|
|
|
|
|
|
me.Code = ""
|
2024-04-17 21:28:28 +08:00
|
|
|
|
return m.Tx(ctx, func(ctx context.Context) error {
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return m.UpdateById(ctx, me)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-04-12 13:24:20 +08:00
|
|
|
|
return m.resourceAuthCertApp.RelateAuthCert(ctx, &tagapp.RelateAuthCertParam{
|
|
|
|
|
|
ResourceCode: oldMachine.Code,
|
|
|
|
|
|
ResourceType: resourceType,
|
|
|
|
|
|
AuthCerts: authCerts,
|
|
|
|
|
|
})
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
if oldMachine.Name != me.Name {
|
|
|
|
|
|
if err := m.tagApp.UpdateTagName(ctx, tagentity.TagTypeMachine, oldMachine.Code, me.Name); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return m.tagApp.SaveResourceTag(ctx, &tagapp.SaveResourceTagParam{
|
|
|
|
|
|
ResourceTag: m.genMachineResourceTag(oldMachine, authCerts),
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
2024-04-12 13:24:20 +08:00
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) TestConn(me *entity.Machine, authCert *tagentity.ResourceAuthCert) error {
|
2023-03-06 16:59:57 +08:00
|
|
|
|
me.Id = 0
|
2024-04-10 13:04:31 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
if authCert.Id != 0 {
|
|
|
|
|
|
// 密文可能被清除,故需要重新获取
|
|
|
|
|
|
authCert, _ = m.resourceAuthCertApp.GetAuthCert(authCert.Name)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if authCert.CiphertextType == tagentity.AuthCertCiphertextTypePublic {
|
|
|
|
|
|
publicAuthCert, err := m.resourceAuthCertApp.GetAuthCert(authCert.Ciphertext)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
authCert = publicAuthCert
|
2024-04-10 13:04:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
mi, err := m.toMi(me, authCert)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2023-10-30 17:34:56 +08:00
|
|
|
|
cli, err := mi.Conn()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
cli.Close()
|
|
|
|
|
|
return nil
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *machineAppImpl) ChangeStatus(ctx context.Context, id uint64, status int8) error {
|
2022-04-27 10:59:02 +08:00
|
|
|
|
if status == entity.MachineStatusDisable {
|
|
|
|
|
|
// 关闭连接
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mcm.DeleteCli(id)
|
2022-04-27 10:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
machine := new(entity.Machine)
|
|
|
|
|
|
machine.Id = id
|
|
|
|
|
|
machine.Status = status
|
2023-11-07 21:05:21 +08:00
|
|
|
|
return m.UpdateById(ctx, machine)
|
2022-04-27 10:59:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 根据条件获取机器信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *machineAppImpl) Delete(ctx context.Context, id uint64) error {
|
2023-12-13 14:01:13 +08:00
|
|
|
|
machine, err := m.GetById(new(entity.Machine), id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("机器信息不存在")
|
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 关闭连接
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mcm.DeleteCli(id)
|
2023-12-13 14:01:13 +08:00
|
|
|
|
|
|
|
|
|
|
// 发布机器删除事件
|
2024-04-27 01:35:21 +08:00
|
|
|
|
global.EventBus.Publish(ctx, event.EventTopicDeleteMachine, machine)
|
2024-04-09 12:55:51 +08:00
|
|
|
|
|
|
|
|
|
|
resourceType := tagentity.TagTypeMachine
|
2023-12-13 14:01:13 +08:00
|
|
|
|
return m.Tx(ctx,
|
|
|
|
|
|
func(ctx context.Context) error {
|
|
|
|
|
|
return m.DeleteById(ctx, id)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-04-12 13:24:20 +08:00
|
|
|
|
return m.tagApp.SaveResourceTag(ctx, &tagapp.SaveResourceTagParam{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
ResourceTag: &tagapp.ResourceTag{
|
|
|
|
|
|
Code: machine.Code,
|
|
|
|
|
|
Type: tagentity.TagTypeMachine,
|
|
|
|
|
|
},
|
2024-04-12 13:24:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return m.resourceAuthCertApp.RelateAuthCert(ctx, &tagapp.RelateAuthCertParam{
|
2024-04-06 18:19:17 +08:00
|
|
|
|
ResourceCode: machine.Code,
|
2024-04-09 12:55:51 +08:00
|
|
|
|
ResourceType: resourceType,
|
|
|
|
|
|
})
|
2023-12-13 14:01:13 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) NewCli(authCertName string) (*mcm.Cli, error) {
|
|
|
|
|
|
if mi, err := m.ToMachineInfoByAc(authCertName); err != nil {
|
2024-02-23 22:53:17 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return mi.Conn()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) GetCliByAc(authCertName string) (*mcm.Cli, error) {
|
|
|
|
|
|
return mcm.GetMachineCli(authCertName, func(ac string) (*mcm.MachineInfo, error) {
|
|
|
|
|
|
return m.ToMachineInfoByAc(ac)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-07-23 16:41:04 +08:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) GetCli(machineId uint64) (*mcm.Cli, error) {
|
|
|
|
|
|
cli, err := mcm.GetMachineCliById(machineId)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return cli, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, authCert, err := m.getMachineAndAuthCert(machineId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return m.GetCliByAc(authCert.Name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-30 17:34:56 +08:00
|
|
|
|
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) (*mcm.SshTunnelMachine, error) {
|
|
|
|
|
|
return mcm.GetSshTunnelMachine(machineId, func(mid uint64) (*mcm.MachineInfo, error) {
|
2024-04-06 04:03:38 +00:00
|
|
|
|
return m.ToMachineInfoById(mid)
|
2022-07-23 16:41:04 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *machineAppImpl) TimerUpdateStats() {
|
|
|
|
|
|
logx.Debug("开始定时收集并缓存服务器状态信息...")
|
|
|
|
|
|
scheduler.AddFun("@every 2m", func() {
|
|
|
|
|
|
machineIds := new([]entity.Machine)
|
2024-04-06 04:03:38 +00:00
|
|
|
|
m.GetRepo().ListByCond(&entity.Machine{Status: entity.MachineStatusEnable, Protocol: entity.MachineProtocolSsh}, machineIds, "id")
|
2023-11-07 21:05:21 +08:00
|
|
|
|
for _, ma := range *machineIds {
|
|
|
|
|
|
go func(mid uint64) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
logx.ErrorTrace(fmt.Sprintf("定时获取机器[id=%d]状态信息失败", mid), err.(error))
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
logx.Debugf("定时获取机器[id=%d]状态信息开始", mid)
|
|
|
|
|
|
cli, err := m.GetCli(mid)
|
|
|
|
|
|
if err != nil {
|
2024-04-12 13:24:20 +08:00
|
|
|
|
logx.Errorf("定时获取机器[id=%d]状态信息失败, 获取机器cli失败: %s", mid, err.Error())
|
|
|
|
|
|
return
|
2023-11-07 21:05:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
cache.SaveMachineStats(mid, cli.GetAllStats())
|
|
|
|
|
|
logx.Debugf("定时获取机器[id=%d]状态信息结束", mid)
|
|
|
|
|
|
}(ma.Id)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *machineAppImpl) GetMachineStats(machineId uint64) (*mcm.Stats, error) {
|
|
|
|
|
|
return cache.GetMachineStats(machineId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// 根据授权凭证,生成机器信息
|
|
|
|
|
|
func (m *machineAppImpl) ToMachineInfoByAc(authCertName string) (*mcm.MachineInfo, error) {
|
|
|
|
|
|
authCert, err := m.resourceAuthCertApp.GetAuthCert(authCertName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
machine := &entity.Machine{
|
|
|
|
|
|
Code: authCert.ResourceCode,
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := m.GetBy(machine); err != nil {
|
|
|
|
|
|
return nil, errorx.NewBiz("该授权凭证关联的机器信息不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return m.toMi(machine, authCert)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 16:59:57 +08:00
|
|
|
|
// 生成机器信息,根据授权凭证id填充用户密码等
|
2024-04-06 04:03:38 +00:00
|
|
|
|
func (m *machineAppImpl) ToMachineInfoById(machineId uint64) (*mcm.MachineInfo, error) {
|
2024-04-09 12:55:51 +08:00
|
|
|
|
me, authCert, err := m.getMachineAndAuthCert(machineId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return m.toMi(me, authCert)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *machineAppImpl) getMachineAndAuthCert(machineId uint64) (*entity.Machine, *tagentity.ResourceAuthCert, error) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
me, err := m.GetById(new(entity.Machine), machineId)
|
|
|
|
|
|
if err != nil {
|
2024-04-09 12:55:51 +08:00
|
|
|
|
return nil, nil, errorx.NewBiz("[%d]机器信息不存在", machineId)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
2024-04-06 04:03:38 +00:00
|
|
|
|
if me.Status != entity.MachineStatusEnable && me.Protocol == 1 {
|
2024-04-09 12:55:51 +08:00
|
|
|
|
return nil, nil, errorx.NewBiz("[%s]该机器已被停用", me.Code)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
authCert, err := m.resourceAuthCertApp.GetResourceAuthCert(tagentity.TagTypeMachine, me.Code)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
2023-10-30 17:34:56 +08:00
|
|
|
|
}
|
2024-04-09 12:55:51 +08:00
|
|
|
|
|
|
|
|
|
|
return me, authCert, nil
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (m *machineAppImpl) toMi(me *entity.Machine, authCert *tagentity.ResourceAuthCert) (*mcm.MachineInfo, error) {
|
2023-10-30 17:34:56 +08:00
|
|
|
|
mi := new(mcm.MachineInfo)
|
2023-03-06 16:59:57 +08:00
|
|
|
|
mi.Id = me.Id
|
|
|
|
|
|
mi.Name = me.Name
|
|
|
|
|
|
mi.Ip = me.Ip
|
|
|
|
|
|
mi.Port = me.Port
|
2024-04-12 13:24:20 +08:00
|
|
|
|
mi.TagPath = m.tagApp.ListTagPathByTypeAndCode(int8(tagentity.TagTypeMachineAuthCert), authCert.Name)
|
2023-03-06 16:59:57 +08:00
|
|
|
|
mi.EnableRecorder = me.EnableRecorder
|
2024-04-06 04:03:38 +00:00
|
|
|
|
mi.Protocol = me.Protocol
|
2023-03-06 16:59:57 +08:00
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
mi.AuthCertName = authCert.Name
|
2024-04-09 12:55:51 +08:00
|
|
|
|
mi.Username = authCert.Username
|
|
|
|
|
|
mi.Password = authCert.Ciphertext
|
2024-04-10 13:04:31 +08:00
|
|
|
|
mi.Passphrase = authCert.GetExtraString(tagentity.ExtraKeyPassphrase)
|
2024-04-09 12:55:51 +08:00
|
|
|
|
mi.AuthMethod = int8(authCert.CiphertextType)
|
2023-10-30 17:34:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用了ssh隧道,则将隧道机器信息也附上
|
|
|
|
|
|
if me.SshTunnelMachineId > 0 {
|
2024-04-09 12:55:51 +08:00
|
|
|
|
sshTunnelMi, err := m.ToMachineInfoById(uint64(me.SshTunnelMachineId))
|
2023-10-30 17:34:56 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
mi.SshTunnelMachine = sshTunnelMi
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return mi, nil
|
2023-03-06 16:59:57 +08:00
|
|
|
|
}
|
2024-04-17 21:28:28 +08:00
|
|
|
|
|
|
|
|
|
|
func (m *machineAppImpl) genMachineResourceTag(me *entity.Machine, authCerts []*tagentity.ResourceAuthCert) *tagapp.ResourceTag {
|
|
|
|
|
|
authCertTags := collx.ArrayMap[*tagentity.ResourceAuthCert, *tagapp.ResourceTag](authCerts, func(val *tagentity.ResourceAuthCert) *tagapp.ResourceTag {
|
|
|
|
|
|
return &tagapp.ResourceTag{
|
|
|
|
|
|
Code: val.Name,
|
|
|
|
|
|
Name: val.Username,
|
|
|
|
|
|
Type: tagentity.TagTypeMachineAuthCert,
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return &tagapp.ResourceTag{
|
|
|
|
|
|
Code: me.Code,
|
|
|
|
|
|
Type: tagentity.TagTypeMachine,
|
|
|
|
|
|
Name: me.Name,
|
|
|
|
|
|
Children: authCertTags,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|