This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new 14feefe  feat:add delete backup method and capture shell exec status 
(#206)
14feefe is described below

commit 14feefe5acaa7eb97b896c991787078feac6464a
Author: lltgo <[email protected]>
AuthorDate: Thu Feb 16 19:10:24 2023 +0800

    feat:add delete backup method and capture shell exec status (#206)
---
 pitr/agent/internal/cons/error.go         |  1 +
 pitr/agent/internal/pkg/opengauss.go      | 26 +++++++++++++++++++++-----
 pitr/agent/internal/pkg/opengauss_test.go | 17 +++++++++++++----
 pitr/agent/pkg/cmds/cmd.go                |  7 +++++++
 4 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/pitr/agent/internal/cons/error.go 
b/pitr/agent/internal/cons/error.go
index 1bfe795..189b9f9 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -25,4 +25,5 @@ var (
        Internal          = xerror.New(10000, "Internal error.")
        InvalidHttpHeader = xerror.New(10001, "Invalid http header.")
        DataNotFound      = xerror.New(10002, "Data not found.")
+       CmdOperateFailed  = xerror.New(10003, "Command operate failed")
 )
diff --git a/pitr/agent/internal/pkg/opengauss.go 
b/pitr/agent/internal/pkg/opengauss.go
index a837846..6d89b30 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -19,13 +19,13 @@ package pkg
 
 import (
        "encoding/json"
+       "errors"
        "fmt"
 
-       "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
-       
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg/model"
-
        "github.com/dlclark/regexp2"
 
+       "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
+       
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg/model"
        "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/cmds"
 )
 
@@ -34,8 +34,9 @@ type openGauss struct {
 }
 
 const (
-       _backupFmt = "gs_probackup backup --backup-path=%s --instance=%s 
--backup-mode=%s --pgdata=%s 2>&1"
-       _showFmt   = "gs_probackup show --instance=%s --backup-path=%s 
--backup-id=%s --format=json 2>&1 "
+       _backupFmt    = "gs_probackup backup --backup-path=%s --instance=%s 
--backup-mode=%s --pgdata=%s 2>&1"
+       _showFmt      = "gs_probackup show --instance=%s --backup-path=%s 
--backup-id=%s --format=json 2>&1"
+       _delBackupFmt = "gs_probackup delete --backup-path=%s --instance=%s 
--backup-id=%s 2>&1"
 )
 
 func (og *openGauss) AsyncBackup(backupPath, instanceName, backupMode, pgData 
string) (string, error) {
@@ -87,6 +88,21 @@ func (og *openGauss) ShowBackupDetail(backupPath, 
instanceName, backupID string)
        return nil, fmt.Errorf("backupList[v=%+v],err=%w", list, 
cons.DataNotFound)
 }
 
+func (og *openGauss) delBackup(backupPath, instanceName, backupID string) 
error {
+       cmd := fmt.Sprintf(_delBackupFmt, backupPath, instanceName, backupID)
+       _, err := cmds.Exec(og.shell, cmd)
+       if err != nil {
+               return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
+       }
+       if errors.Is(err, cons.CmdOperateFailed) {
+               return cons.CmdOperateFailed
+       }
+       if err != nil {
+               return fmt.Errorf("cmds.Exec[shell=%s, cmd=%s] return err=%w", 
og.shell, cmd, err)
+       }
+       return nil
+}
+
 func (og *openGauss) ignore(outputs chan *cmds.Output) {
        defer func() {
                _ = recover()
diff --git a/pitr/agent/internal/pkg/opengauss_test.go 
b/pitr/agent/internal/pkg/opengauss_test.go
index d93dcec..a6cb516 100644
--- a/pitr/agent/internal/pkg/opengauss_test.go
+++ b/pitr/agent/internal/pkg/opengauss_test.go
@@ -18,17 +18,20 @@
 package pkg
 
 import (
+       "errors"
        "fmt"
        "time"
 
        . "github.com/onsi/gomega"
 
        . "github.com/onsi/ginkgo/v2"
+
+       "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
 )
 
 var _ = Describe("OpenGauss,requires opengauss environment", func() {
-       Context("AsyncBackup & ShowBackupDetail", func() {
-               It("One backup and show", func() {
+       Context("AsyncBackup & ShowBackupDetail ", func() {
+               It("backup, show and delete", func() {
                        og := &openGauss{
                                shell: "/bin/sh",
                        }
@@ -61,12 +64,18 @@ var _ = Describe("OpenGauss,requires opengauss 
environment", func() {
                                Expect(backup).NotTo(BeNil())
                                Expect(backup.ID).To(Equal(backupID))
                                if backup.Status == "OK" {
-                                       goto End
+                                       goto Del
                                }
                                time.Sleep(time.Second)
                        }
                        Fail("Timeout[60s]")
-               End:
+                       return
+               Del:
+                       err = og.delBackup(data, instance, backupID)
+                       Expect(err).To(BeNil())
+
+                       err = og.delBackup(data, instance, backupID)
+                       Expect(errors.Is(err, 
cons.CmdOperateFailed)).To(BeTrue())
                })
        })
 })
diff --git a/pitr/agent/pkg/cmds/cmd.go b/pitr/agent/pkg/cmds/cmd.go
index 357885a..ebb6b89 100644
--- a/pitr/agent/pkg/cmds/cmd.go
+++ b/pitr/agent/pkg/cmds/cmd.go
@@ -23,6 +23,7 @@ import (
        "io"
        "os/exec"
 
+       "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
        "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/syncutils"
 )
 
@@ -109,5 +110,11 @@ func Exec(name string, args ...string) (string, error) {
                return "", fmt.Errorf("io.ReadAll return err=%w", err)
        }
 
+       if err = cmd.Wait(); err != nil {
+               if _, ok := err.(*exec.ExitError); ok {
+                       return "", cons.CmdOperateFailed
+               }
+               return "", cons.Internal
+       }
        return string(reader), nil
 }

Reply via email to