diff --git a/models/attachment.go b/models/attachment.go index 79aa317f2..f9e697966 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -385,34 +385,28 @@ func getPrivateAttachments(e Engine, userID int64) ([]*AttachmentUsername, error return attachments, nil } -/* -func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { - attachsPub, err := getAllPublicAttachments(x) - if err != nil { - log.Error("getAllPublicAttachments failed:%v", err) - return nil, err - } - - attachsPri, err := getPrivateAttachments(x, userID) - if err != nil { - log.Error("getPrivateAttachments failed:%v", err) +func getAllUserAttachments(e Engine, userID int64) ([]*AttachmentUsername, error) { + attachments := make([]*AttachmentUsername, 0, 10) + if err := e.Table("attachment").Join("LEFT", "`user`", "attachment.uploader_id "+ + "= `user`.id").Where("decompress_state= ? and type = ? and (uploader_id= ? or is_private = ?)", DecompressStateDone, TypeCloudBrainOne, userID, false).Find(&attachments); err != nil { return nil, err } - - return append(attachsPub, attachsPri...), nil + return attachments, nil } -*/ +func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { + return getAllUserAttachments(x, userID) +} -func getAllUserAttachments(e Engine, userID int64) ([]*AttachmentUsername, error) { +func getModelArtsUserAttachments(e Engine, userID int64) ([]*AttachmentUsername, error) { attachments := make([]*AttachmentUsername, 0, 10) if err := e.Table("attachment").Join("LEFT", "`user`", "attachment.uploader_id "+ - "= `user`.id").Where("decompress_state= ? and type = ? and (uploader_id= ? or is_private = ?)", DecompressStateDone, TypeCloudBrainOne, userID, false).Find(&attachments); err != nil { + "= `user`.id").Where("type = ? and (uploader_id= ? or is_private = ?)", TypeCloudBrainTwo, userID, false).Find(&attachments); err != nil { return nil, err } return attachments, nil } -func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { - return getAllUserAttachments(x, userID) +func GetModelArtsUserAttachments(userID int64) ([]*AttachmentUsername, error) { + return getModelArtsUserAttachments(x, userID) } diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 80fceb394..9c8d08291 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -407,6 +407,25 @@ type Domain struct { Name string `json:name` } +const ( + ActionStart = "start" + ActionStop = "stop" + ActionRestart = "restart" + ActionQueue = "queue" + ActionDequeue = "dequeue" +) + +type NotebookAction struct { + Action string `json:"action"` +} + +type NotebookActionResult struct { + ErrorCode string `json:"error_code"` + ErrorMsg string `json:"error_msg"` + CurrentStatus string `json:"current_status"` + PreviousState string `json:"previous_state"` +} + func Cloudbrains(opts *CloudbrainsOptions) ([]*Cloudbrain, int64, error) { sess := x.NewSession() defer sess.Close() diff --git a/modules/modelarts/resty.go b/modules/modelarts/resty.go index 97a7bfa93..2c225c603 100755 --- a/modules/modelarts/resty.go +++ b/modules/modelarts/resty.go @@ -145,54 +145,23 @@ sendjob: return &result, nil } -func GetImages() (*models.GetImagesResult, error) { +func StopJob(jobID string, param models.NotebookAction) (*models.NotebookActionResult, error) { checkSetting() client := getRestyClient() - var getImagesResult models.GetImagesResult + var result models.NotebookActionResult retry := 0 sendjob: res, err := client.R(). SetHeader("Content-Type", "application/json"). + SetBody(param). SetAuthToken(TOKEN). - SetResult(&getImagesResult). - Get(HOST + "/rest-server/api/v1/image/list/") - - if err != nil { - return nil, fmt.Errorf("resty GetImages: %v", err) - } - - if res.StatusCode() == http.StatusUnauthorized && retry < 1 { - retry++ - _ = getToken() - goto sendjob - } - - //if len(result.ErrorCode) != 0 { - // return &result, fmt.Errorf("CreateJob failed(%s): %s", result.ErrorCode, result.ErrorMsg) - //} - - return &getImagesResult, nil -} - -func CommitImage(jobID string, params models.CommitImageParams) error { - checkSetting() - client := getRestyClient() - var result models.CommitImageResult - - retry := 0 - -sendjob: - res, err := client.R(). - SetHeader("Content-Type", "application/json"). - SetAuthToken(TOKEN). - SetBody(params). SetResult(&result). - Post(HOST + "/rest-server/api/v1/jobs/" + jobID + "/commitImage") + Post(HOST + "/v1/" + setting.ProjectID + urlNotebook + jobID + "/action") if err != nil { - return fmt.Errorf("resty CommitImage: %v", err) + return &result, fmt.Errorf("resty StopJob: %v", err) } if res.StatusCode() == http.StatusUnauthorized && retry < 1 { @@ -201,40 +170,9 @@ sendjob: goto sendjob } - //if len(result.ErrorCode) != 0 { - // return &result, fmt.Errorf("CreateJob failed(%s): %s", result.ErrorCode, result.ErrorMsg) - //} - - return nil -} - -func StopJob(jobID string) error { - checkSetting() - client := getRestyClient() - var result models.StopJobResult - - retry := 0 - -sendjob: - res, err := client.R(). - SetHeader("Content-Type", "application/json"). - SetAuthToken(TOKEN). - SetResult(&result). - Delete(HOST + "/rest-server/api/v1/jobs/" + jobID) - - if err != nil { - return fmt.Errorf("resty StopJob: %v", err) - } - - if res.StatusCode() == http.StatusUnauthorized && retry < 1 { - retry++ - _ = getToken() - goto sendjob + if len(result.ErrorCode) != 0 { + return &result, fmt.Errorf("CreateJob failed(%s): %s", result.ErrorCode, result.ErrorMsg) } - //if len(result.ErrorCode) != 0 { - // return &result, fmt.Errorf("CreateJob failed(%s): %s", result.ErrorCode, result.ErrorMsg) - //} - - return nil + return &result, nil } diff --git a/routers/repo/modelarts.go b/routers/repo/modelarts.go index ffa51beef..defb0b858 100755 --- a/routers/repo/modelarts.go +++ b/routers/repo/modelarts.go @@ -29,7 +29,7 @@ func MustEnableModelArts(ctx *context.Context) { } } func ModelArtsIndex(ctx *context.Context) { - MustEnableCloudbrain(ctx) + MustEnableModelArts(ctx) repo := ctx.Repo.Repository page := ctx.QueryInt("page") if page <= 0 { @@ -42,7 +42,7 @@ func ModelArtsIndex(ctx *context.Context) { PageSize: setting.UI.IssuePagingNum, }, RepoID: repo.ID, - // SortType: sortType, + Type: models.TypeCloudBrainTwo, }) if err != nil { ctx.ServerError("Cloudbrain", err) @@ -64,7 +64,7 @@ func ModelArtsIndex(ctx *context.Context) { ctx.Data["PageIsCloudBrain"] = true ctx.Data["Tasks"] = ciTasks - ctx.HTML(200, tplCloudBrainIndex) + ctx.HTML(200, tplModelArtsIndex) } func ModelArtsNew(ctx *context.Context) { @@ -74,17 +74,14 @@ func ModelArtsNew(ctx *context.Context) { var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:] ctx.Data["job_name"] = jobName - //attachs, err := models.GetAllUserAttachments(ctx.User.ID) - //if err != nil { - // ctx.ServerError("GetAllUserAttachments failed:", err) - // return - //} + attachs, err := models.GetModelArtsUserAttachments(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["attachments"] = attachs ctx.Data["dataset_path"] = modelarts.DataSetMountPath - //ctx.Data["model_path"] = cloudbrain.ModelMountPath ctx.HTML(200, tplModelArtsNew) } @@ -96,7 +93,7 @@ func ModelArtsCreate(ctx *context.Context, form auth.CreateCloudBrainForm) { err := modelarts.GenerateTask(ctx, jobName, uuid) if err != nil { - ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) + ctx.RenderWithErr(err.Error(), tplModelArtsNew, &form) return } ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts") @@ -134,7 +131,7 @@ func ModelArtsShow(ctx *context.Context) { ctx.Data["task"] = task ctx.Data["jobID"] = jobID - ctx.HTML(200, tplCloudBrainShow) + ctx.HTML(200, tplModelArtsShow) } func ModelArtsDebug(ctx *context.Context) { @@ -151,38 +148,6 @@ func ModelArtsDebug(ctx *context.Context) { ctx.Redirect(debugUrl) } -func ModelArtsCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { - var jobID = ctx.Params(":jobid") - task, err := models.GetCloudbrainByJobID(jobID) - if err != nil { - ctx.JSON(200, map[string]string{ - "result_code": "-1", - "error_msg": "GetCloudbrainByJobID failed", - }) - 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.JSON(200, map[string]string{ - "result_code": "-1", - "error_msg": "CommitImage failed", - }) - return - } - - ctx.JSON(200, map[string]string{ - "result_code": "0", - "error_msg": "", - }) -} - func ModelArtsStop(ctx *context.Context) { var jobID = ctx.Params(":jobid") log.Info(jobID) @@ -198,13 +163,18 @@ func ModelArtsStop(ctx *context.Context) { return } - err = cloudbrain.StopJob(jobID) + param := models.NotebookAction{ + Action: models.ActionStop, + } + res, err := modelarts.StopJob(jobID, param) if err != nil { log.Error("StopJob(%s) failed:%v", task.JobName, err.Error()) ctx.ServerError("StopJob failed", err) return } + log.Info("pre(%s), current(%s)", res.PreviousState, res.CurrentStatus) + task.Status = string(models.JobStopped) err = models.UpdateJob(task) if err != nil { @@ -212,7 +182,7 @@ func ModelArtsStop(ctx *context.Context) { return } - ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") + ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts") } func ModelArtsDel(ctx *context.Context) { @@ -235,16 +205,6 @@ func ModelArtsDel(ctx *context.Context) { return } - ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") + ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts") } -func ModelArtsBenchmark(ctx *context.Context) { - var jobID = ctx.Params(":jobid") - _, err := models.GetCloudbrainByJobID(jobID) - if err != nil { - ctx.ServerError("GetCloudbrainByJobID failed", err) - return - } - - ctx.Redirect(setting.BenchmarkServerHost) -} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 9499890bd..312707314 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -920,8 +920,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:jobid", func() { m.Get("", reqRepoCloudBrainReader, repo.ModelArtsShow) m.Get("/debug", reqRepoCloudBrainReader, repo.ModelArtsDebug) - //m.Post("/stop", reqRepoCloudBrainWriter, repo.CloudBrainStop) - //m.Post("/del", reqRepoCloudBrainWriter, repo.CloudBrainDel) + m.Post("/stop", reqRepoCloudBrainWriter, repo.ModelArtsStop) + m.Post("/del", reqRepoCloudBrainWriter, repo.ModelArtsDel) }) m.Get("/create", reqRepoCloudBrainWriter, repo.ModelArtsNew) m.Post("/create", reqRepoCloudBrainWriter, bindIgnErr(auth.CreateModelArtsForm{}), repo.ModelArtsCreate)