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

feat: support to download cn region binaries #162

Merged
merged 1 commit into from
Oct 9, 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
3 changes: 3 additions & 0 deletions pkg/artifacts/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
43 changes: 33 additions & 10 deletions pkg/artifacts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions pkg/artifacts/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down