| @@ -580,6 +580,8 @@ type CommitImageParams struct { | |||||
| Topics []string | Topics []string | ||||
| CloudBrainType int | CloudBrainType int | ||||
| UID int64 | UID int64 | ||||
| Place string | |||||
| Type int | |||||
| } | } | ||||
| type CommitImageResult struct { | type CommitImageResult struct { | ||||
| @@ -567,12 +567,12 @@ func isImageStaring(e Engine, userID, imageID int64) bool { | |||||
| } | } | ||||
| func RecommendImage(imageId int64, recommond bool) error { | func RecommendImage(imageId int64, recommond bool) error { | ||||
| image := Image{Type: getRecommondType(recommond)} | |||||
| image := Image{Type: GetRecommondType(recommond)} | |||||
| _, err := x.ID(imageId).Cols("type").Update(image) | _, err := x.ID(imageId).Cols("type").Update(image) | ||||
| return err | return err | ||||
| } | } | ||||
| func getRecommondType(recommond bool) int { | |||||
| func GetRecommondType(recommond bool) int { | |||||
| if recommond { | if recommond { | ||||
| return RECOMMOND_TYPE | return RECOMMOND_TYPE | ||||
| @@ -33,6 +33,16 @@ type CommitImageCloudBrainForm struct { | |||||
| Topics string `form:"topics"` | Topics string `form:"topics"` | ||||
| } | } | ||||
| type CommitAdminImageCloudBrainForm struct { | |||||
| Description string `form:"description" binding:"Required"` | |||||
| Type int `form:"type" binding:"Required"` | |||||
| Tag string `form:"tag" binding:"Required;MaxSize(100)" ` | |||||
| IsPrivate bool `form:"isPrivate" binding:"Required"` | |||||
| Topics string `form:"topics"` | |||||
| Place string `form:"place" binding:"Required"` | |||||
| IsRecommend bool `form:"isRecommend" binding:"Required"` | |||||
| } | |||||
| type EditImageCloudBrainForm struct { | type EditImageCloudBrainForm struct { | ||||
| ID int64 `form:"id" binding:"Required"` | ID int64 `form:"id" binding:"Required"` | ||||
| Description string `form:"description" binding:"Required"` | Description string `form:"description" binding:"Required"` | ||||
| @@ -312,12 +312,51 @@ sendjob: | |||||
| return nil | return nil | ||||
| }) | }) | ||||
| if err == nil { | if err == nil { | ||||
| go updateImageStatus(image, isSetCreatedUnix, createTime) | go updateImageStatus(image, isSetCreatedUnix, createTime) | ||||
| } | } | ||||
| return err | return err | ||||
| } | } | ||||
| func CommitAdminImage(jobID string, params models.CommitImageParams) error { | |||||
| exist, err := models.IsImageExist(params.ImageTag) | |||||
| if err != nil { | |||||
| return fmt.Errorf("resty CommitImage: %v", err) | |||||
| } | |||||
| if exist { | |||||
| return models.ErrorImageTagExist{ | |||||
| Tag: params.ImageTag, | |||||
| } | |||||
| } | |||||
| image := models.Image{ | |||||
| CloudbrainType: params.CloudBrainType, | |||||
| UID: params.UID, | |||||
| IsPrivate: params.IsPrivate, | |||||
| Tag: params.ImageTag, | |||||
| Description: params.ImageDescription, | |||||
| Place: params.Place, | |||||
| Status: models.IMAGE_STATUS_SUCCESS, | |||||
| Type: params.Type, | |||||
| } | |||||
| err = models.WithTx(func(ctx models.DBContext) error { | |||||
| if err := models.CreateLocalImage(&image); err != nil { | |||||
| log.Error("Failed to insert image record.", err) | |||||
| return fmt.Errorf("resty CommitImage: %v", err) | |||||
| } | |||||
| if err := models.SaveImageTopics(image.ID, params.Topics...); err != nil { | |||||
| log.Error("Failed to insert image record.", err) | |||||
| return fmt.Errorf("resty CommitImage: %v", err) | |||||
| } | |||||
| return nil | |||||
| }) | |||||
| return err | |||||
| } | |||||
| func updateImageStatus(image models.Image, isSetCreatedUnix bool, createTime time.Time) { | func updateImageStatus(image models.Image, isSetCreatedUnix bool, createTime time.Time) { | ||||
| attemps := 5 | attemps := 5 | ||||
| commitSuccess := false | commitSuccess := false | ||||
| @@ -21,6 +21,7 @@ import ( | |||||
| const ( | const ( | ||||
| tplCloudBrains base.TplName = "admin/cloudbrain/list" | tplCloudBrains base.TplName = "admin/cloudbrain/list" | ||||
| tplImages base.TplName = "admin/cloudbrain/images" | tplImages base.TplName = "admin/cloudbrain/images" | ||||
| tplCommitImages base.TplName = "admin/cloudbrain/imagecommit" | |||||
| EXCEL_DATE_FORMAT = "20060102150405" | EXCEL_DATE_FORMAT = "20060102150405" | ||||
| CREATE_TIME_FORMAT = "2006/01/02 15:04:05" | CREATE_TIME_FORMAT = "2006/01/02 15:04:05" | ||||
| ) | ) | ||||
| @@ -114,6 +115,12 @@ func Images(ctx *context.Context) { | |||||
| } | } | ||||
| func CloudBrainCommitImageShow(ctx *context.Context) { | |||||
| ctx.Data["PageIsAdminImages"] = true | |||||
| ctx.HTML(200, tplImages) | |||||
| } | |||||
| func DownloadCloudBrains(ctx *context.Context) { | func DownloadCloudBrains(ctx *context.Context) { | ||||
| page := 1 | page := 1 | ||||
| @@ -702,6 +702,55 @@ func CloudBrainCommitImageCheck(ctx *context.Context, form auth.CommitImageCloud | |||||
| } | } | ||||
| func CloudBrainAdminCommitImage(ctx *context.Context, form auth.CommitAdminImageCloudBrainForm) { | |||||
| if !NamePattern.MatchString(form.Tag) { | |||||
| ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err"))) | |||||
| return | |||||
| } | |||||
| if utf8.RuneCountInString(form.Description) > 255 { | |||||
| ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err", 255))) | |||||
| return | |||||
| } | |||||
| validTopics, errMessage := checkTopics(form.Topics) | |||||
| if errMessage != "" { | |||||
| ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr(errMessage))) | |||||
| return | |||||
| } | |||||
| err := cloudbrain.CommitAdminImage(ctx.Cloudbrain.JobID, models.CommitImageParams{ | |||||
| CommitImageCloudBrainParams: models.CommitImageCloudBrainParams{ | |||||
| Ip: ctx.Cloudbrain.ContainerIp, | |||||
| TaskContainerId: ctx.Cloudbrain.ContainerID, | |||||
| ImageDescription: form.Description, | |||||
| ImageTag: form.Tag, | |||||
| }, | |||||
| IsPrivate: form.IsPrivate, | |||||
| CloudBrainType: form.Type, | |||||
| Topics: validTopics, | |||||
| UID: ctx.User.ID, | |||||
| Type: models.GetRecommondType(form.IsRecommend), | |||||
| Place: form.Place, | |||||
| }) | |||||
| if err != nil { | |||||
| log.Error("CommitImage(%s) failed:%v", ctx.Cloudbrain.JobName, err.Error(), ctx.Data["msgID"]) | |||||
| if models.IsErrImageTagExist(err) { | |||||
| ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_exist"))) | |||||
| } else if models.IsErrorImageCommitting(err) { | |||||
| ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_committing"))) | |||||
| } else { | |||||
| ctx.JSON(200, models.BaseErrorMessage(ctx.Tr("repo.image_commit_fail"))) | |||||
| } | |||||
| return | |||||
| } | |||||
| ctx.JSON(200, models.BaseOKMessage) | |||||
| } | |||||
| func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { | func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { | ||||
| if !NamePattern.MatchString(form.Tag) { | if !NamePattern.MatchString(form.Tag) { | ||||
| @@ -534,6 +534,8 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Group("/images", func() { | m.Group("/images", func() { | ||||
| m.Get("", admin.Images) | m.Get("", admin.Images) | ||||
| m.Get("/data", repo.GetAllImages) | m.Get("/data", repo.GetAllImages) | ||||
| m.Get("/commit_image", admin.CloudBrainCommitImageShow) | |||||
| m.Post("/commit_image", bindIgnErr(auth.CommitAdminImageCloudBrainForm{}), repo.CloudBrainAdminCommitImage) | |||||
| }) | }) | ||||
| m.Put("/image/:id/action/:action", image.Action) | m.Put("/image/:id/action/:action", image.Action) | ||||