Skip to content

Commit d6ef7e3

Browse files
committed
implement handler for deleting pipelines
1 parent 34c57db commit d6ef7e3

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

frontend/client/views/settings/index.vue

+17-3
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ export default {
365365
.get('/api/v1/pipeline', { showProgressBar: false })
366366
.then(response => {
367367
if (response.data) {
368-
this.pipelineRows = response.data
368+
this.pipelineRows = response.data;
369+
} else {
370+
this.pipelineRows = [];
369371
}
370372
}).catch((error) => {
371373
this.$onError(error)
@@ -522,8 +524,20 @@ export default {
522524
},
523525
524526
deletePipeline () {
525-
console.log('Delete pipeline')
526-
this.close()
527+
this.$http
528+
.delete('/api/v1/pipeline/' + this.selectPipeline.id)
529+
.then(response => {
530+
openNotification({
531+
title: 'Pipeline deleted!',
532+
message: 'Pipeline ' + this.selectPipeline.name + ' has been successfully deleted.',
533+
type: 'success'
534+
})
535+
this.fetchData()
536+
this.close()
537+
})
538+
.catch((error) => {
539+
this.$onError(error)
540+
})
527541
}
528542
}
529543
}

handlers/handler.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func InitHandlers(e *echo.Echo, store *store.Store, scheduler *scheduler.Schedul
7474
e.GET(p+"pipeline/name", PipelineNameAvailable)
7575
e.GET(p+"pipeline", PipelineGetAll)
7676
e.GET(p+"pipeline/:pipelineid", PipelineGet)
77+
e.DELETE(p+"pipeline/:pipelineid", PipelineDelete)
7778
e.POST(p+"pipeline/:pipelineid/start", PipelineStart)
7879
e.GET(p+"pipeline/latest", PipelineGetAllWithLatestRun)
7980

handlers/pipeline.go

+44
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,50 @@ func PipelineGet(c echo.Context) error {
136136
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
137137
}
138138

139+
// PipelineDelete accepts a pipeline id and deletes it from the
140+
// store. It also removes the binary inside the pipeline folder.
141+
func PipelineDelete(c echo.Context) error {
142+
pipelineIDStr := c.Param("pipelineid")
143+
144+
pipelineID, err := strconv.Atoi(pipelineIDStr)
145+
if err != nil {
146+
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
147+
}
148+
149+
// Look up pipeline for the given id
150+
var foundPipeline gaia.Pipeline
151+
var index int
152+
var deletedPipelineIndex int
153+
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
154+
if pipeline.ID == pipelineID {
155+
foundPipeline = pipeline
156+
deletedPipelineIndex = index
157+
}
158+
index++
159+
}
160+
161+
if foundPipeline.Name == "" {
162+
return c.String(http.StatusNotFound, err.Error())
163+
}
164+
165+
// Delete pipeline binary
166+
err = pipeline.DeleteBinary(foundPipeline)
167+
if err != nil {
168+
return c.String(http.StatusInternalServerError, err.Error())
169+
}
170+
171+
// Delete pipeline from store
172+
err = storeService.PipelineDelete(pipelineID)
173+
if err != nil {
174+
return c.String(http.StatusNotFound, err.Error())
175+
}
176+
177+
// Remove from active pipelines
178+
pipeline.GlobalActivePipelines.Remove(deletedPipelineIndex)
179+
180+
return c.String(http.StatusOK, "Pipeline has been deleted")
181+
}
182+
139183
// PipelineStart starts a pipeline by the given id.
140184
// Afterwards it returns the created/scheduled pipeline run.
141185
func PipelineStart(c echo.Context) error {

pipeline/pipeline.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package pipeline
33
import (
44
"errors"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"sync"
79

810
"github.com/gaia-pipeline/gaia"
@@ -191,6 +193,12 @@ func (ap *ActivePipelines) RemoveDeletedPipelines(existingPipelineNames []string
191193
}
192194
}
193195

196+
// DeleteBinary deletes the binary for the given pipeline.
197+
func DeleteBinary(p gaia.Pipeline) error {
198+
binaryFile := filepath.Join(gaia.Cfg.PipelinePath, appendTypeToName(p.Name, p.Type))
199+
return os.Remove(binaryFile)
200+
}
201+
194202
// appendTypeToName appends the type to the output binary name.
195203
// This allows us later to define the pipeline type by the name.
196204
func appendTypeToName(n string, pType gaia.PipelineType) string {

store/pipeline.go

+11
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,14 @@ func (s *Store) PipelineGetLatestRun(pipelineID int) (*gaia.PipelineRun, error)
310310
})
311311
})
312312
}
313+
314+
// PipelineDelete deletes the pipeline with the given id.
315+
func (s *Store) PipelineDelete(id int) error {
316+
return s.db.Update(func(tx *bolt.Tx) error {
317+
// Get bucket
318+
b := tx.Bucket(pipelineBucket)
319+
320+
// Delete pipeline
321+
return b.Delete(itob(id))
322+
})
323+
}

0 commit comments

Comments
 (0)