Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(PITR): install mysqlbinlog (#1341)
h3n4l827723520c18
diff --git a/resources/mysqlbinlog/mysqlbinlog.go b/resources/mysqlbinlog/mysqlbinlog.gonew file mode 100644index 0000000000..8822191085--- /dev/null+++ b/resources/mysqlbinlog/mysqlbinlog.go@@ -0,0 +1,54 @@+package mysqlbinlog++import (+ "fmt"+ "os"+ "path"+ "path/filepath"+ "runtime"+ "strings"++ "github.com/bytebase/bytebase/resources/utils"+)++// Install will extract the mysqlbinlog tar in resourceDir, returns the mysqlbinlog binary directory if no error occur.+func Install(resourceDir string) (string, error) {+ var tarName string+ switch {+ case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64":+ tarName = "mysqlbinlog-8.0.28-macos11-arm64.tar.gz"+ case runtime.GOOS == "linux" && runtime.GOARCH == "amd64":+ tarName = "mysqlbinlog-8.0.28-linux-glibc-2.17-x86_64.tar.gz"+ default:+ return "", fmt.Errorf("Unsupported combination of OS[%s] and ARCH[%s]", runtime.GOOS, runtime.GOARCH)+ }++ version := strings.TrimRight(tarName, "tar.gz")+ mysqlbinlogDir := path.Join(resourceDir, version)+ if _, err := os.Stat(mysqlbinlogDir); err != nil {+ if !os.IsNotExist(err) {+ return "", fmt.Errorf("failed to check binary directory path %q, error: %w", mysqlbinlogDir, err)+ }+ // Install if not exist yet+ tmpDir := path.Join(resourceDir, fmt.Sprintf("tmp-%s", version))+ if err := os.RemoveAll(tmpDir); err != nil {+ return "", fmt.Errorf("failed to remove mysqlbinlog binary temp directory %q, error: %w", tmpDir, err)+ }++ f, err := resources.Open(tarName)+ if err != nil {+ return "", fmt.Errorf("failed to find %q in embedded resources, error: %v", tarName, err)+ }+ defer f.Close()++ if err := utils.ExtractTarGz(f, tmpDir); err != nil {+ return "", fmt.Errorf("failed to extract tar.gz file, error: %w", err)+ }++ if err := os.Rename(tmpDir, mysqlbinlogDir); err != nil {+ return "", fmt.Errorf("failed to rename mysqlbinlog binary directory from %q to %q, error: %w", tmpDir, mysqlbinlogDir, err)+ }+ }++ return filepath.Join(mysqlbinlogDir, version, "bin"), nil+}diff --git a/resources/mysqlbinlog/mysqlbinlog_test.go b/resources/mysqlbinlog/mysqlbinlog_test.goindex b5d26d7bf6..9ab29b010a 100644--- a/resources/mysqlbinlog/mysqlbinlog_test.go+++ b/resources/mysqlbinlog/mysqlbinlog_test.go@@ -1,14 +1,11 @@package mysqlbinlogimport (- "fmt""os""os/exec""path/filepath"- "runtime""testing"- "github.com/bytebase/bytebase/resources/utils""github.com/stretchr/testify/require")@@ -16,31 +13,11 @@ import (// to check whether the lib extraction is correct.func TestRunBinary(t *testing.T) {a := require.New(t)-- var tarName string- var version string- fmt.Println(runtime.GOOS, runtime.GOARCH)- switch {- case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64":- tarName = "mysqlbinlog-8.0.28-macos11-arm64.tar.gz"- version = "mysqlbinlog-8.0.28-macos11-arm64"- case runtime.GOOS == "linux" && runtime.GOARCH == "amd64":- tarName = "mysqlbinlog-8.0.28-linux-glibc-2.17-x86_64.tar.gz"- version = "mysqlbinlog-8.0.28-linux-glibc-2.17-x86_64"- default:- t.Fatalf("Unsupported combination of OS[%s] and ARCH[%s]", runtime.GOOS, runtime.GOARCH)- }-- tarF, err := resources.Open(tarName)- a.NoError(err)-- defer tarF.Close()-tmpDir := t.TempDir()- err = utils.ExtractTarGz(tarF, tmpDir)+ mysqlbinlogDir, err := Install(tmpDir)a.NoError(err)- mysqlbinlogPath := filepath.Join(tmpDir, version, "bin", "mysqlbinlog")+ mysqlbinlogPath := filepath.Join(mysqlbinlogDir, "mysqlbinlog")cmd := exec.Command(mysqlbinlogPath, "-V")cmd.Stdout = os.Stderrcmd.Stderr = os.Stderrdiff --git a/server/server.go b/server/server.goindex 2a5f8328b0..af4ae84c05 100644--- a/server/server.go+++ b/server/server.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""net/http"+ "path""strings""sync""time"@@ -12,6 +13,7 @@ import ("github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/metric"metricCollector "github.com/bytebase/bytebase/metric/collector"+ "github.com/bytebase/bytebase/resources/mysqlbinlog""github.com/bytebase/bytebase/store""github.com/google/uuid"@@ -55,7 +57,9 @@ type Server struct {startedTs int64secret string- // boot specifies that the server+ mysqlbinlogDir string++ // boot specifies that whether the server boot correctlycancel context.CancelFuncboot bool}@@ -93,8 +97,16 @@ func NewServer(ctx context.Context, prof Profile, logger *zap.Logger, loggerLevefmt.Printf("debug=%t\n", prof.Debug)fmt.Println("-----Config END-------")- // New MetadataDB instance.var err error+ // TODO(zp): refactor to keep consistency with embedded Postgres.+ // Install mysqlbinlog.+ mysqlbinlogDir, err := mysqlbinlog.Install(path.Join(prof.DataDir, "resources"))+ if err != nil {+ return nil, fmt.Errorf("cannot install mysqlbinlog binary, error: %w", err)+ }+ s.mysqlbinlogDir = mysqlbinlogDir++ // New MetadataDB instance.if prof.useEmbedDB() {s.metaDB, err = store.NewMetadataDBWithEmbedPg(logger, prof.PgUser, prof.DataDir, prof.DemoDataDir, prof.Mode)} else {