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.

octopus.go 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package storeLink
  2. import (
  3. "context"
  4. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
  5. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  6. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  7. "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopus"
  8. "strings"
  9. )
  10. type OctopusLink struct {
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. pageIndex int32
  14. pageSize int32
  15. participant *models.ScParticipantPhyInfo
  16. }
  17. const (
  18. IMG_NAME_PREFIX = "oct_"
  19. IMG_VERSION_PREFIX = "version_"
  20. TASK_NAME_PREFIX = "trainJob_"
  21. RESOURCE_POOL = "common-pool"
  22. )
  23. func NewOctopusLink(ctx context.Context, svcCtx *svc.ServiceContext, participant *models.ScParticipantPhyInfo) *OctopusLink {
  24. return &OctopusLink{ctx: ctx, svcCtx: svcCtx, participant: participant, pageIndex: 1, pageSize: 100}
  25. }
  26. func (o *OctopusLink) UploadImage(path string) (interface{}, error) {
  27. // octopus创建镜像
  28. createReq := &octopus.CreateImageReq{
  29. Platform: o.participant.Name,
  30. CreateImage: &octopus.CreateImage{
  31. SourceType: 1,
  32. ImageName: IMG_NAME_PREFIX + utils.RandomString(7),
  33. ImageVersion: IMG_VERSION_PREFIX + utils.RandomString(7),
  34. },
  35. }
  36. createResp, err := o.svcCtx.OctopusRpc.CreateImage(o.ctx, createReq)
  37. if err != nil {
  38. return nil, err
  39. }
  40. // octopus上传镜像
  41. uploadReq := &octopus.UploadImageReq{
  42. Platform: o.participant.Name,
  43. ImageId: createResp.Payload.ImageId,
  44. Params: &octopus.UploadImageParam{
  45. Domain: "",
  46. FileName: "",
  47. },
  48. }
  49. uploadResp, err := o.svcCtx.OctopusRpc.UploadImage(o.ctx, uploadReq)
  50. if err != nil {
  51. return nil, err
  52. }
  53. // Todo 实际上传
  54. //转换成统一返回类型
  55. resp, err := ConvertType[octopus.UploadImageResp](uploadResp, nil)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return resp, nil
  60. }
  61. func (o *OctopusLink) DeleteImage(imageId string) (interface{}, error) {
  62. // octopus删除镜像
  63. req := &octopus.DeleteImageReq{
  64. Platform: o.participant.Name,
  65. ImageId: imageId,
  66. }
  67. resp, err := o.svcCtx.OctopusRpc.DeleteImage(o.ctx, req)
  68. if err != nil {
  69. return nil, err
  70. }
  71. //转换成统一返回类型
  72. deleteResp, err := ConvertType[octopus.DeleteImageResp](resp, nil)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return deleteResp, nil
  77. }
  78. func (o *OctopusLink) QueryImageList() (interface{}, error) {
  79. // octopus获取镜像列表
  80. req := &octopus.GetUserImageListReq{
  81. Platform: o.participant.Name,
  82. PageIndex: o.pageIndex,
  83. PageSize: o.pageSize,
  84. }
  85. resp, err := o.svcCtx.OctopusRpc.GetUserImageList(o.ctx, req)
  86. if err != nil {
  87. return nil, err
  88. }
  89. //转换成统一返回类型
  90. imgListResp, err := ConvertType[octopus.GetUserImageListResp](resp, nil)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return imgListResp, nil
  95. }
  96. func (o *OctopusLink) SubmitTask(imageId string, cmd string, params []string, resourceId string) (interface{}, error) {
  97. // octopus提交任务
  98. var prms []*octopus.Parameters
  99. for _, param := range params {
  100. var p octopus.Parameters
  101. s := strings.Split(param, COMMA)
  102. p.Key = s[0]
  103. p.Value = s[1]
  104. prms = append(prms, &p)
  105. }
  106. req := &octopus.CreateTrainJobReq{
  107. Platform: o.participant.Name,
  108. Params: &octopus.CreateTrainJobParam{
  109. ImageId: imageId,
  110. Name: TASK_NAME_PREFIX + utils.RandomString(7),
  111. ResourcePool: RESOURCE_POOL,
  112. Config: []*octopus.Config{
  113. {
  114. Command: cmd,
  115. ResourceSpecId: resourceId,
  116. MinFailedTaskCount: 1,
  117. MinSucceededTaskCount: 1,
  118. TaskNumber: 1,
  119. Parameters: prms,
  120. },
  121. },
  122. },
  123. }
  124. resp, err := o.svcCtx.OctopusRpc.CreateTrainJob(o.ctx, req)
  125. if err != nil {
  126. return nil, err
  127. }
  128. //转换成统一返回类型
  129. submitResp, err := ConvertType[octopus.CreateTrainJobResp](resp, nil)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return submitResp, nil
  134. }
  135. func (o *OctopusLink) QueryTask(taskId string) (interface{}, error) {
  136. // octopus获取任务
  137. req := &octopus.GetTrainJobReq{
  138. Platform: o.participant.Name,
  139. Id: taskId,
  140. }
  141. resp, err := o.svcCtx.OctopusRpc.GetTrainJob(o.ctx, req)
  142. if err != nil {
  143. return nil, err
  144. }
  145. //转换成统一返回类型
  146. taskResp, err := ConvertType[octopus.GetTrainJobResp](resp, nil)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return taskResp, nil
  151. }
  152. func (o *OctopusLink) DeleteTask(taskId string) (interface{}, error) {
  153. // octopus删除任务
  154. req := &octopus.DeleteTrainJobReq{
  155. Platform: o.participant.Name,
  156. JobIds: []string{taskId},
  157. }
  158. resp, err := o.svcCtx.OctopusRpc.DeleteTrainJob(o.ctx, req)
  159. if err != nil {
  160. return nil, err
  161. }
  162. //转换成统一返回类型
  163. deleteResp, err := ConvertType[octopus.DeleteTrainJobResp](resp, nil)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return deleteResp, nil
  168. }
  169. func (o *OctopusLink) QuerySpecs() (interface{}, error) {
  170. // octopus查询资源规格
  171. req := &octopus.GetResourceSpecsReq{
  172. Platform: o.participant.Name,
  173. ResourcePool: "common-pool",
  174. }
  175. resp, err := o.svcCtx.OctopusRpc.GetResourceSpecs(o.ctx, req)
  176. if err != nil {
  177. return nil, err
  178. }
  179. //转换成统一返回类型
  180. specsResp, err := ConvertType[octopus.GetResourceSpecsResp](resp, o.participant)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return specsResp, nil
  185. }

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.