You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

storeLink.go 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package storeLink
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
  6. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
  7. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  8. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils/timeutils"
  9. "gitlink.org.cn/jcce-pcm/pcm-participant-ac/hpcAC"
  10. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/modelarts"
  11. "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopus"
  12. "gorm.io/gorm"
  13. "strconv"
  14. )
  15. type Linkage interface {
  16. UploadImage(path string) (interface{}, error)
  17. DeleteImage(imageId string) (interface{}, error)
  18. QueryImageList() (interface{}, error)
  19. SubmitTask(imageId string, cmd string, params []string, resourceId string) (interface{}, error)
  20. QueryTask(taskId string) (interface{}, error)
  21. QuerySpecs() (interface{}, error)
  22. DeleteTask(taskId string) (interface{}, error)
  23. }
  24. const (
  25. COMMA = ","
  26. TYPE_OCTOPUS = "1"
  27. TYPE_MODELARTS = "2"
  28. TYPE_SHUGUANGAI = "3"
  29. OCTOPUS = "Octopus"
  30. MODELARTS = "Modelarts"
  31. SHUGUANGAI = "ShuguangAi"
  32. DCU = "dcu"
  33. PYTORCH = "Pytorch"
  34. TASK_PYTORCH_PREFIX = "PytorchTask"
  35. TENSORFLOW = "Tensorflow"
  36. RESOURCE_GROUP = "wzhdtest"
  37. WorkPath = "/work/home/acgnnmfbwo/111111/py/"
  38. TimeoutLimit = "10:00:00"
  39. PythonCodePath = "/work/home/acgnnmfbwo/111111/py/test.py"
  40. )
  41. var (
  42. OctImgStatus = map[int32]string{
  43. 1: "未上传",
  44. 3: "制作完成",
  45. 4: "制作失败",
  46. }
  47. AITYPE = map[string]string{
  48. "1": OCTOPUS,
  49. "2": MODELARTS,
  50. "3": SHUGUANGAI,
  51. }
  52. )
  53. type StoreLink struct {
  54. ILinkage Linkage
  55. }
  56. func NewStoreLink(ctx context.Context, svcCtx *svc.ServiceContext, participant *models.ScParticipantPhyInfo) *StoreLink {
  57. switch participant.Type {
  58. case TYPE_OCTOPUS:
  59. linkStruct := NewOctopusLink(ctx, svcCtx, participant)
  60. return &StoreLink{ILinkage: linkStruct}
  61. case TYPE_MODELARTS:
  62. linkStruct := NewModelArtsLink(ctx, svcCtx, participant)
  63. return &StoreLink{ILinkage: linkStruct}
  64. case TYPE_SHUGUANGAI:
  65. linkStruct := NewShuguangAi(ctx, svcCtx, participant)
  66. return &StoreLink{ILinkage: linkStruct}
  67. default:
  68. return nil
  69. }
  70. }
  71. func GetParticipants(dbEngin *gorm.DB) []*models.ScParticipantPhyInfo {
  72. var participants []*models.ScParticipantPhyInfo
  73. dbEngin.Raw("select * from sc_participant_phy_info where type = 1").Scan(&participants)
  74. return participants
  75. }
  76. func GetParticipantById(partId int64, dbEngin *gorm.DB) *models.ScParticipantPhyInfo {
  77. var participant models.ScParticipantPhyInfo
  78. dbEngin.Raw("select * from sc_participant_phy_info where id = ?", partId).Scan(&participant)
  79. return &participant
  80. }
  81. func ConvertType[T any](in *T, participant *models.ScParticipantPhyInfo) (interface{}, error) {
  82. switch (interface{})(in).(type) {
  83. case *octopus.UploadImageResp:
  84. var resp types.UploadLinkImageResp
  85. inresp := (interface{})(in).(*octopus.UploadImageResp)
  86. resp.Success = inresp.Success
  87. if !resp.Success {
  88. resp.ErrorMsg = inresp.Error.Message
  89. return resp, nil
  90. }
  91. return resp, nil
  92. case *octopus.DeleteImageResp:
  93. var resp types.DeleteLinkImageResp
  94. inresp := (interface{})(in).(*octopus.DeleteImageResp)
  95. resp.Success = inresp.Success
  96. if !resp.Success {
  97. resp.ErrorMsg = inresp.Error.Message
  98. return resp, nil
  99. }
  100. return resp, nil
  101. case *octopus.GetUserImageListResp:
  102. var resp types.GetLinkImageListResp
  103. inresp := (interface{})(in).(*octopus.GetUserImageListResp)
  104. resp.Success = inresp.Success
  105. if !resp.Success {
  106. resp.ErrorMsg = inresp.Error.Message
  107. resp.Images = nil
  108. return resp, nil
  109. }
  110. for _, v := range inresp.Payload.Images {
  111. var image types.ImageSl
  112. image.ImageId = v.Image.Id
  113. image.ImageName = v.Image.ImageName
  114. image.ImageStatus = OctImgStatus[v.Image.ImageStatus]
  115. resp.Images = append(resp.Images, &image)
  116. }
  117. return resp, nil
  118. case *modelarts.ListReposDetailsResp:
  119. var resp types.GetLinkImageListResp
  120. inresp := (interface{})(in).(*modelarts.ListReposDetailsResp)
  121. if inresp.Errors != nil {
  122. resp.Success = false
  123. resp.ErrorMsg = inresp.Errors[0].ErrorMessage
  124. resp.Images = nil
  125. return resp, nil
  126. }
  127. for _, v := range inresp.Items {
  128. for _, r := range v.Tags {
  129. var image types.ImageSl
  130. image.ImageId = v.Namespace + "/" + v.Name + ":" + r
  131. image.ImageName = v.Name
  132. image.ImageStatus = "created"
  133. resp.Images = append(resp.Images, &image)
  134. }
  135. }
  136. return resp, nil
  137. case *hpcAC.GetImageListAiResp:
  138. var resp types.GetLinkImageListResp
  139. inresp := (interface{})(in).(*hpcAC.GetImageListAiResp)
  140. if inresp.Code == "0" {
  141. resp.Success = true
  142. for _, img := range inresp.Data {
  143. var image types.ImageSl
  144. image.ImageId = img.ImageId
  145. image.ImageName = img.Name
  146. image.ImageStatus = "created"
  147. resp.Images = append(resp.Images, &image)
  148. }
  149. } else {
  150. resp.Success = false
  151. resp.ErrorMsg = inresp.Msg
  152. resp.Images = nil
  153. }
  154. return resp, nil
  155. case *octopus.CreateTrainJobResp:
  156. var resp types.SubmitLinkTaskResp
  157. inresp := (interface{})(in).(*octopus.CreateTrainJobResp)
  158. resp.Success = inresp.Success
  159. if !resp.Success {
  160. resp.ErrorMsg = inresp.Error.Message
  161. return resp, nil
  162. }
  163. resp.TaskId = inresp.Payload.JobId
  164. return resp, nil
  165. case *modelarts.CreateTrainingJobResp:
  166. var resp types.SubmitLinkTaskResp
  167. inresp := (interface{})(in).(*modelarts.CreateTrainingJobResp)
  168. if inresp.ErrorMsg != "" {
  169. resp.ErrorMsg = inresp.ErrorMsg
  170. resp.Success = false
  171. return resp, nil
  172. }
  173. resp.TaskId = inresp.Metadata.Id
  174. return resp, nil
  175. case *hpcAC.SubmitTaskAiResp:
  176. var resp types.SubmitLinkTaskResp
  177. inresp := (interface{})(in).(*hpcAC.SubmitTaskAiResp)
  178. if inresp.Code == "0" {
  179. resp.Success = true
  180. resp.TaskId = inresp.Data
  181. } else {
  182. resp.Success = false
  183. resp.ErrorMsg = inresp.Msg
  184. }
  185. return resp, nil
  186. case *octopus.GetTrainJobResp:
  187. var resp types.GetLinkTaskResp
  188. inresp := (interface{})(in).(*octopus.GetTrainJobResp)
  189. resp.Success = inresp.Success
  190. if !resp.Success {
  191. resp.ErrorMsg = inresp.Error.Message
  192. return resp, nil
  193. }
  194. resp.Task.TaskId = inresp.Payload.TrainJob.Id
  195. resp.Task.TaskName = inresp.Payload.TrainJob.Name
  196. resp.Task.StartedAt = inresp.Payload.TrainJob.StartedAt
  197. resp.Task.CompletedAt = inresp.Payload.TrainJob.CompletedAt
  198. resp.Task.TaskStatus = inresp.Payload.TrainJob.Status
  199. return resp, nil
  200. case *modelarts.JobResponse:
  201. var resp types.GetLinkTaskResp
  202. inresp := (interface{})(in).(*modelarts.JobResponse)
  203. if inresp.ErrorMsg != "" {
  204. resp.ErrorMsg = inresp.ErrorMsg
  205. resp.Success = false
  206. return resp, nil
  207. }
  208. resp.Task.TaskId = inresp.Metadata.Id
  209. resp.Task.TaskName = inresp.Metadata.Name
  210. resp.Task.StartedAt = int64(inresp.Status.StartTime)
  211. resp.Task.CompletedAt = int64(inresp.Status.Duration)
  212. resp.Task.TaskStatus = inresp.Status.Phase
  213. return resp, nil
  214. case *hpcAC.GetPytorchTaskResp:
  215. var resp types.GetLinkTaskResp
  216. inresp := (interface{})(in).(*hpcAC.GetPytorchTaskResp)
  217. if inresp.Code == "0" {
  218. resp.Success = true
  219. resp.Task.TaskId = inresp.Data.Id
  220. resp.Task.TaskName = inresp.Data.TaskName
  221. resp.Task.TaskStatus = inresp.Data.Status
  222. resp.Task.StartedAt = timeutils.StringToUnixTime(inresp.Data.StartTime)
  223. resp.Task.CompletedAt = timeutils.StringToUnixTime(inresp.Data.EndTime)
  224. } else {
  225. resp.Success = false
  226. resp.ErrorMsg = inresp.Msg
  227. resp.Task = nil
  228. }
  229. return resp, nil
  230. case *octopus.DeleteTrainJobResp:
  231. var resp types.DeleteLinkTaskResp
  232. inresp := (interface{})(in).(*octopus.DeleteTrainJobResp)
  233. resp.Success = inresp.Success
  234. if !resp.Success {
  235. resp.ErrorMsg = inresp.Error.Message
  236. return resp, nil
  237. }
  238. return resp, nil
  239. case *modelarts.DeleteTrainingJobResp:
  240. var resp types.DeleteLinkTaskResp
  241. inresp := (interface{})(in).(*modelarts.DeleteTrainingJobResp)
  242. if inresp.ErrorMsg != "" {
  243. resp.ErrorMsg = inresp.ErrorMsg
  244. resp.Success = false
  245. return resp, nil
  246. }
  247. return resp, nil
  248. case *hpcAC.DeleteTaskAiResp:
  249. var resp types.DeleteLinkTaskResp
  250. inresp := (interface{})(in).(*hpcAC.DeleteTaskAiResp)
  251. if inresp.Code == "0" {
  252. resp.Success = true
  253. } else {
  254. resp.Success = false
  255. resp.ErrorMsg = inresp.Msg
  256. }
  257. return resp, nil
  258. case *octopus.GetResourceSpecsResp:
  259. var resp types.GetResourceSpecsResp
  260. inresp := (interface{})(in).(*octopus.GetResourceSpecsResp)
  261. resp.Success = inresp.Success
  262. if !resp.Success {
  263. resp.ResourceSpecs = nil
  264. return resp, nil
  265. }
  266. for _, spec := range inresp.TrainResourceSpecs {
  267. var respec types.ResourceSpecSl
  268. respec.SpecId = spec.Id
  269. respec.SpecName = spec.Name
  270. respec.ParticipantId = strconv.FormatInt(participant.Id, 10)
  271. respec.ParticipantName = participant.Name
  272. respec.SpecPrice = spec.Price
  273. resp.ResourceSpecs = append(resp.ResourceSpecs, &respec)
  274. }
  275. return resp, nil
  276. case *hpcAC.GetResourceSpecResp:
  277. var resp types.GetResourceSpecsResp
  278. inresp := (interface{})(in).(*hpcAC.GetResourceSpecResp)
  279. if inresp.Code != "0" {
  280. resp.Success = false
  281. resp.ResourceSpecs = nil
  282. } else {
  283. var spec types.ResourceSpecSl
  284. resp.Success = true
  285. spec.ParticipantName = participant.Name
  286. spec.ParticipantId = strconv.FormatInt(participant.Id, 10)
  287. spec.SpecName = SHUGUANGAI_CUSTOM_RESOURCE_NAME
  288. spec.SpecId = SHUGUANGAI_CUSTOM_RESOURCE_ID
  289. resp.ResourceSpecs = append(resp.ResourceSpecs, &spec)
  290. }
  291. return resp, nil
  292. case *modelarts.TrainingJobFlavorsResp:
  293. var resp types.GetResourceSpecsResp
  294. resp.Success = true
  295. inresp := (interface{})(in).(*modelarts.TrainingJobFlavorsResp)
  296. if inresp.Flavors == nil {
  297. resp.Success = false
  298. resp.ResourceSpecs = nil
  299. return resp, nil
  300. }
  301. for _, spec := range inresp.Flavors {
  302. var respec types.ResourceSpecSl
  303. respec.SpecId = spec.FlavorId
  304. respec.SpecName = spec.FlavorName
  305. respec.ParticipantId = strconv.FormatInt(participant.Id, 10)
  306. respec.ParticipantName = participant.Name
  307. respec.SpecPrice = 0
  308. resp.ResourceSpecs = append(resp.ResourceSpecs, &respec)
  309. }
  310. return resp, nil
  311. default:
  312. return nil, errors.New("type convert fail")
  313. }
  314. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.