diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 39d71dd83..f586802d5 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -31,6 +31,7 @@ type Cloudbrain struct { RepoID int64 `xorm:"INDEX"` SubTaskName string `xorm:"INDEX"` ContainerID string + ContainerIp string CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` CanDebug bool `xorm:"-"` @@ -219,9 +220,10 @@ type ImageInfo struct { } type CommitImageParams struct { - Ip string `json:"ip"` - TaskContainerId string `json:"taskContainerId"` - ImageDescription string `json:"imageDescription"` + Ip string `json:"ip"` + TaskContainerId string `json:"taskContainerId"` + ImageTag string `json:"imageTag"` + ImageDescription string `json:"imageDescription"` } type CommitImageResult struct { @@ -339,6 +341,6 @@ func UpdateJob(job *Cloudbrain) error { func updateJob(e Engine, job *Cloudbrain) error { var sess *xorm.Session sess = e.Where("job_id = ?", job.JobID) - _, err := sess.Cols("status", "container_id").Update(job) + _, err := sess.Cols("status", "container_id", "container_ip").Update(job) return err } diff --git a/modules/auth/cloudbrain.go b/modules/auth/cloudbrain.go index ca3848a65..b7b59ce63 100755 --- a/modules/auth/cloudbrain.go +++ b/modules/auth/cloudbrain.go @@ -13,6 +13,11 @@ type CreateCloudBrainForm struct { Attachment string `form:"attachment" binding:"Required"` } +type CommitImageCloudBrainForm struct { + Description string `form:"description" binding:"Required"` + Tag string `form:"tag" binding:"Required"` +} + func (f *CreateCloudBrainForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/cloudbrain/resty.go b/modules/cloudbrain/resty.go index 18ffe17c6..a58e31e20 100755 --- a/modules/cloudbrain/resty.go +++ b/modules/cloudbrain/resty.go @@ -150,7 +150,7 @@ sendjob: return &getImagesResult, nil } -func CommitImage(jobID string, params models.CommitImageParams) (*models.CommitImageResult, error) { +func CommitImage(jobID string, params models.CommitImageParams) error { checkSetting() client := getRestyClient() var result models.CommitImageResult @@ -166,7 +166,7 @@ sendjob: Post(HOST + "/rest-server/api/v1/jobs/" + jobID + "/commitImage") if err != nil { - return nil, fmt.Errorf("resty CommitImage: %v", err) + return fmt.Errorf("resty CommitImage: %v", err) } if result.Code == "S401" && retry < 1 { @@ -176,10 +176,10 @@ sendjob: } if result.Code != Success { - return &result, fmt.Errorf("CommitImage err: %s", res.String()) + return fmt.Errorf("CommitImage err: %s", res.String()) } - return &result, nil + return nil } func StopJob(jobID string) error { diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 2e56fbb06..0398d5655 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -752,6 +752,7 @@ cloudbrain=云脑 cloudbrain.new=新建任务 cloudbrain.desc=云脑功能 cloudbrain.cancel=取消 +cloudbrain.commit_image=提交 template.items=模板选项 template.git_content=Git数据(默认分支) diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 055d2120d..564b6c852 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -1,6 +1,7 @@ package repo import ( + "errors" "os" "os/exec" "strconv" @@ -164,6 +165,7 @@ func CloudBrainShow(ctx *context.Context) { 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() @@ -186,27 +188,23 @@ func CloudBrainDebug(ctx *context.Context) { ctx.Redirect(debugUrl) } -func CloudBrainCommitImage(ctx *context.Context) { +func CloudBrainCommitImage(ctx *context.Context, form auth.CommitImageCloudBrainForm) { var jobID = ctx.Params(":jobid") - var description = ctx.Query("description") - log.Info(description) task, err := models.GetCloudbrainByJobID(jobID) if err != nil { ctx.ServerError("GetCloudbrainByJobID failed", err) return } - jobResult, err := cloudbrain.CommitImage(jobID, models.CommitImageParams{ - Ip: setting.ImageServerHost, - TaskContainerId: task.ContainerID, - ImageDescription: description, + err = cloudbrain.CommitImage(jobID, models.CommitImageParams{ + Ip: task.ContainerIp, + TaskContainerId: task.ContainerID, + ImageDescription: form.Description, + ImageTag: form.Tag, }) if err != nil { log.Error("CommitImage failed:", err.Error()) - return - } - if jobResult.Code != cloudbrain.Success { - log.Error("CommitImage(%s) failed:%s", jobID, jobResult.Msg) + ctx.ServerError("CommitImage failed", err) return } @@ -215,9 +213,30 @@ func CloudBrainCommitImage(ctx *context.Context) { func CloudBrainStop(ctx *context.Context) { var jobID = ctx.Params(":jobid") - err := cloudbrain.StopJob(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 { - log.Error("StopJob failed:", err.Error()) + ctx.ServerError("UpdateJob failed", err) return } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 76dc05e72..814af1fd9 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -894,10 +894,12 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/cloudbrain", func() { m.Get("", reqRepoCloudBrainReader, repo.CloudBrainIndex) - m.Get("/:jobid", reqRepoCloudBrainReader, repo.CloudBrainShow) - m.Get("/:jobid/debug", reqRepoCloudBrainReader, repo.CloudBrainDebug) - m.Post("/:jobid/commit_image", reqRepoCloudBrainReader, repo.CloudBrainCommitImage) - m.Post("/:jobid/stop", reqRepoCloudBrainReader, repo.CloudBrainStop) + m.Group("/:jobid", func() { + m.Get("", reqRepoCloudBrainReader, repo.CloudBrainShow) + m.Get("/debug", reqRepoCloudBrainReader, repo.CloudBrainDebug) + m.Post("/commit_image", reqRepoCloudBrainWriter, bindIgnErr(auth.CommitImageCloudBrainForm{}), repo.CloudBrainCommitImage) + m.Post("/stop", reqRepoCloudBrainWriter, repo.CloudBrainStop) + }) m.Get("/create", reqRepoCloudBrainWriter, repo.CloudBrainNew) m.Post("/create", reqRepoCloudBrainWriter, bindIgnErr(auth.CreateCloudBrainForm{}), repo.CloudBrainCreate) }, context.RepoRef()) diff --git a/templates/repo/cloudbrain/index.tmpl b/templates/repo/cloudbrain/index.tmpl index 4029340fc..a02c96270 100755 --- a/templates/repo/cloudbrain/index.tmpl +++ b/templates/repo/cloudbrain/index.tmpl @@ -63,18 +63,50 @@