package repo import ( "errors" "os" "os/exec" "strconv" "strings" "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/cloudbrain" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) const ( tplCloudBrainIndex base.TplName = "repo/cloudbrain/index" tplCloudBrainNew base.TplName = "repo/cloudbrain/new" tplCloudBrainShow base.TplName = "repo/cloudbrain/show" ) // MustEnableDataset check if repository enable internal cb func MustEnableCloudbrain(ctx *context.Context) { if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) { ctx.NotFound("MustEnableCloudbrain", nil) return } } func CloudBrainIndex(ctx *context.Context) { MustEnableCloudbrain(ctx) repo := ctx.Repo.Repository page := ctx.QueryInt("page") if page <= 0 { page = 1 } ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{ ListOptions: models.ListOptions{ Page: page, PageSize: setting.UI.IssuePagingNum, }, RepoID: repo.ID, // SortType: sortType, }) if err != nil { ctx.ServerError("Cloudbrain", err) return } timestamp := time.Now().Unix() for i, task := range ciTasks { if task.Status == string(models.JobRunning) && (timestamp - int64(task.CreatedUnix) > 30){ ciTasks[i].CanDebug = true } else { ciTasks[i].CanDebug = false } } pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) pager.SetDefaultParams(ctx) ctx.Data["Page"] = pager ctx.Data["PageIsCloudBrain"] = true ctx.Data["Tasks"] = ciTasks ctx.HTML(200, tplCloudBrainIndex) } func cutString(str string, lens int) string { if len(str) < lens { return str } return str[:lens] } func CloudBrainNew(ctx *context.Context) { ctx.Data["PageIsCloudBrain"] = true t := time.Now() var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:] ctx.Data["job_name"] = jobName result, err := cloudbrain.GetImages() if err != nil { ctx.Data["error"] = err.Error() } for i,payload := range result.Payload { if strings.HasPrefix(result.Payload[i].Place,"192.168") { result.Payload[i].PlaceView = payload.Place[strings.Index(payload.Place, "/"): len(payload.Place)-1] } else { result.Payload[i].PlaceView = payload.Place } } ctx.Data["images"] = result.Payload attachs, err := models.GetAllUserAttachments(ctx.User.ID) if err != nil { ctx.ServerError("GetAllUserAttachments failed:", err) return } ctx.Data["attachments"] = attachs ctx.Data["command"] = cloudbrain.Command ctx.Data["code_path"] = cloudbrain.CodeMountPath ctx.Data["dataset_path"] = cloudbrain.DataSetMountPath ctx.Data["model_path"] = cloudbrain.ModelMountPath ctx.HTML(200, tplCloudBrainNew) } func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) { ctx.Data["PageIsCloudBrain"] = true jobName := form.JobName image := form.Image command := form.Command uuid := form.Attachment codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath repo := ctx.Repo.Repository err := downloadCode(repo, codePath) if err != nil { ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) return } modelPath := setting.JobPath + jobName + "/model" err = os.MkdirAll(modelPath, os.ModePerm) if err != nil { ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) return } err = cloudbrain.GenerateTask(ctx, jobName, image, command, uuid, codePath, modelPath) if err != nil { ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) return } ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") } func CloudBrainShow(ctx *context.Context) { ctx.Data["PageIsCloudBrain"] = true var jobID = ctx.Params(":jobid") task, err := models.GetCloudbrainByJobID(jobID) if err != nil { ctx.Data["error"] = err.Error() } result, err := cloudbrain.GetJob(jobID) if err != nil { ctx.Data["error"] = err.Error() } if result != nil { jobRes, _ := models.ConvertToJobResultPayload(result.Payload) ctx.Data["result"] = jobRes taskRoles := jobRes.TaskRoles taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{})) ctx.Data["taskRes"] = taskRes task.Status = taskRes.TaskStatuses[0].State task.ContainerID = taskRes.TaskStatuses[0].ContainerID task.ContainerIp = taskRes.TaskStatuses[0].ContainerIP err = models.UpdateJob(task) if err != nil { ctx.Data["error"] = err.Error() } } ctx.Data["task"] = task ctx.Data["jobID"] = jobID ctx.HTML(200, tplCloudBrainShow) } func CloudBrainDebug(ctx *context.Context) { var jobID = ctx.Params(":jobid") task, err := models.GetCloudbrainByJobID(jobID) if err != nil { ctx.ServerError("GetCloudbrainByJobID failed", err) return } debugUrl := setting.DebugServerHost + "jpylab_" + task.JobID + "_" + task.SubTaskName ctx.Redirect(debugUrl) } func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { var jobID = ctx.Params(":jobid") task, err := models.GetCloudbrainByJobID(jobID) if err != nil { ctx.ServerError("GetCloudbrainByJobID failed", err) return } err = cloudbrain.CommitImage(jobID, models.CommitImageParams{ Ip: task.ContainerIp, TaskContainerId: task.ContainerID, ImageDescription: form.Description, ImageTag: form.Tag, }) if err != nil { log.Error("CommitImage(%s) failed:", task.JobName, err.Error()) ctx.ServerError("CommitImage failed", err) return } ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") } func CloudBrainStop(ctx *context.Context) { var jobID = ctx.Params(":jobid") log.Info(jobID) task, err := models.GetCloudbrainByJobID(jobID) if err != nil { ctx.ServerError("GetCloudbrainByJobID failed", err) return } if task.Status != string(models.JobRunning) { log.Error("the job(%s) is not running", task.JobName) ctx.ServerError("the job is not running", errors.New("the job is not running")) return } err = cloudbrain.StopJob(jobID) if err != nil { log.Error("StopJob(%s) failed:%v", task.JobName, err.Error()) ctx.ServerError("StopJob failed", err) return } task.Status = string(models.JobStopped) err = models.UpdateJob(task) if err != nil { ctx.ServerError("UpdateJob failed", err) return } ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") } func downloadCode(repo *models.Repository, codePath string) error { /* if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{ Bare: true, Shared: true, }); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) return "", fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) } */ err := os.MkdirAll(codePath, os.ModePerm) if err != nil { log.Error("MkdirAll failed:%v", err) return err } command := "git clone " + repo.CloneLink().HTTPS + " " + codePath cmd := exec.Command("/bin/bash", "-c", command) output, err := cmd.Output() log.Info(string(output)) if err != nil { log.Error("exec.Command(%s) failed:%v", command, err) return err } return nil }