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 ef8c14d feat(pitr):complete agent show and show list api (#231)
ef8c14d is described below
commit ef8c14de0c59eb84d5879220e8a3fbcfb5348361
Author: lltgo <[email protected]>
AuthorDate: Tue Feb 28 20:55:30 2023 +0800
feat(pitr):complete agent show and show list api (#231)
* feat(pitr):complete agent show and show list api
* chore:cancle commet some code
* chore:add license
* chore:rm agent cli
---
pitr/agent/internal/cons/error.go | 1 +
pitr/agent/internal/handler/show.go | 43 +++++++++++++++++-
pitr/agent/internal/handler/view/show.go | 53 +++++++++++++++++++++-
pitr/agent/internal/pkg/opengauss.go | 36 +++++++++++----
.../gsutil/conn_test.go => internal/pkg/pkg.go} | 22 +++------
pitr/agent/main.go | 18 ++++----
pitr/agent/pkg/cmds/cmd.go | 4 +-
pitr/agent/pkg/gsutil/conn.go | 13 ++++--
pitr/agent/pkg/gsutil/conn_test.go | 2 +-
9 files changed, 148 insertions(+), 44 deletions(-)
diff --git a/pitr/agent/internal/cons/error.go
b/pitr/agent/internal/cons/error.go
index cf9ef6a..f5ba399 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -44,4 +44,5 @@ var (
MissingDnBackupId = xerror.New(10019, "Missing dn backup id.")
BodyParseFailed = xerror.New(10020, "Invalid http request body.")
MissingDbName = xerror.New(10021, "Missing db name.")
+ DbConnectionFailed = xerror.New(10022, "Database connection
failed.")
)
diff --git a/pitr/agent/internal/handler/show.go
b/pitr/agent/internal/handler/show.go
index da7bc17..028073d 100644
--- a/pitr/agent/internal/handler/show.go
+++ b/pitr/agent/internal/handler/show.go
@@ -19,6 +19,8 @@ package handler
import (
"fmt"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
@@ -35,8 +37,45 @@ func Show(ctx *fiber.Ctx) error {
}
if err := in.Validate(); err != nil {
- return err
+ return fmt.Errorf("invalid parameter,err=%w", err)
}
- return ctx.JSON(in)
+ if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort);
err != nil {
+ efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
+ return fmt.Errorf(efmt, in.Username, len(in.Password),
in.DbName, err)
+ }
+
+ data, err := pkg.OG.ShowBackup(in.DnBackupPath, in.Instance,
in.DnBackupId)
+ if err != nil {
+ efmt := "pkg.OG.ShowBackupDetail
failure[backupPath=%s,instance=%s,backupID=%s],err=%w"
+ return fmt.Errorf(efmt, in.DnBackupPath, in.Instance,
in.DnBackupId, err)
+ }
+
+ return responder.Success(ctx, view.NewBackupInfo(data, in.DnBackupPath,
in.Instance))
+}
+
+func ShowList(ctx *fiber.Ctx) error {
+ in := &view.ShowIn{}
+
+ if err := ctx.BodyParser(in); err != nil {
+ return fmt.Errorf("body parse err=%s,wrap=%w", err,
cons.BodyParseFailed)
+ }
+
+ if err := in.Validate(); err != nil {
+ return fmt.Errorf("invalid parameter,err=%w", err)
+ }
+
+ if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort);
err != nil {
+ efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w"
+ return fmt.Errorf(efmt, in.Username, len(in.Password),
in.DbName, err)
+ }
+
+ //Show list
+ list, err := pkg.OG.ShowBackupList(in.DnBackupPath, in.Instance)
+ if err != nil {
+ efmt := "pkg.OG.ShowBackupList
failure[backupPath=%s,instance=%s],err=%w"
+ return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, err)
+ }
+
+ return responder.Success(ctx, view.NewBackupInfoList(list,
in.DnBackupPath, in.Instance))
}
diff --git a/pitr/agent/internal/handler/view/show.go
b/pitr/agent/internal/handler/view/show.go
index 5952bce..c2643f9 100644
--- a/pitr/agent/internal/handler/view/show.go
+++ b/pitr/agent/internal/handler/view/show.go
@@ -17,7 +17,10 @@
package view
-import "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
+import (
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
+
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg/model"
+)
type (
ShowIn struct {
@@ -33,6 +36,7 @@ type (
BackupInfo struct {
Id string `json:"dn_backup_id"`
Path string `json:"dn_backup_path"`
+ Mode string `json:"db_backup_mode"`
Instance string `json:"instance"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
@@ -74,3 +78,50 @@ func (in *ShowIn) Validate() error {
}
return nil
}
+
+func NewBackupInfo(data *model.Backup, path, instance string) *BackupInfo {
+ if data == nil {
+ return nil
+ }
+ return &BackupInfo{
+ Id: data.ID,
+ Path: path,
+ Mode: data.BackupMode,
+ Instance: instance,
+ StartTime: data.StartTime,
+ EndTime: data.EndTime,
+ Status: statusTrans(data.Status),
+ }
+}
+
+func NewBackupInfoList(list []model.Backup, path, instance string)
[]BackupInfo {
+ if len(list) == 0 {
+ return []BackupInfo{}
+ }
+ ret := make([]BackupInfo, 0, len(list))
+ for _, v := range list {
+ ret = append(ret, BackupInfo{
+ Id: v.ID,
+ Path: path,
+ Mode: v.BackupMode,
+ Instance: instance,
+ StartTime: v.StartTime,
+ EndTime: v.EndTime,
+ Status: statusTrans(v.Status),
+ })
+ }
+ return ret
+}
+
+func statusTrans(status string) string {
+ switch status {
+ case "OK":
+ return "Completed"
+ case "ERROR":
+ return "Failed"
+ case "RUNNING":
+ return "Running"
+ default:
+ return "Other"
+ }
+}
diff --git a/pitr/agent/internal/pkg/opengauss.go
b/pitr/agent/internal/pkg/opengauss.go
index 21d363c..c992e88 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -31,8 +31,27 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/cmds"
)
-type openGauss struct {
- shell string
+type (
+ openGauss struct {
+ shell string
+ }
+
+ IOpenGauss interface {
+ AsyncBackup(backupPath, instanceName, backupMode, pgData
string) (string, error)
+ ShowBackup(backupPath, instanceName, backupID string)
(*model.Backup, error)
+ Init(backupPath string) error
+ AddInstance(backupPath, instancee, pgData string) error
+ DelInstance(backupPath, instancee string) error
+ Start(pgData string) error
+ Stop(pgData string) error
+ Restore(backupPath, instance, backupID, pgData string) error
+ ShowBackupList(backupPath, instanceName string)
([]model.Backup, error)
+ Auth(user, password, dbName string, dbPort uint16) error
+ }
+)
+
+func NewOpenGauss(shell string) IOpenGauss {
+ return &openGauss{shell: shell}
}
const (
@@ -77,7 +96,7 @@ func (og *openGauss) AsyncBackup(backupPath, instanceName,
backupMode, pgData st
return "", fmt.Errorf("unknow err")
}
-func (og *openGauss) ShowBackupDetail(backupPath, instanceName, backupID
string) (*model.Backup, error) {
+func (og *openGauss) ShowBackup(backupPath, instanceName, backupID string)
(*model.Backup, error) {
cmd := fmt.Sprintf(_showFmt, instanceName, backupPath, backupID)
output, err := cmds.Exec(og.shell, cmd)
if err != nil {
@@ -246,16 +265,17 @@ func (og *openGauss) getBackupID(msg string) (string,
error) {
return match.String(), err
}
-func (og *openGauss) Auth(user, password, dbName string) error {
+func (og *openGauss) Auth(user, password, dbName string, dbPort uint16) error {
if strings.Trim(user, " ") == "" ||
strings.Trim(password, " ") == "" ||
- strings.Trim(dbName, " ") == "" {
- return fmt.Errorf("invalid
inputs[user=%s,password=%s,dbName=%s]", user, password, dbName)
+ strings.Trim(dbName, " ") == "" ||
+ dbPort == 0 {
+ return fmt.Errorf("invalid
inputs[user=%s,password=%s,dbName=%s,dbPort=%d]", user, password, dbName,
dbPort)
}
- _og, err := gsutil.Open(user, password, dbName)
+ _og, err := gsutil.Open(user, password, dbName, dbPort)
if err != nil {
- return err
+ return fmt.Errorf("gsutil.Open failure,err=%w", err)
}
if err := _og.Ping(); err != nil {
diff --git a/pitr/agent/pkg/gsutil/conn_test.go b/pitr/agent/internal/pkg/pkg.go
similarity index 69%
copy from pitr/agent/pkg/gsutil/conn_test.go
copy to pitr/agent/internal/pkg/pkg.go
index e63fc6f..59e81e2 100644
--- a/pitr/agent/pkg/gsutil/conn_test.go
+++ b/pitr/agent/internal/pkg/pkg.go
@@ -15,22 +15,12 @@
* limitations under the License.
*/
-package gsutil
+package pkg
-import (
- . "github.com/onsi/ginkgo/v2"
- . "github.com/onsi/gomega"
+var (
+ OG IOpenGauss
)
-var _ = Describe("OpenGauss", func() {
- Context("Connection", func() {
- It("Open and ping", func() {
- og, err := Open("dba3", "1234567890@ss", "school")
- Expect(err).To(BeNil())
- Expect(og).NotTo(BeNil())
-
- err = og.Ping()
- Expect(err).To(BeNil())
- })
- })
-})
+func Init(shell string) {
+ OG = NewOpenGauss(shell)
+}
diff --git a/pitr/agent/main.go b/pitr/agent/main.go
index 577f35f..81dd37a 100644
--- a/pitr/agent/main.go
+++ b/pitr/agent/main.go
@@ -20,21 +20,18 @@ package main
import (
"flag"
"fmt"
- "os"
- "os/signal"
- "strings"
- "syscall"
-
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler"
-
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/middleware"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"
-
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
-
- "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging"
+ "os"
+ "os/signal"
+ "strings"
+ "syscall"
)
const (
@@ -72,6 +69,7 @@ func main() {
if shell == "" {
panic(fmt.Errorf("shell does not exist"))
}
+ pkg.Init(shell)
if pgData == "" {
pgData = os.Getenv("PGDATA")
@@ -145,6 +143,7 @@ func Serve(port string) error {
r.Post("/backup", handler.Backup)
r.Post("/restore", handler.Restore)
r.Post("/show", handler.Show)
+ r.Post("/show/list", handler.ShowList)
})
// 404
@@ -152,5 +151,6 @@ func Serve(port string) error {
return responder.NotFound(ctx, "API not found")
})
+ // return app.Listen(":18080")
return app.ListenTLS(fmt.Sprintf(":%s", port), tlsCrt, tlsKey)
}
diff --git a/pitr/agent/pkg/cmds/cmd.go b/pitr/agent/pkg/cmds/cmd.go
index 2550d02..86dc1de 100644
--- a/pitr/agent/pkg/cmds/cmd.go
+++ b/pitr/agent/pkg/cmds/cmd.go
@@ -116,8 +116,8 @@ func Exec(name string, args ...string) (string, error) {
}
if err = cmd.Wait(); err != nil {
- if _, ok := err.(*exec.ExitError); ok {
- return "", cons.CmdOperateFailed
+ if ee, ok := err.(*exec.ExitError); ok {
+ return "", fmt.Errorf("exec
failure[ee=%s,stdout=%s],wrap=%w", ee, string(reader), cons.CmdOperateFailed)
}
return "", cons.Internal
}
diff --git a/pitr/agent/pkg/gsutil/conn.go b/pitr/agent/pkg/gsutil/conn.go
index 68b2883..6b4f565 100644
--- a/pitr/agent/pkg/gsutil/conn.go
+++ b/pitr/agent/pkg/gsutil/conn.go
@@ -21,6 +21,7 @@ import (
"database/sql"
"fmt"
_ "gitee.com/opengauss/openGauss-connector-go-pq"
+ "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"strings"
)
@@ -31,7 +32,7 @@ type OpenGauss struct {
dbName string
}
-func Open(user, password, dbName string) (*OpenGauss, error) {
+func Open(user, password, dbName string, dbPort uint16) (*OpenGauss, error) {
if strings.Trim(user, " ") == "" {
return nil, fmt.Errorf("user is empty")
}
@@ -42,10 +43,11 @@ func Open(user, password, dbName string) (*OpenGauss,
error) {
return nil, fmt.Errorf("db name is empty")
}
- connStr := "user=%s password=%s dbname=%s sslmode=disable"
- db, err := sql.Open("opengauss", fmt.Sprintf(connStr, user, password,
dbName))
+ connStr := "port=%d user=%s password=%s dbname=%s sslmode=disable"
+ db, err := sql.Open("opengauss", fmt.Sprintf(connStr, dbPort, user,
password, dbName))
if err != nil {
- return nil, fmt.Errorf("sql:open
fail[user=%s,pwLen=%d,dbName=%s]", user, len(password), dbName)
+ efmt := "sql:open
fail[user=%s,pwLen=%d,dbName=%s],err=%s,wrap=%w"
+ return nil, fmt.Errorf(efmt, user, len(password), dbName, err,
cons.DbConnectionFailed)
}
return &OpenGauss{
@@ -58,7 +60,8 @@ func Open(user, password, dbName string) (*OpenGauss, error) {
func (og *OpenGauss) Ping() error {
if err := og.db.Ping(); err != nil {
- return fmt.Errorf("db ping
fail[user=%s,pwLen=%d,dbName=%s],err=%w", og.user, og.pwLen, og.dbName, err)
+ efmt := "db ping
fail[user=%s,pwLen=%d,dbName=%s],err=%s,wrap=%w"
+ return fmt.Errorf(efmt, og.user, og.pwLen, og.dbName, err,
cons.DbConnectionFailed)
}
return nil
}
diff --git a/pitr/agent/pkg/gsutil/conn_test.go
b/pitr/agent/pkg/gsutil/conn_test.go
index e63fc6f..4dace0d 100644
--- a/pitr/agent/pkg/gsutil/conn_test.go
+++ b/pitr/agent/pkg/gsutil/conn_test.go
@@ -25,7 +25,7 @@ import (
var _ = Describe("OpenGauss", func() {
Context("Connection", func() {
It("Open and ping", func() {
- og, err := Open("dba3", "1234567890@ss", "school")
+ og, err := Open("dba3", "1234567890@ss", "school",
uint16(5432))
Expect(err).To(BeNil())
Expect(og).NotTo(BeNil())