Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: embed mongoutil which only contains mongosh and libs (#3756)

h3n4l09e1da8ea423
diff --git a/resources/README.md b/resources/README.md
index d7a7a20923..9bd7f99e4d 100644
--- a/resources/README.md
+++ b/resources/README.md
@@ -29,3 +29,13 @@ linux-glibc2.17-x86_64 used for Linux (MD5 55a7759e25cc527416150c8181ce3f6d): ht
macos11-arm64 used for MacOS Apple Silicon development (MD5 f1943053b12428e4c0e4ed309a636fd0): https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.28-macos11-arm64.tar.gz, extract bin/mysqlbinlog, bin/mysql, bin/mysqldump, lib/libcrypto.1.1.dylib and lib/libssl.1.1.dylib.
macos11-x86_64 used for MacOS Intel Silicon development (MD5 b2d5b57edb92811040fd61c84f1c9d6f): https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.28-macos11-x86_64.tar.gz, extract bin/mysqlbinlog, bin/mysql, bin/mysqldump, lib/libcrypto.1.1.dylib and lib/libssl.1.1.dylib.
+
+### mongoutil
+
+We embed mongoutil to execute the MongoDB commands input by users. It only contains the mongosh executable and the depending libs.
+
+monogoutil-linux-x86_64 used for Linux x86_64 (MD5 39aceeced14007e62d729ff0c2db74b9): https://downloads.mongodb.com/compass/mongosh-1.6.1-linux-x64.tgz, extract bin/mongosh, bin/mongosh_crypt_v1.so.
+
+monogoutil-darwin-arm64 used for MacOS Apple Silicon development (MD5 bea4e6d46e904773e6145d929fe65146): https://downloads.mongodb.com/compass/mongosh-1.6.1-darwin-arm64.zip, extract bin/mongosh, bin/mongosh_crypt_v1.dylib.
+
+monogoutil-darwin-x86_64 used for MacOS Intel Silicon development (MD5 80bd2b3c453d3c80dc6896db3e0d9436): https://downloads.mongodb.com/compass/mongosh-1.6.1-darwin-x64.zip, extract bin/mongosh, bin/mongosh_crypt_v1.dylib.
diff --git a/resources/mongoutil/mongoutil.go b/resources/mongoutil/mongoutil.go
new file mode 100644
index 0000000000..14a65bbae4
--- /dev/null
+++ b/resources/mongoutil/mongoutil.go
@@ -0,0 +1,81 @@
+// Package mongoutil provides the resource for MongoDB utility packages.
+package mongoutil
+
+import (
+ "fmt"
+ "os"
+ "path"
+ "runtime"
+ "strings"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/resources/utils"
+)
+
+// nolint
+// GetMongoshPath returns the mongosh path.
+func GetMongoshPath(binDir string) string {
+ return path.Join(binDir, "mongosh")
+}
+
+// getTarnameAndVersion returns the mongoutil tarball name and version string.
+func getTarNameAndVersion() (tarname string, version string, err error) {
+ var tarName string
+ switch {
+ case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64":
+ tarName = "mongoutil-darwin-arm64.txz"
+ case runtime.GOOS == "darwin" && runtime.GOARCH == "amd64":
+ tarName = "mongoutil-darwin-x86_64.txz"
+ case runtime.GOOS == "linux" && runtime.GOARCH == "amd64":
+ tarName = "mongoutil-linux-x86_64.txz"
+ default:
+ return "", "", errors.Errorf("unsupported platform: %s/%s", runtime.GOOS, runtime.GOARCH)
+ }
+ return tarName, strings.TrimRight(tarName, "txz"), nil
+}
+
+// Install installs mongoutil in resourceDir.
+func Install(resourceDir string) (string, error) {
+ tarName, version, err := getTarNameAndVersion()
+ if err != nil {
+ return "", errors.Wrap(err, "failed to get tarball name and version")
+ }
+
+ mysqlutilDir := path.Join(resourceDir, version)
+ if _, err := os.Stat(mysqlutilDir); err != nil {
+ if !os.IsNotExist(err) {
+ return "", errors.Wrapf(err, "failed to check binary directory path %q", mysqlutilDir)
+ }
+ // Install if not exist yet
+ if err := installImpl(resourceDir, mysqlutilDir, tarName, version); err != nil {
+ return "", errors.Wrap(err, "cannot install mongoutil")
+ }
+ }
+
+ return path.Join(mysqlutilDir, "bin"), nil
+}
+
+// installImpl installs mongoutil in resourceDir.
+func installImpl(resourceDir, mongoutilDir, tarName, version string) error {
+ tmpDir := path.Join(resourceDir, fmt.Sprintf("tmp-%s", version))
+ if err := os.RemoveAll(tmpDir); err != nil {
+ return errors.Wrapf(err, "failed to remove mysqlutil binaries temp directory %q", tmpDir)
+ }
+
+ f, err := resources.Open(tarName)
+ if err != nil {
+ return errors.Wrapf(err, "failed to find %q in embedded resources", tarName)
+ }
+ defer f.Close()
+
+ if err := utils.ExtractTarXz(f, tmpDir); err != nil {
+ return errors.Wrap(err, "failed to extract tar.gz file")
+ }
+
+ if err := os.Rename(tmpDir, mongoutilDir); err != nil {
+ return errors.Wrapf(err, "failed to rename mongoutil binaries directory from %q to %q", tmpDir, mongoutilDir)
+ }
+
+ return nil
+}
diff --git a/resources/mongoutil/mongoutil_test.go b/resources/mongoutil/mongoutil_test.go
new file mode 100644
index 0000000000..cb19a99c2b
--- /dev/null
+++ b/resources/mongoutil/mongoutil_test.go
@@ -0,0 +1,24 @@
+package mongoutil
+
+import (
+ "os/exec"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestRunBinary tests whether all binarys can be started on the target platform.
+func TestRunBinary(t *testing.T) {
+ t.Parallel()
+
+ a := require.New(t)
+ tmpDir := t.TempDir()
+ binDir, err := Install(tmpDir)
+ a.NoError(err)
+ expectedVersion := `1.6.1`
+ cmd := exec.Command(GetMongoshPath(binDir), "--version")
+ out, err := cmd.CombinedOutput()
+ a.NoError(err)
+ a.Equal(strings.TrimSuffix(string(out), "\n"), expectedVersion)
+}
diff --git a/resources/mongoutil/resources_darwin_amd64.go b/resources/mongoutil/resources_darwin_amd64.go
new file mode 100644
index 0000000000..e378b6c3c1
--- /dev/null
+++ b/resources/mongoutil/resources_darwin_amd64.go
@@ -0,0 +1,6 @@
+package mongoutil
+
+import "embed"
+
+//go:embed mongoutil-darwin-x86_64.txz
+var resources embed.FS
diff --git a/resources/mongoutil/resources_darwin_arm64.go b/resources/mongoutil/resources_darwin_arm64.go
new file mode 100644
index 0000000000..8ba722418d
--- /dev/null
+++ b/resources/mongoutil/resources_darwin_arm64.go
@@ -0,0 +1,6 @@
+package mongoutil
+
+import "embed"
+
+//go:embed mongoutil-darwin-arm64.txz
+var resources embed.FS
diff --git a/resources/mongoutil/resources_linux_amd64.go b/resources/mongoutil/resources_linux_amd64.go
new file mode 100644
index 0000000000..8552ede587
--- /dev/null
+++ b/resources/mongoutil/resources_linux_amd64.go
@@ -0,0 +1,6 @@
+package mongoutil
+
+import "embed"
+
+//go:embed mongoutil-linux-x86_64.txz
+var resources embed.FS