Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor common functions and command prompt #469

Merged
merged 6 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pitr/cli/internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil"

"github.com/google/uuid"
Expand All @@ -42,6 +43,9 @@ const (
defaultInstance = "ins-default-ss"
// defaultShowDetailRetryTimes retry times of check backup detail from agent server
defaultShowDetailRetryTimes = 3

backupPromptFmt = "Please Check All Nodes Disk Space, Make Sure Have Enough Space To Backup Or Restore Data.\n" +
"Are you sure to continue? (Y/N)"
)

var filename string
Expand Down Expand Up @@ -161,10 +165,8 @@ func backup() error {
return xerr.NewCliErr(fmt.Sprintf("check disk space failed. err: %s", err))
}

prompt := fmt.Sprintf(
"Please Check All Nodes Disk Space, Make Sure Have Enough Space To Backup Or Restore Data.\n" +
"Are you sure to continue? (Y/N)")
err = getUserApproveInTerminal(prompt)
prompt := fmt.Sprintln(backupPromptFmt)
err = promptutil.GetUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}
Expand Down
3 changes: 2 additions & 1 deletion pitr/cli/internal/cmd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/httputils"
mock_httputils "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/httputils/mocks"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -327,7 +328,7 @@ var _ = Describe("test backup mock", func() {
monkey.Patch(pkg.NewAgentServer, func(addr string) pkg.IAgentServer {
return as
})
monkey.Patch(getUserApproveInTerminal, func(_ string) error {
monkey.Patch(promptutil.GetUserApproveInTerminal, func(_ string) error {
return nil
})
})
Expand Down
105 changes: 105 additions & 0 deletions pitr/cli/internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package cmd

import (
"fmt"
"os"

"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
"github.com/jedib0t/go-pretty/v6/table"
)

func validate(ls pkg.ILocalStorage, csn, recordID string) ([]*model.LsBackup, error) {
Expand Down Expand Up @@ -54,3 +57,105 @@ func validate(ls pkg.ILocalStorage, csn, recordID string) ([]*model.LsBackup, er

return baks, nil
}

func convertLocalhost(ip string) string {
if ip == "127.0.0.1" {
return Host
}
return ip
}

func checkAgentServerStatus(lsBackup *model.LsBackup) bool {

statusList := make([]*model.AgentServerStatus, 0)

// all agent server are available
available := true

// IMPORTANT: we don't support multiple agent server run on the same host
asMap := make(map[string]bool)
asDuplicate := false

for _, node := range lsBackup.SsBackup.StorageNodes {
sn := node
as := pkg.NewAgentServer(fmt.Sprintf("%s:%d", convertLocalhost(sn.IP), AgentPort))
in := &model.HealthCheckIn{
DBPort: sn.Port,
DBName: sn.Database,
Username: sn.Username,
Password: sn.Password,
}
if err := as.CheckStatus(in); err != nil {
statusList = append(statusList, &model.AgentServerStatus{IP: sn.IP, Port: AgentPort, Status: fmt.Sprintf("Unavailable: %s", err)})
available = false
} else {
statusList = append(statusList, &model.AgentServerStatus{IP: sn.IP, Port: AgentPort, Status: "Available"})
}
}

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetTitle("Agent Server Status")
t.AppendHeader(table.Row{"#", "Agent Server IP", "Agent Server Port", "Status"})

for i, s := range statusList {
t.AppendRow([]interface{}{i + 1, s.IP, s.Port, s.Status})
t.AppendSeparator()
}

t.Render()

for _, node := range lsBackup.SsBackup.StorageNodes {
if _, ok := asMap[node.IP]; ok {
asDuplicate = true
break
}
asMap[node.IP] = true
}

if asDuplicate {
logging.Error("IMPORTANT!: we don't support multiple agent server run on the same host.\n")
return false
}

return available
}

func checkDiskSpace(lsBackup *model.LsBackup) error {
var (
diskspaceList = make([]*model.DiskSpaceStatus, 0)
)
for _, sn := range lsBackup.SsBackup.StorageNodes {
var data string
as := pkg.NewAgentServer(fmt.Sprintf("%s:%d", convertLocalhost(sn.IP), AgentPort))
in := &model.DiskSpaceIn{
DiskPath: BackupPath,
}

out, err := as.ShowDiskSpace(in)

if err != nil {
data = "Check disk space failed."
} else {
data = out.Data
}

diskspaceList = append(diskspaceList, &model.DiskSpaceStatus{
IP: sn.IP,
Path: BackupPath,
DiskSpaceStatus: data,
})
}

// print diskspace result formatted
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetTitle("Disk Space Status")
t.AppendHeader(table.Row{"#", "Data Node IP", "Disk Path", "Disk Space Status"})
for i, ds := range diskspaceList {
t.AppendRow([]interface{}{i + 1, ds.IP, ds.Path, ds.DiskSpaceStatus})
t.AppendSeparator()
}
t.Render()
return nil
}
12 changes: 8 additions & 4 deletions pitr/cli/internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/jedib0t/go-pretty/v6/table"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -79,6 +80,11 @@ func init() {
DeleteCmd.Flags().StringVarP(&RecordID, "id", "", "", "backup record id")
}

const (
deletePromptFmt = "The backup record(ID: %s, CSN: %s) will be deleted forever.\n" +
"Are you sure to continue? (Y/N)"
)

//nolint:dupl
func deleteRecord() error {
// init local storage
Expand All @@ -101,10 +107,8 @@ func deleteRecord() error {
return xerr.NewCliErr("one or more agent server are not available.")
}

prompt := fmt.Sprintf(
"The backup record(ID: %s, CSN: %s) will be deleted forever.\n"+
"Are you sure to continue? (Y/N)", bak.Info.ID, bak.Info.CSN)
err = getUserApproveInTerminal(prompt)
prompt := fmt.Sprintf(deletePromptFmt, bak.Info.ID, bak.Info.CSN)
err = promptutil.GetUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}
Expand Down
3 changes: 2 additions & 1 deletion pitr/cli/internal/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg"
mock_pkg "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/mocks"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -75,7 +76,7 @@ var _ = Describe("test delete", func() {
monkey.Patch(pkg.NewLocalStorage, func(rootDir string) (pkg.ILocalStorage, error) {
return ls, nil
})
monkey.Patch(getUserApproveInTerminal, func(_ string) error {
monkey.Patch(promptutil.GetUserApproveInTerminal, func(_ string) error {
return nil
})
})
Expand Down
14 changes: 9 additions & 5 deletions pitr/cli/internal/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"

"github.com/jedib0t/go-pretty/v6/progress"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -86,6 +87,12 @@ func init() {
RestoreCmd.Flags().StringVarP(&RecordID, "id", "", "", "backup record id")
}

const (
restorePromptFmt = "Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n" +
"The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n" +
"Are you sure to continue? (Y/N)"
)

//nolint:dupl
func restore() error {
// init local storage
Expand All @@ -112,11 +119,8 @@ func restore() error {
return xerr.NewCliErr(fmt.Sprintf("check database exist failed. err: %s", err))
}

prompt := fmt.Sprintf(
"Detected That The Database [%s] Already Exists In ShardingSphere-Proxy Metadata.\n"+
"The Logic Database Will Be DROPPED And Then Insert Backup's Metadata Into ShardingSphere-Proxy After Restoring The Backup Data.\n"+
"Are you sure to continue? (Y/N)", strings.Join(databaseNamesExist, ","))
err = getUserApproveInTerminal(prompt)
prompt := fmt.Sprintf(restorePromptFmt, strings.Join(databaseNamesExist, ","))
err = promptutil.GetUserApproveInTerminal(prompt)
if err != nil {
return xerr.NewCliErr(fmt.Sprintf("%s", err))
}
Expand Down
5 changes: 3 additions & 2 deletions pitr/cli/internal/cmd/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
mock_pkg "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/mocks"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/promptutil"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -104,7 +105,7 @@ var _ = Describe("test restore", func() {
monkey.Patch(pkg.NewLocalStorage, func(rootDir string) (pkg.ILocalStorage, error) {
return ls, nil
})
monkey.Patch(getUserApproveInTerminal, func(_ string) error {
monkey.Patch(promptutil.GetUserApproveInTerminal, func(_ string) error {
return nil
})
})
Expand All @@ -115,7 +116,7 @@ var _ = Describe("test restore", func() {
})

It("check database if exists", func() {
monkey.Patch(getUserApproveInTerminal, func(_ string) error { return nil })
monkey.Patch(promptutil.GetUserApproveInTerminal, func(_ string) error { return nil })
proxy.EXPECT().ExportMetaData()
Expect(checkDatabaseExist(proxy, bak)).To(BeNil())
})
Expand Down
Loading
Loading