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.

cloudbrain.go 5.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package cloudbrain
  2. import (
  3. "errors"
  4. "strconv"
  5. "code.gitea.io/gitea/modules/setting"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. const (
  11. Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"`
  12. CodeMountPath = "/code"
  13. DataSetMountPath = "/dataset"
  14. ModelMountPath = "/model"
  15. BenchMarkMountPath = "/benchmark"
  16. Snn4imagenetMountPath = "/snn4imagenet"
  17. BrainScoreMountPath = "/brainscore"
  18. TaskInfoName = "/taskInfo"
  19. SubTaskName = "task1"
  20. Success = "S000"
  21. )
  22. var (
  23. ResourceSpecs *models.ResourceSpecs
  24. )
  25. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  26. if !ctx.IsSigned {
  27. return false
  28. }
  29. log.Info("is repo owner:" + strconv.FormatBool(ctx.IsUserRepoOwner()))
  30. log.Info("is user admin:" + strconv.FormatBool(ctx.IsUserSiteAdmin()))
  31. if err != nil {
  32. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  33. } else {
  34. log.Info("is job creator:" + strconv.FormatBool(ctx.User.ID == job.UserID))
  35. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  36. }
  37. }
  38. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  39. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  40. }
  41. func CanCreateOrDebugJob(ctx *context.Context) bool {
  42. if !ctx.IsSigned {
  43. return false
  44. }
  45. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  46. }
  47. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  48. return isAdminOrJobCreater(ctx, job, nil)
  49. }
  50. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  51. if !ctx.IsSigned {
  52. return false
  53. }
  54. if err != nil {
  55. return ctx.IsUserSiteAdmin()
  56. } else {
  57. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  58. }
  59. }
  60. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  61. var jobID = ctx.Params(":jobid")
  62. job, err := models.GetCloudbrainByJobID(jobID)
  63. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  64. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  65. }
  66. }
  67. func AdminOrJobCreaterRight(ctx *context.Context) {
  68. var jobID = ctx.Params(":jobid")
  69. job, err := models.GetCloudbrainByJobID(jobID)
  70. if !isAdminOrJobCreater(ctx, job, err) {
  71. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  72. }
  73. }
  74. func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue string, resourceSpecId int) error {
  75. dataActualPath := setting.Attachment.Minio.RealPath +
  76. setting.Attachment.Minio.Bucket + "/" +
  77. setting.Attachment.Minio.BasePath +
  78. models.AttachmentRelativePath(uuid) +
  79. uuid
  80. var resourceSpec *models.ResourceSpec
  81. for _, spec := range ResourceSpecs.ResourceSpec {
  82. if resourceSpecId == spec.Id {
  83. resourceSpec = spec
  84. }
  85. }
  86. if resourceSpec == nil {
  87. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  88. return errors.New("no such resourceSpec")
  89. }
  90. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  91. JobName: jobName,
  92. RetryCount: 1,
  93. GpuType: gpuQueue,
  94. Image: image,
  95. TaskRoles: []models.TaskRole{
  96. {
  97. Name: SubTaskName,
  98. TaskNumber: 1,
  99. MinSucceededTaskCount: 1,
  100. MinFailedTaskCount: 1,
  101. CPUNumber: resourceSpec.CpuNum,
  102. GPUNumber: resourceSpec.GpuNum,
  103. MemoryMB: resourceSpec.MemMiB,
  104. ShmMB: resourceSpec.ShareMemMiB,
  105. Command: command,
  106. NeedIBDevice: false,
  107. IsMainRole: false,
  108. UseNNI: false,
  109. },
  110. },
  111. Volumes: []models.Volume{
  112. {
  113. HostPath: models.StHostPath{
  114. Path: codePath,
  115. MountPath: CodeMountPath,
  116. ReadOnly: false,
  117. },
  118. },
  119. {
  120. HostPath: models.StHostPath{
  121. Path: dataActualPath,
  122. MountPath: DataSetMountPath,
  123. ReadOnly: true,
  124. },
  125. },
  126. {
  127. HostPath: models.StHostPath{
  128. Path: modelPath,
  129. MountPath: ModelMountPath,
  130. ReadOnly: false,
  131. },
  132. },
  133. {
  134. HostPath: models.StHostPath{
  135. Path: benchmarkPath,
  136. MountPath: BenchMarkMountPath,
  137. ReadOnly: true,
  138. },
  139. },
  140. {
  141. HostPath: models.StHostPath{
  142. Path: snn4imagenetPath,
  143. MountPath: Snn4imagenetMountPath,
  144. ReadOnly: true,
  145. },
  146. },
  147. {
  148. HostPath: models.StHostPath{
  149. Path: brainScorePath,
  150. MountPath: BrainScoreMountPath,
  151. ReadOnly: true,
  152. },
  153. },
  154. },
  155. })
  156. if err != nil {
  157. log.Error("CreateJob failed:", err.Error())
  158. return err
  159. }
  160. if jobResult.Code != Success {
  161. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg)
  162. return errors.New(jobResult.Msg)
  163. }
  164. var jobID = jobResult.Payload["jobId"].(string)
  165. err = models.CreateCloudbrain(&models.Cloudbrain{
  166. Status: string(models.JobWaiting),
  167. UserID: ctx.User.ID,
  168. RepoID: ctx.Repo.Repository.ID,
  169. JobID: jobID,
  170. JobName: jobName,
  171. SubTaskName: SubTaskName,
  172. JobType: jobType,
  173. Type: models.TypeCloudBrainOne,
  174. Uuid: uuid,
  175. })
  176. if err != nil {
  177. return err
  178. }
  179. return nil
  180. }