From 6bc28bd41c4401668475ab9b41f4ef1aef261ecd Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 1 Oct 2015 12:05:23 -0700 Subject: [PATCH] ability to cancel a single job (not entire build) --- controller/build.go | 27 ++++++++++++++++++++++++++- engine/engine.go | 2 +- router/router.go | 2 +- static/scripts/build.js | 17 +++++++++++++++++ template/amber/build.amber | 3 ++- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/controller/build.go b/controller/build.go index 81fbb7d78..4a4659e80 100644 --- a/controller/build.go +++ b/controller/build.go @@ -93,7 +93,32 @@ func GetBuildLogs(c *gin.Context) { } func DeleteBuild(c *gin.Context) { - c.String(http.StatusOK, "DeleteBuild") + engine_ := context.Engine(c) + repo := session.Repo(c) + db := context.Database(c) + + // parse the build number and job sequence number from + // the repquest parameter. + num, _ := strconv.Atoi(c.Params.ByName("number")) + seq, _ := strconv.Atoi(c.Params.ByName("job")) + + build, err := model.GetBuildNumber(db, repo, num) + if err != nil { + c.AbortWithError(404, err) + return + } + + job, err := model.GetJobNumber(db, build, seq) + if err != nil { + c.AbortWithError(404, err) + return + } + node, err := model.GetNode(db, job.NodeID) + if err != nil { + c.AbortWithError(404, err) + return + } + engine_.Cancel(build.ID, job.ID, node) } func PostBuild(c *gin.Context) { diff --git a/engine/engine.go b/engine/engine.go index b93bbb6be..58d3821fd 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -80,7 +80,7 @@ func Load(db *sql.DB, remote remote.Remote) Engine { // Cancel cancels the job running on the specified Node. func (e *engine) Cancel(build, job int64, node *model.Node) error { - client, err := dockerclient.NewDockerClient(node.Addr, nil) + client, err := newDockerClient(node.Addr, node.Cert, node.Key, node.CA) if err != nil { return err } diff --git a/router/router.go b/router/router.go index c4c2c9554..86297f8ac 100644 --- a/router/router.go +++ b/router/router.go @@ -104,7 +104,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { repo.DELETE("", session.MustPush, controller.DeleteRepo) repo.POST("/builds/:number", session.MustPush, controller.PostBuild) - // repo.DELETE("/builds/:number", MustPush(), controller.DeleteBuild) + repo.DELETE("/builds/:number/:job", session.MustPush, controller.DeleteBuild) } } diff --git a/static/scripts/build.js b/static/scripts/build.js index 84b613bc3..c5158da64 100644 --- a/static/scripts/build.js +++ b/static/scripts/build.js @@ -6,6 +6,8 @@ function JobViewModel(repo, build, job, status) { self.stream = function() { $( "#output" ).html(""); + $("#restart").hide(); + $("#cancel").show(); var buf = new Drone.Buffer(); buf.start(document.getElementById("output")); @@ -32,6 +34,7 @@ function JobViewModel(repo, build, job, status) { if (status !== "running" && status !== "pending") { Logs(repo, build, job); + $("#restart").show(); } if (status === "running") { @@ -53,6 +56,19 @@ function JobViewModel(repo, build, job, status) { }); }) + $("#cancel").click(function() { + $("#cancel").hide(); + + $.ajax({ + url: "/api/repos/"+repo+"/builds/"+build+"/"+job, + type: "DELETE", + success: function( data ) { }, + error: function( data ) { + console.log(data); + } + }); + }) + Subscribe(repo, function(data){ if (!data.jobs) { @@ -81,6 +97,7 @@ function JobViewModel(repo, build, job, status) { // the restart button and hide the tail button. if (after !== "pending" && after !== "running") { $("#restart").show(); + $("#cancel").hide(); $("#tail").hide(); } }.bind(this)); diff --git a/template/amber/build.amber b/template/amber/build.amber index a001da042..3f12a9dc7 100644 --- a/template/amber/build.amber +++ b/template/amber/build.amber @@ -52,7 +52,8 @@ block content div with exit code #{$job.ExitCode} div.build-btn-group - button.btn.btn-info#restart restart + button.btn.btn-info.hidden#restart restart + button.btn.btn-info.hidden#cancel cancel div.col-md-8 pre#output