From 203446d8459f1e427f4b61ee3d910d6b18c16658 Mon Sep 17 00:00:00 2001 From: zyy17 Date: Sun, 8 Oct 2023 20:55:05 +0800 Subject: [PATCH] feat: support to download cn region binaries --- pkg/artifacts/constants.go | 3 +++ pkg/artifacts/manager.go | 43 ++++++++++++++++++++++++-------- pkg/artifacts/manager_test.go | 46 +++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/pkg/artifacts/constants.go b/pkg/artifacts/constants.go index aea03547..e315bd42 100644 --- a/pkg/artifacts/constants.go +++ b/pkg/artifacts/constants.go @@ -27,6 +27,9 @@ const ( // GreptimeDBCNBinaries is the URL of the GreptimeDB binaries that stored in the S3 bucket of the CN region. GreptimeDBCNBinaries = "https://downloads.greptime.cn/releases/greptimedb" + // EtcdCNBinaries is the URL of the etcd binaries that stored in the S3 bucket of the CN region. + EtcdCNBinaries = "https://downloads.greptime.cn/releases/etcd" + // LatestVersionTag is the tag of the latest version. LatestVersionTag = "latest" diff --git a/pkg/artifacts/manager.go b/pkg/artifacts/manager.go index 0a5beead..a9549091 100644 --- a/pkg/artifacts/manager.go +++ b/pkg/artifacts/manager.go @@ -151,7 +151,7 @@ func (m *manager) NewSource(name, version string, typ ArtifactType, fromCNRegion if src.Type == ArtifactTypeBinary { if src.Name == EtcdBinName { - downloadURL, err := m.etcdBinaryDownloadURL(src.Version) + downloadURL, err := m.etcdBinaryDownloadURL(src.Version, src.FromCNRegion) if err != nil { return nil, err } @@ -170,7 +170,7 @@ func (m *manager) NewSource(name, version string, typ ArtifactType, fromCNRegion specificVersion = latestVersion } - downloadURL, err := m.greptimeBinaryDownloadURL(specificVersion) + downloadURL, err := m.greptimeBinaryDownloadURL(specificVersion, src.FromCNRegion) if err != nil { return nil, err } @@ -376,7 +376,15 @@ func (m *manager) latestGitHubReleaseVersion(org, repo string) (string, error) { return *release.TagName, nil } -func (m *manager) etcdBinaryDownloadURL(version string) (string, error) { +func (m *manager) etcdBinaryDownloadURL(version string, fromCNRegion bool) (string, error) { + if version != DefaultEtcdBinVersion && fromCNRegion { + return "", fmt.Errorf("only support %s in cn region", DefaultEtcdBinVersion) + } + + if version == LatestVersionTag { + return "", fmt.Errorf("can't support latest version") + } + var ext string switch runtime.GOOS { @@ -388,14 +396,23 @@ func (m *manager) etcdBinaryDownloadURL(version string) (string, error) { return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS) } - // For the function stability, we always use the specific version of etcd. - downloadURL := fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/etcd-%s-%s-%s%s", - EtcdGitHubOrg, EtcdGithubRepo, version, version, runtime.GOOS, runtime.GOARCH, ext) + var downloadURL string + if fromCNRegion { + downloadURL = EtcdCNBinaries + } else { + downloadURL = fmt.Sprintf("https://github.com/%s/%s/releases/download", EtcdGitHubOrg, EtcdGithubRepo) + } - return downloadURL, nil + // For the function stability, we always use the specific version of etcd. + return fmt.Sprintf("%s/%s/etcd-%s-%s-%s%s", downloadURL, version, version, runtime.GOOS, runtime.GOARCH, ext), nil } -func (m *manager) greptimeBinaryDownloadURL(version string) (string, error) { +func (m *manager) greptimeBinaryDownloadURL(version string, fromCNRegion bool) (string, error) { + // FIXME(zyy17): we should support the latest version in the future. + if version == LatestVersionTag && fromCNRegion { + return "", fmt.Errorf("can't support latest version in cn region") + } + newVersion, err := isBreakingVersion(version) if err != nil { return "", err @@ -408,8 +425,14 @@ func (m *manager) greptimeBinaryDownloadURL(version string) (string, error) { packageName = fmt.Sprintf("greptime-%s-%s.tgz", runtime.GOOS, runtime.GOARCH) } - return fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s", - GreptimeGitHubOrg, GreptimeDBGithubRepo, version, packageName), nil + var downloadURL string + if fromCNRegion { + downloadURL = GreptimeDBCNBinaries + } else { + downloadURL = fmt.Sprintf("https://github.com/%s/%s/releases/download", GreptimeGitHubOrg, GreptimeDBGithubRepo) + } + + return fmt.Sprintf("%s/%s/%s", downloadURL, version, packageName), nil } // installBinaries installs the binaries to the installDir. diff --git a/pkg/artifacts/manager_test.go b/pkg/artifacts/manager_test.go index a8f54143..5526361a 100644 --- a/pkg/artifacts/manager_test.go +++ b/pkg/artifacts/manager_test.go @@ -117,6 +117,52 @@ func TestDownloadChartsFromCNRegion(t *testing.T) { } } +func TestDownloadBinariesFromCNRegion(t *testing.T) { + tempDir, err := os.MkdirTemp("/tmp", "gtctl-ut-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tempDir) + + m, err := NewManager(logger.New(os.Stdout, log.Level(4), logger.WithColored())) + if err != nil { + t.Fatalf("failed to create artifacts manager: %v", err) + } + + ctx := context.Background() + + tests := []struct { + name string + version string + typ ArtifactType + fromCNRegion bool + }{ + {GreptimeBinName, "v0.4.0-nightly-20231002", ArtifactTypeBinary, true}, + {EtcdBinName, DefaultEtcdBinVersion, ArtifactTypeBinary, true}, + } + for _, tt := range tests { + src, err := m.NewSource(tt.name, tt.version, tt.typ, tt.fromCNRegion) + if err != nil { + t.Errorf("failed to create source: %v", err) + } + artifactFile, err := m.DownloadTo(ctx, src, destDir(tempDir, src), &DownloadOptions{UseCache: false}) + if err != nil { + t.Errorf("failed to download: %v", err) + } + + info, err := os.Stat(artifactFile) + if os.IsNotExist(err) { + t.Errorf("artifact file does not exist: %v", err) + } + if info.Mode()&0111 == 0 { + t.Errorf("binary file is not executable") + } + if err != nil { + t.Errorf("failed to stat artifact file: %v", err) + } + } +} + func TestDownloadBinaries(t *testing.T) { tempDir, err := os.MkdirTemp("/tmp", "gtctl-ut-") if err != nil {