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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/modules/git"
  4. "errors"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/cloudbrain"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. const (
  18. tplCloudBrainIndex base.TplName = "repo/cloudbrain/index"
  19. tplCloudBrainNew base.TplName = "repo/cloudbrain/new"
  20. tplCloudBrainShow base.TplName = "repo/cloudbrain/show"
  21. )
  22. // MustEnableDataset check if repository enable internal cb
  23. func MustEnableCloudbrain(ctx *context.Context) {
  24. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  25. ctx.NotFound("MustEnableCloudbrain", nil)
  26. return
  27. }
  28. }
  29. func CloudBrainIndex(ctx *context.Context) {
  30. MustEnableCloudbrain(ctx)
  31. repo := ctx.Repo.Repository
  32. page := ctx.QueryInt("page")
  33. if page <= 0 {
  34. page = 1
  35. }
  36. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  37. ListOptions: models.ListOptions{
  38. Page: page,
  39. PageSize: setting.UI.IssuePagingNum,
  40. },
  41. RepoID: repo.ID,
  42. // SortType: sortType,
  43. })
  44. if err != nil {
  45. ctx.ServerError("Cloudbrain", err)
  46. return
  47. }
  48. timestamp := time.Now().Unix()
  49. for i, task := range ciTasks {
  50. if task.Status == string(models.JobRunning) && (timestamp - int64(task.CreatedUnix) > 30){
  51. ciTasks[i].CanDebug = true
  52. } else {
  53. ciTasks[i].CanDebug = false
  54. }
  55. }
  56. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  57. pager.SetDefaultParams(ctx)
  58. ctx.Data["Page"] = pager
  59. ctx.Data["PageIsCloudBrain"] = true
  60. ctx.Data["Tasks"] = ciTasks
  61. ctx.HTML(200, tplCloudBrainIndex)
  62. }
  63. func cutString(str string, lens int) string {
  64. if len(str) < lens {
  65. return str
  66. }
  67. return str[:lens]
  68. }
  69. func CloudBrainNew(ctx *context.Context) {
  70. ctx.Data["PageIsCloudBrain"] = true
  71. t := time.Now()
  72. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  73. ctx.Data["job_name"] = jobName
  74. result, err := cloudbrain.GetImages()
  75. if err != nil {
  76. ctx.Data["error"] = err.Error()
  77. }
  78. for i,payload := range result.Payload {
  79. if strings.HasPrefix(result.Payload[i].Place,"192.168") {
  80. result.Payload[i].PlaceView = payload.Place[strings.Index(payload.Place, "/"): len(payload.Place)]
  81. } else {
  82. result.Payload[i].PlaceView = payload.Place
  83. }
  84. }
  85. ctx.Data["images"] = result.Payload
  86. attachs, err := models.GetAllUserAttachments(ctx.User.ID)
  87. if err != nil {
  88. ctx.ServerError("GetAllUserAttachments failed:", err)
  89. return
  90. }
  91. ctx.Data["attachments"] = attachs
  92. ctx.Data["command"] = cloudbrain.Command
  93. ctx.Data["code_path"] = cloudbrain.CodeMountPath
  94. ctx.Data["dataset_path"] = cloudbrain.DataSetMountPath
  95. ctx.Data["model_path"] = cloudbrain.ModelMountPath
  96. ctx.HTML(200, tplCloudBrainNew)
  97. }
  98. func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) {
  99. ctx.Data["PageIsCloudBrain"] = true
  100. jobName := form.JobName
  101. image := form.Image
  102. command := form.Command
  103. uuid := form.Attachment
  104. codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath
  105. repo := ctx.Repo.Repository
  106. downloadCode(repo, codePath)
  107. modelPath := setting.JobPath + jobName + "/model"
  108. err := os.MkdirAll(modelPath, os.ModePerm)
  109. if err != nil {
  110. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  111. return
  112. }
  113. err = cloudbrain.GenerateTask(ctx, jobName, image, command, uuid, codePath, modelPath)
  114. if err != nil {
  115. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  116. return
  117. }
  118. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  119. }
  120. func CloudBrainShow(ctx *context.Context) {
  121. ctx.Data["PageIsCloudBrain"] = true
  122. var jobID = ctx.Params(":jobid")
  123. task, err := models.GetCloudbrainByJobID(jobID)
  124. if err != nil {
  125. ctx.Data["error"] = err.Error()
  126. }
  127. result, err := cloudbrain.GetJob(jobID)
  128. if err != nil {
  129. ctx.Data["error"] = err.Error()
  130. }
  131. if result != nil {
  132. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  133. ctx.Data["result"] = jobRes
  134. taskRoles := jobRes.TaskRoles
  135. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  136. ctx.Data["taskRes"] = taskRes
  137. task.Status = taskRes.TaskStatuses[0].State
  138. task.ContainerID = taskRes.TaskStatuses[0].ContainerID
  139. task.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  140. err = models.UpdateJob(task)
  141. if err != nil {
  142. ctx.Data["error"] = err.Error()
  143. }
  144. }
  145. ctx.Data["task"] = task
  146. ctx.Data["jobID"] = jobID
  147. ctx.HTML(200, tplCloudBrainShow)
  148. }
  149. func CloudBrainDebug(ctx *context.Context) {
  150. var jobID = ctx.Params(":jobid")
  151. task, err := models.GetCloudbrainByJobID(jobID)
  152. if err != nil {
  153. ctx.ServerError("GetCloudbrainByJobID failed", err)
  154. return
  155. }
  156. debugUrl := setting.DebugServerHost + "jpylab_" + task.JobID + "_" + task.SubTaskName
  157. ctx.Redirect(debugUrl)
  158. }
  159. func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) {
  160. var jobID = ctx.Params(":jobid")
  161. task, err := models.GetCloudbrainByJobID(jobID)
  162. if err != nil {
  163. ctx.ServerError("GetCloudbrainByJobID failed", err)
  164. return
  165. }
  166. err = cloudbrain.CommitImage(jobID, models.CommitImageParams{
  167. Ip: task.ContainerIp,
  168. TaskContainerId: task.ContainerID,
  169. ImageDescription: form.Description,
  170. ImageTag: form.Tag,
  171. })
  172. if err != nil {
  173. log.Error("CommitImage(%s) failed:", task.JobName, err.Error())
  174. ctx.ServerError("CommitImage failed", err)
  175. return
  176. }
  177. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  178. }
  179. func CloudBrainStop(ctx *context.Context) {
  180. var jobID = ctx.Params(":jobid")
  181. log.Info(jobID)
  182. task, err := models.GetCloudbrainByJobID(jobID)
  183. if err != nil {
  184. ctx.ServerError("GetCloudbrainByJobID failed", err)
  185. return
  186. }
  187. if task.Status != string(models.JobRunning) {
  188. log.Error("the job(%s) is not running", task.JobName)
  189. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  190. return
  191. }
  192. err = cloudbrain.StopJob(jobID)
  193. if err != nil {
  194. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  195. ctx.ServerError("StopJob failed", err)
  196. return
  197. }
  198. task.Status = string(models.JobStopped)
  199. err = models.UpdateJob(task)
  200. if err != nil {
  201. ctx.ServerError("UpdateJob failed", err)
  202. return
  203. }
  204. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  205. }
  206. func downloadCode(repo *models.Repository, codePath string) error {
  207. if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{}); err != nil {
  208. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  209. return err
  210. }
  211. return nil
  212. }