You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by mi...@apache.org on 2023/02/17 08:02:22 UTC

[shardingsphere-on-cloud] branch main updated: feat: instance add\del method and test it (#210)

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 ef4cf32  feat: instance add\del method and test it (#210)
ef4cf32 is described below

commit ef4cf32fdd40299744ef2ae70ba692cdeb851386
Author: lltgo <ll...@outlook.com>
AuthorDate: Fri Feb 17 16:02:16 2023 +0800

    feat: instance add\del method and test it (#210)
---
 pitr/agent/internal/cons/error.go         |  2 ++
 pitr/agent/internal/pkg/opengauss.go      | 36 ++++++++++++++++++++++++++++---
 pitr/agent/internal/pkg/opengauss_test.go | 33 ++++++++++++++++++++++++----
 3 files changed, 64 insertions(+), 7 deletions(-)

diff --git a/pitr/agent/internal/cons/error.go b/pitr/agent/internal/cons/error.go
index d1feb14..fd5a349 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -28,4 +28,6 @@ var (
 	CmdOperateFailed       = xerror.New(10003, "Command operate failed.")
 	BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.")
 	NoPermission           = xerror.New(10005, "No permission to operate.")
+	InstanceAlreadyExist   = xerror.New(10006, "The instance already exist.")
+	InstanceNotExist       = xerror.New(10007, "The instance not exist.")
 )
diff --git a/pitr/agent/internal/pkg/opengauss.go b/pitr/agent/internal/pkg/opengauss.go
index bcfcbec..bcbe90d 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -38,8 +38,12 @@ 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"
 	_delBackupFmt = "gs_probackup delete --backup-path=%s --instance=%s --backup-id=%s 2>&1"
-	_initFmt      = "gs_probackup init --backup-path=%s 2>&1"
-	_deinitFmt    = "rm -r %s"
+
+	_initFmt   = "gs_probackup init --backup-path=%s 2>&1"
+	_deinitFmt = "rm -r %s"
+
+	_addInstanceFmt = "gs_probackup add-instance --backup-path=%s --instance=%s --pgdata=%s 2>&1"
+	_delInstanceFmt = "gs_probackup del-instance --backup-path=%s --instance=%s 2>&1"
 )
 
 func (og *openGauss) AsyncBackup(backupPath, instanceName, backupMode, pgData string) (string, error) {
@@ -100,7 +104,7 @@ func (og *openGauss) delBackup(backupPath, instanceName, backupID string) error
 	return nil
 }
 
-func (og *openGauss) init(backupPath string) error {
+func (og *openGauss) Init(backupPath string) error {
 	cmd := fmt.Sprintf(_initFmt, backupPath)
 	_, err := cmds.Exec(og.shell, cmd)
 	// already exist and it's not empty
@@ -113,6 +117,32 @@ func (og *openGauss) init(backupPath string) error {
 	return nil
 }
 
+func (og *openGauss) AddInstance(backupPath, instancee, pgData string) error {
+	cmd := fmt.Sprintf(_addInstanceFmt, backupPath, instancee, pgData)
+	_, err := cmds.Exec(og.shell, cmd)
+	// already exist and it's not empty
+	if errors.Is(err, cons.CmdOperateFailed) {
+		return cons.InstanceAlreadyExist
+	}
+	if err != nil {
+		return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
+	}
+	return nil
+}
+
+func (og *openGauss) DelInstance(backupPath, instancee string) error {
+	cmd := fmt.Sprintf(_delInstanceFmt, backupPath, instancee)
+	_, err := cmds.Exec(og.shell, cmd)
+	// already exist and it's not empty
+	if errors.Is(err, cons.CmdOperateFailed) {
+		return cons.InstanceNotExist
+	}
+	if err != nil {
+		return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
+	}
+	return nil
+}
+
 func (og *openGauss) deinit(backupPath string) error {
 	if !strings.HasPrefix(backupPath, "/home/omm/") {
 		return cons.NoPermission
diff --git a/pitr/agent/internal/pkg/opengauss_test.go b/pitr/agent/internal/pkg/opengauss_test.go
index 02b33cd..73c54d2 100644
--- a/pitr/agent/internal/pkg/opengauss_test.go
+++ b/pitr/agent/internal/pkg/opengauss_test.go
@@ -80,17 +80,17 @@ var _ = Describe("OpenGauss,requires opengauss environment", func() {
 	})
 
 	Context("Init and deinit", func() {
-		It("init backup and clean up the env", func() {
+		It("Init backup and clean up the env", func() {
 			og := &openGauss{
 				shell: "/bin/sh",
 			}
 
 			data2 := "/home/omm/data2"
 
-			err := og.init(data2)
+			err := og.Init(data2)
 			Expect(err).To(BeNil())
 
-			err = og.init(data2)
+			err = og.Init(data2)
 			Expect(err).NotTo(BeNil())
 			Expect(errors.Is(err, cons.BackupPathAlreadyExist)).To(BeTrue())
 
@@ -98,7 +98,7 @@ var _ = Describe("OpenGauss,requires opengauss environment", func() {
 			Expect(err).To(BeNil())
 
 			// repeat validation
-			err = og.init(data2)
+			err = og.Init(data2)
 			Expect(err).To(BeNil())
 			err = og.deinit(data2)
 			Expect(err).To(BeNil())
@@ -116,4 +116,29 @@ var _ = Describe("OpenGauss,requires opengauss environment", func() {
 			Expect(errors.Is(err, cons.NoPermission)).To(BeTrue())
 		})
 	})
+
+	Context("AddInstance and DelInstance", func() {
+		It("instance:add and delete", func() {
+			og := &openGauss{
+				shell: "/bin/sh",
+			}
+
+			var (
+				backupPath = "/home/omm/data"
+				instance   = "ins-test-1"
+				pgData     = "/data/opengauss/3.1.1/data/single_node/"
+			)
+			err := og.AddInstance(backupPath, instance, pgData)
+			Expect(err).To(BeNil())
+
+			err = og.AddInstance(backupPath, instance, pgData)
+			Expect(errors.Is(err, cons.InstanceAlreadyExist)).To(BeTrue())
+
+			err = og.DelInstance(backupPath, instance)
+			Expect(err).To(BeNil())
+
+			err = og.DelInstance(backupPath, instance)
+			Expect(errors.Is(err, cons.InstanceNotExist)).To(BeTrue())
+		})
+	})
 })