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

4 years ago
4 years ago
4 years ago
4 years ago
5 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
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 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
4 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
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package repo
  2. import (
  3. "errors"
  4. "os"
  5. "os/exec"
  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)-1]
  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. err := downloadCode(repo, codePath)
  107. if err != nil {
  108. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  109. return
  110. }
  111. modelPath := setting.JobPath + jobName + "/model"
  112. err = os.MkdirAll(modelPath, os.ModePerm)
  113. if err != nil {
  114. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  115. return
  116. }
  117. err = cloudbrain.GenerateTask(ctx, jobName, image, command, uuid, codePath, modelPath)
  118. if err != nil {
  119. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  120. return
  121. }
  122. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  123. }
  124. func CloudBrainShow(ctx *context.Context) {
  125. ctx.Data["PageIsCloudBrain"] = true
  126. var jobID = ctx.Params(":jobid")
  127. task, err := models.GetCloudbrainByJobID(jobID)
  128. if err != nil {
  129. ctx.Data["error"] = err.Error()
  130. }
  131. result, err := cloudbrain.GetJob(jobID)
  132. if err != nil {
  133. ctx.Data["error"] = err.Error()
  134. }
  135. if result != nil {
  136. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  137. ctx.Data["result"] = jobRes
  138. taskRoles := jobRes.TaskRoles
  139. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  140. ctx.Data["taskRes"] = taskRes
  141. task.Status = taskRes.TaskStatuses[0].State
  142. task.ContainerID = taskRes.TaskStatuses[0].ContainerID
  143. task.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  144. err = models.UpdateJob(task)
  145. if err != nil {
  146. ctx.Data["error"] = err.Error()
  147. }
  148. }
  149. ctx.Data["task"] = task
  150. ctx.Data["jobID"] = jobID
  151. ctx.HTML(200, tplCloudBrainShow)
  152. }
  153. func CloudBrainDebug(ctx *context.Context) {
  154. var jobID = ctx.Params(":jobid")
  155. task, err := models.GetCloudbrainByJobID(jobID)
  156. if err != nil {
  157. ctx.ServerError("GetCloudbrainByJobID failed", err)
  158. return
  159. }
  160. debugUrl := setting.DebugServerHost + "jpylab_" + task.JobID + "_" + task.SubTaskName
  161. ctx.Redirect(debugUrl)
  162. }
  163. func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) {
  164. var jobID = ctx.Params(":jobid")
  165. task, err := models.GetCloudbrainByJobID(jobID)
  166. if err != nil {
  167. ctx.ServerError("GetCloudbrainByJobID failed", err)
  168. return
  169. }
  170. err = cloudbrain.CommitImage(jobID, models.CommitImageParams{
  171. Ip: task.ContainerIp,
  172. TaskContainerId: task.ContainerID,
  173. ImageDescription: form.Description,
  174. ImageTag: form.Tag,
  175. })
  176. if err != nil {
  177. log.Error("CommitImage(%s) failed:", task.JobName, err.Error())
  178. ctx.ServerError("CommitImage failed", err)
  179. return
  180. }
  181. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  182. }
  183. func CloudBrainStop(ctx *context.Context) {
  184. var jobID = ctx.Params(":jobid")
  185. log.Info(jobID)
  186. task, err := models.GetCloudbrainByJobID(jobID)
  187. if err != nil {
  188. ctx.ServerError("GetCloudbrainByJobID failed", err)
  189. return
  190. }
  191. if task.Status != string(models.JobRunning) {
  192. log.Error("the job(%s) is not running", task.JobName)
  193. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  194. return
  195. }
  196. err = cloudbrain.StopJob(jobID)
  197. if err != nil {
  198. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  199. ctx.ServerError("StopJob failed", err)
  200. return
  201. }
  202. task.Status = string(models.JobStopped)
  203. err = models.UpdateJob(task)
  204. if err != nil {
  205. ctx.ServerError("UpdateJob failed", err)
  206. return
  207. }
  208. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  209. }
  210. func downloadCode(repo *models.Repository, codePath string) error {
  211. /*
  212. if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{
  213. Bare: true,
  214. Shared: true,
  215. }); err != nil {
  216. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  217. return "", fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  218. }
  219. */
  220. err := os.MkdirAll(codePath, os.ModePerm)
  221. if err != nil {
  222. log.Error("MkdirAll failed:%v", err)
  223. return err
  224. }
  225. command := "git clone " + repo.CloneLink().HTTPS + " " + codePath
  226. cmd := exec.Command("/bin/bash", "-c", command)
  227. output, err := cmd.Output()
  228. log.Info(string(output))
  229. if err != nil {
  230. log.Error("exec.Command(%s) failed:%v", command, err)
  231. return err
  232. }
  233. return nil
  234. }