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 2.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package repo
  2. import (
  3. "strconv"
  4. "time"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/auth"
  7. "code.gitea.io/gitea/modules/base"
  8. "code.gitea.io/gitea/modules/cloudbrain"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. const (
  13. tplCloudBrainIndex base.TplName = "repo/cloudbrain/index"
  14. tplCloudBrainNew base.TplName = "repo/cloudbrain/new"
  15. tplCloudBrainShow base.TplName = "repo/cloudbrain/show"
  16. )
  17. // MustEnableDataset check if repository enable internal cb
  18. func MustEnableCloudbrain(ctx *context.Context) {
  19. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  20. ctx.NotFound("MustEnableCloudbrain", nil)
  21. return
  22. }
  23. }
  24. func CloudBrainIndex(ctx *context.Context) {
  25. MustEnableCloudbrain(ctx)
  26. repo := ctx.Repo.Repository
  27. page := ctx.QueryInt("page")
  28. if page <= 0 {
  29. page = 1
  30. }
  31. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  32. ListOptions: models.ListOptions{
  33. Page: page,
  34. PageSize: setting.UI.IssuePagingNum,
  35. },
  36. RepoID: repo.ID,
  37. // SortType: sortType,
  38. })
  39. if err != nil {
  40. ctx.ServerError("Cloudbrain", err)
  41. return
  42. }
  43. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  44. pager.SetDefaultParams(ctx)
  45. ctx.Data["Page"] = pager
  46. ctx.Data["PageIsCloudBrain"] = true
  47. ctx.Data["Tasks"] = ciTasks
  48. ctx.HTML(200, tplCloudBrainIndex)
  49. }
  50. func cutString(str string, lens int) string {
  51. if len(str) < lens {
  52. return str
  53. }
  54. return str[:lens]
  55. }
  56. func CloudBrainNew(ctx *context.Context) {
  57. ctx.Data["PageIsCloudBrain"] = true
  58. t := time.Now()
  59. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[:5]
  60. ctx.Data["job_name"] = jobName
  61. ctx.Data["image"] = "192.168.202.74:5000/user-images/deepo:v2.0"
  62. ctx.Data["command"] = `pip3 install jupyterlab==1.1.4;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir=\"/userhome\" --port=80 --NotebookApp.token=\"\" --LabApp.allow_origin=\"self https://cloudbrain.pcl.ac.cn\"`
  63. ctx.HTML(200, tplCloudBrainNew)
  64. }
  65. func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) {
  66. ctx.Data["PageIsCloudBrain"] = true
  67. jobName := form.JobName
  68. image := form.Image
  69. command := form.Command
  70. err := cloudbrain.GenerateTask(ctx, jobName, image, command)
  71. if err != nil {
  72. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  73. return
  74. }
  75. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  76. }
  77. func CloudBrainShow(ctx *context.Context) {
  78. ctx.Data["PageIsCloudBrain"] = true
  79. var jobID = ctx.Params(":jobid")
  80. task, err := models.GetCloudbrainByJobID(jobID)
  81. if err != nil {
  82. ctx.Data["error"] = err.Error()
  83. }
  84. ctx.Data["task"] = task
  85. result, err := cloudbrain.GetJob(jobID)
  86. if err != nil {
  87. ctx.Data["error"] = err.Error()
  88. }
  89. if result != nil {
  90. ctx.Data["result"] = result
  91. }
  92. ctx.Data["jobID"] = jobID
  93. ctx.HTML(200, tplCloudBrainShow)
  94. }