pcm-coordinator/api/internal/storeLink/storeLink.go

415 lines
12 KiB
Go

/*
Copyright (c) [2023] [pcm]
[pcm-coordinator] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
package storeLink
import (
"context"
"github.com/pkg/errors"
"gitlink.org.cn/jcce-pcm/pcm-ac/hpcAC"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils/timeutils"
"gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/modelarts"
"gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopus"
"gorm.io/gorm"
)
type Linkage interface {
UploadImage(path string) (interface{}, error)
DeleteImage(imageId string) (interface{}, error)
QueryImageList() (interface{}, error)
SubmitTask(imageId string, cmd string, envs []string, params []string, resourceId string) (interface{}, error)
QueryTask(taskId string) (interface{}, error)
QuerySpecs() (interface{}, error)
DeleteTask(taskId string) (interface{}, error)
}
const (
PY_PARAM_PREFIX = "--"
SPACE = " "
UNDERSCORE = "_"
EQUAL = "="
COMMA = ","
TYPE_OCTOPUS = "1"
TYPE_MODELARTS = "2"
TYPE_SHUGUANGAI = "3"
OCTOPUS = "Octopus"
MODELARTS = "Modelarts"
SHUGUANGAI = "ShuguangAi"
)
var (
OctImgStatus = map[int32]string{
1: "未上传",
3: "制作完成",
4: "制作失败",
}
AITYPE = map[string]string{
"1": OCTOPUS,
"2": MODELARTS,
"3": SHUGUANGAI,
}
)
type StoreLink struct {
ILinkage Linkage
}
func NewStoreLink(ctx context.Context, svcCtx *svc.ServiceContext, participant *models.StorelinkCenter) *StoreLink {
switch participant.Type {
case TYPE_OCTOPUS:
linkStruct := NewOctopusLink(ctx, svcCtx, participant)
return &StoreLink{ILinkage: linkStruct}
case TYPE_MODELARTS:
linkStruct := NewModelArtsLink(ctx, svcCtx, participant)
return &StoreLink{ILinkage: linkStruct}
case TYPE_SHUGUANGAI:
linkStruct := NewShuguangAi(ctx, svcCtx, participant)
return &StoreLink{ILinkage: linkStruct}
default:
return nil
}
}
func GetParticipants(dbEngin *gorm.DB) []*models.StorelinkCenter {
var participants []*models.StorelinkCenter
dbEngin.Raw("select * from storelink_center").Scan(&participants)
return participants
}
func GetParticipantById(partId int64, dbEngin *gorm.DB) *models.StorelinkCenter {
var participant models.StorelinkCenter
dbEngin.Raw("select * from storelink_center where id = ?", partId).Scan(&participant)
return &participant
}
func ConvertType[T any](in *T, participant *models.StorelinkCenter) (interface{}, error) {
switch (interface{})(in).(type) {
case *octopus.UploadImageResp:
var resp types.UploadLinkImageResp
inresp := (interface{})(in).(*octopus.UploadImageResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
return resp, nil
}
return resp, nil
case *octopus.DeleteImageResp:
var resp types.DeleteLinkImageResp
inresp := (interface{})(in).(*octopus.DeleteImageResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
return resp, nil
}
return resp, nil
case *octopus.GetUserImageListResp:
var resp types.GetLinkImageListResp
inresp := (interface{})(in).(*octopus.GetUserImageListResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
resp.Images = nil
return resp, nil
}
for _, v := range inresp.Payload.Images {
var image types.ImageSl
image.ImageId = v.Image.Id
image.ImageName = v.Image.ImageName
image.ImageStatus = OctImgStatus[v.Image.ImageStatus]
resp.Images = append(resp.Images, &image)
}
return resp, nil
case *modelarts.ListReposDetailsResp:
var resp types.GetLinkImageListResp
inresp := (interface{})(in).(*modelarts.ListReposDetailsResp)
if inresp.Errors != nil {
resp.Success = false
resp.ErrorMsg = inresp.Errors[0].ErrorMessage
resp.Images = nil
return resp, nil
}
resp.Success = true
for _, v := range inresp.Items {
for _, r := range v.Tags {
var image types.ImageSl
image.ImageId = v.Namespace + "/" + v.Name + ":" + r
image.ImageName = v.Name
image.ImageStatus = "created"
resp.Images = append(resp.Images, &image)
}
}
return resp, nil
case *hpcAC.GetImageListAiResp:
var resp types.GetLinkImageListResp
inresp := (interface{})(in).(*hpcAC.GetImageListAiResp)
if inresp.Code == "0" {
resp.Success = true
for _, img := range inresp.Data {
var image types.ImageSl
image.ImageId = img.ImageId
image.ImageName = img.Version
image.ImageStatus = "created"
resp.Images = append(resp.Images, &image)
}
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
resp.Images = nil
}
return resp, nil
case *octopus.CreateTrainJobResp:
var resp types.SubmitLinkTaskResp
inresp := (interface{})(in).(*octopus.CreateTrainJobResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
return resp, nil
}
resp.TaskId = inresp.Payload.JobId
return resp, nil
case *modelarts.CreateTrainingJobResp:
var resp types.SubmitLinkTaskResp
inresp := (interface{})(in).(*modelarts.CreateTrainingJobResp)
if inresp.ErrorMsg != "" {
resp.ErrorMsg = inresp.ErrorMsg
resp.Success = false
return resp, nil
}
resp.Success = true
resp.TaskId = inresp.Metadata.Id
return resp, nil
case *hpcAC.SubmitTaskAiResp:
var resp types.SubmitLinkTaskResp
inresp := (interface{})(in).(*hpcAC.SubmitTaskAiResp)
if inresp.Code == "0" {
resp.Success = true
resp.TaskId = inresp.Data
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
}
return resp, nil
case *hpcAC.SubmitJobResp:
var resp types.SubmitLinkTaskResp
inresp := (interface{})(in).(*hpcAC.SubmitJobResp)
if inresp.Code == "0" {
resp.Success = true
resp.TaskId = inresp.Data
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
}
return resp, nil
case *octopus.GetTrainJobResp:
var resp types.GetLinkTaskResp
inresp := (interface{})(in).(*octopus.GetTrainJobResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
return resp, nil
}
var task types.TaskSl
task.TaskId = inresp.Payload.TrainJob.Id
task.TaskName = inresp.Payload.TrainJob.Name
task.StartedAt = inresp.Payload.TrainJob.StartedAt
task.CompletedAt = inresp.Payload.TrainJob.CompletedAt
task.TaskStatus = inresp.Payload.TrainJob.Status
resp.Task = &task
return resp, nil
case *modelarts.JobResponse:
var resp types.GetLinkTaskResp
inresp := (interface{})(in).(*modelarts.JobResponse)
if inresp.ErrorMsg != "" {
resp.ErrorMsg = inresp.ErrorMsg
resp.Success = false
return resp, nil
}
resp.Success = true
resp.Task = &types.TaskSl{}
resp.Task.TaskId = inresp.Metadata.Id
resp.Task.TaskName = inresp.Metadata.Name
resp.Task.StartedAt = int64(inresp.Status.StartTime)
resp.Task.CompletedAt = int64(inresp.Status.Duration)
resp.Task.TaskStatus = inresp.Status.Phase
return resp, nil
case *hpcAC.GetPytorchTaskResp:
var resp types.GetLinkTaskResp
inresp := (interface{})(in).(*hpcAC.GetPytorchTaskResp)
if inresp.Code == "0" {
resp.Success = true
var task types.TaskSl
task.TaskId = inresp.Data.Id
task.TaskName = inresp.Data.TaskName
task.TaskStatus = inresp.Data.Status
task.StartedAt = timeutils.StringToUnixTime(inresp.Data.StartTime)
task.CompletedAt = timeutils.StringToUnixTime(inresp.Data.EndTime)
resp.Task = &task
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
resp.Task = nil
}
return resp, nil
case *hpcAC.GetJobDetailResp:
var resp types.GetLinkTaskResp
inresp := (interface{})(in).(*hpcAC.GetJobDetailResp)
if inresp.Code == "0" {
resp.Success = true
var task types.TaskSl
task.TaskId = inresp.Data.JobId
task.TaskName = inresp.Data.JobName
task.TaskStatus = AcStatus[inresp.Data.JobStatus]
task.StartedAt = timeutils.StringToUnixTime(inresp.Data.JobStartTime)
task.CompletedAt = timeutils.StringToUnixTime(inresp.Data.JobEndTime)
resp.Task = &task
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
resp.Task = nil
}
return resp, nil
case *hpcAC.HistoryJobDetailResp:
var resp types.GetLinkTaskResp
inresp := (interface{})(in).(*hpcAC.HistoryJobDetailResp)
if inresp.Code == "0" {
resp.Success = true
var task types.TaskSl
task.TaskId = inresp.Data.JobId
task.TaskName = inresp.Data.JobName
task.TaskStatus = AcStatus[inresp.Data.JobState]
task.StartedAt = timeutils.StringToUnixTime(inresp.Data.JobStartTime)
task.CompletedAt = timeutils.StringToUnixTime(inresp.Data.JobEndTime)
resp.Task = &task
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
resp.Task = nil
}
return resp, nil
case *octopus.DeleteTrainJobResp:
var resp types.DeleteLinkTaskResp
inresp := (interface{})(in).(*octopus.DeleteTrainJobResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ErrorMsg = inresp.Error.Message
return resp, nil
}
return resp, nil
case *modelarts.DeleteTrainingJobResp:
var resp types.DeleteLinkTaskResp
inresp := (interface{})(in).(*modelarts.DeleteTrainingJobResp)
if inresp.ErrorMsg != "" {
resp.ErrorMsg = inresp.ErrorMsg
resp.Success = false
return resp, nil
}
resp.Success = true
return resp, nil
case *hpcAC.DeleteTaskAiResp:
var resp types.DeleteLinkTaskResp
inresp := (interface{})(in).(*hpcAC.DeleteTaskAiResp)
if inresp.Code == "0" {
resp.Success = true
} else {
resp.Success = false
resp.ErrorMsg = inresp.Msg
}
return resp, nil
case *octopus.GetResourceSpecsResp:
var resp types.GetResourceSpecsResp
inresp := (interface{})(in).(*octopus.GetResourceSpecsResp)
resp.Success = inresp.Success
if !resp.Success {
resp.ResourceSpecs = nil
return resp, nil
}
for _, spec := range inresp.TrainResourceSpecs {
var respec types.ResourceSpecSl
respec.SpecId = spec.Id
respec.SpecName = spec.Name
respec.ParticipantId = participant.Id
respec.ParticipantName = participant.Name
respec.SpecPrice = spec.Price
resp.ResourceSpecs = append(resp.ResourceSpecs, &respec)
}
return resp, nil
case *hpcAC.GetResourceSpecResp:
var resp types.GetResourceSpecsResp
inresp := (interface{})(in).(*hpcAC.GetResourceSpecResp)
if inresp.Code != "0" {
resp.Success = false
resp.ResourceSpecs = nil
} else {
var spec types.ResourceSpecSl
resp.Success = true
spec.ParticipantName = participant.Name
spec.ParticipantId = participant.Id
spec.SpecName = SHUGUANGAI_CUSTOM_RESOURCE_NAME
spec.SpecId = SHUGUANGAI_CUSTOM_RESOURCE_ID
resp.ResourceSpecs = append(resp.ResourceSpecs, &spec)
}
return resp, nil
case *modelarts.TrainingJobFlavorsResp:
var resp types.GetResourceSpecsResp
resp.Success = true
inresp := (interface{})(in).(*modelarts.TrainingJobFlavorsResp)
if inresp.Flavors == nil {
resp.Success = false
resp.ResourceSpecs = nil
return resp, nil
}
for _, spec := range inresp.Flavors {
var respec types.ResourceSpecSl
respec.SpecId = spec.FlavorId
respec.SpecName = spec.FlavorName
respec.ParticipantId = participant.Id
respec.ParticipantName = participant.Name
respec.SpecPrice = 0
resp.ResourceSpecs = append(resp.ResourceSpecs, &respec)
}
return resp, nil
default:
return nil, errors.New("type convert fail")
}
}