Skip to content

Commit

Permalink
fix(gitlabcedocker): Improve the folder deletion logic in plugin gitl…
Browse files Browse the repository at this point in the history
…ab-ce-docer

Signed-off-by: luckyDogg <wilson.wang.hc@gmail.com>
  • Loading branch information
LuckyDogg committed Jun 30, 2022
1 parent a341dc3 commit ce9d132
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/pkg/plugin/gitlabcedocker/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package gitlabcedocker

import (
"fmt"
"os"

"github.com/mitchellh/mapstructure"
"os"
"path/filepath"

"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -46,9 +46,16 @@ func Delete(options map[string]interface{}) (bool, error) {
}

// 4. remove the volume if it exists
volumesFromOptions := []string{
filepath.Join(opts.GitLabHome, "config"),
filepath.Join(opts.GitLabHome, "data"),
filepath.Join(opts.GitLabHome, "logs"),
}
if opts.RmDataAfterDelete {
if err := os.RemoveAll(opts.GitLabHome); err != nil {
log.Errorf("failed to remove data %v: %v", opts.GitLabHome, err)
for _, volume := range volumesFromOptions {
if err := os.RemoveAll(volume); err != nil {
log.Errorf("failed to remove data %v: %v", volume, err)
}
}
}

Expand All @@ -67,8 +74,12 @@ func Delete(options map[string]interface{}) (bool, error) {
errs = append(errs, fmt.Errorf("failed to delete image %s", getImageNameWithTag(opts)))
}

// TODO: 3. check if data is removed successfully(if opts.RmDataAfterDelete is true)
// note: the data is defined by opts.GitLabHome
// 3. check if the data volume is removed
for _, volume := range volumesFromOptions {
if _, err := os.Stat(volume); !os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("failed to delete data %s", volume))
}
}

// splice the errors
if len(errs) != 0 {
Expand Down
27 changes: 27 additions & 0 deletions internal/pkg/plugin/gitlabcedocker/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gitlabcedocker_test

import (
"github.com/devstream-io/devstream/internal/pkg/plugin/gitlabcedocker"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("delete", func() {
var options map[string]interface{}

BeforeEach(func() {
options = map[string]interface{}{
"hostname": "gitlab.devstream.io",
"gitlab_home": "/srv/gitlab",
"ssh_port": 8122,
"http_port": 8180,
"https_port": 8443,
}
})

It("delete docker data successfully", func() {
cmd, _ := gitlabcedocker.Delete(options)
Expect(cmd).To(Equal(true))
})

})

0 comments on commit ce9d132

Please sign in to comment.