From dfeb70cd9964eaa443590dda6de8890c2025d9be Mon Sep 17 00:00:00 2001 From: Eyal Ben Moshe Date: Wed, 25 Dec 2024 21:53:53 +0200 Subject: [PATCH] Internal - Set a min Artifactory version for Visibility System usage (#1320) --- .../commands/transferfiles/transfer.go | 2 +- artifactory/utils/utils.go | 4 +- common/commands/command.go | 59 ++++++++++++------- go.mod | 2 +- go.sum | 4 +- utils/usage/usage.go | 2 +- utils/usage/visibility_system_manager.go | 2 +- 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/artifactory/commands/transferfiles/transfer.go b/artifactory/commands/transferfiles/transfer.go index f303c1ca9..31b3f77a8 100644 --- a/artifactory/commands/transferfiles/transfer.go +++ b/artifactory/commands/transferfiles/transfer.go @@ -289,7 +289,7 @@ func (tdc *TransferFilesCommand) reportTransferFilesUsage() { AttributeValue: sourceStorageInfo.BinariesSize, }, } - if err = usage.NewArtifactoryCallHome().SendUsage(coreutils.GetCliUserAgent(), tdc.CommandName(), tdc.targetStorageInfoManager.GetServiceManager(), reportUsageAttributes...); err != nil { + if err = usage.NewArtifactoryCallHome().Send(coreutils.GetCliUserAgent(), tdc.CommandName(), tdc.targetStorageInfoManager.GetServiceManager(), reportUsageAttributes...); err != nil { log.Debug(err.Error()) } } diff --git a/artifactory/utils/utils.go b/artifactory/utils/utils.go index 68831475e..0ebe42265 100644 --- a/artifactory/utils/utils.go +++ b/artifactory/utils/utils.go @@ -262,7 +262,7 @@ func CreateMetadataServiceManager(serviceDetails *config.ServerDetails, isDryRun return metadata.NewManager(serviceConfig) } -func CreateJfConnectServiceManager(serverDetails *config.ServerDetails) (jfconnect.Manager, error) { +func CreateJfConnectServiceManager(serverDetails *config.ServerDetails, httpRetries, httpRetryWaitMilliSecs int) (jfconnect.Manager, error) { certsPath, err := coreutils.GetJfrogCertsDir() if err != nil { return nil, err @@ -275,6 +275,8 @@ func CreateJfConnectServiceManager(serverDetails *config.ServerDetails) (jfconne SetServiceDetails(jfConnectAuth). SetCertificatesPath(certsPath). SetInsecureTls(serverDetails.InsecureTls). + SetHttpRetries(httpRetries). + SetHttpRetryWaitMilliSecs(httpRetryWaitMilliSecs). Build() if err != nil { return nil, err diff --git a/common/commands/command.go b/common/commands/command.go index 52bd83cf6..f103dfda7 100644 --- a/common/commands/command.go +++ b/common/commands/command.go @@ -3,15 +3,22 @@ package commands import ( "sync" + "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" coreusage "github.com/jfrog/jfrog-cli-core/v2/utils/usage" usageReporter "github.com/jfrog/jfrog-cli-core/v2/utils/usage" + rtClient "github.com/jfrog/jfrog-client-go/artifactory" "github.com/jfrog/jfrog-client-go/artifactory/usage" "github.com/jfrog/jfrog-client-go/utils/log" ) +const ( + minCallHomeArtifactoryVersion = "6.9.0" + minVisibilitySystemArtifactoryVersion = "7.102" +) + type Command interface { // Runs the command Run() error @@ -43,32 +50,45 @@ func reportUsage(command Command, channel chan<- bool) { serverDetails, err := command.ServerDetails() if err != nil { - log.Debug("Usage reporting:", err.Error()) + log.Debug("Usage reporting. Failed accessing ServerDetails.", err.Error()) + return + } + if serverDetails == nil || serverDetails.ArtifactoryUrl == "" { + return + } + serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false) + if err != nil { + log.Debug("Usage reporting. Failed creating the Artifactory Service Manager.", err.Error()) + return + } + artifactoryVersion, err := serviceManager.GetVersion() + if err != nil { + log.Debug("Usage reporting. Failed getting the version of Artifactory", err.Error()) return } - if serverDetails != nil { - var wg sync.WaitGroup + var wg sync.WaitGroup - // Report the usage to Artifactory's Call Home API. - if serverDetails.ArtifactoryUrl != "" { - wg.Add(1) - go func() { - defer wg.Done() - reportUsageToArtifactoryCallHome(command, serverDetails) - }() - } + // Report the usage to Artifactory's Call Home API. + if version.NewVersion(artifactoryVersion).AtLeast(minCallHomeArtifactoryVersion) { + wg.Add(1) + go func() { + defer wg.Done() + reportUsageToArtifactoryCallHome(command, serviceManager) + }() + } - // Report the usage to the Visibility System. + // Report the usage to the Visibility System. + if version.NewVersion(artifactoryVersion).AtLeast(minVisibilitySystemArtifactoryVersion) { wg.Add(1) go func() { defer wg.Done() reportUsageToVisibilitySystem(command, serverDetails) }() - - // Wait for the two report actions to finish. - wg.Wait() } + + // Wait for the two report actions to finish. + wg.Wait() } func reportUsageToVisibilitySystem(command Command, serverDetails *config.ServerDetails) { @@ -77,14 +97,9 @@ func reportUsageToVisibilitySystem(command Command, serverDetails *config.Server } } -func reportUsageToArtifactoryCallHome(command Command, serverDetails *config.ServerDetails) { +func reportUsageToArtifactoryCallHome(command Command, serviceManager rtClient.ArtifactoryServicesManager) { log.Debug(usageReporter.ArtifactoryCallHomePrefix, "Sending info...") - serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false) - if err != nil { - log.Debug(usageReporter.ArtifactoryCallHomePrefix, err.Error()) - return - } - if err = usage.NewArtifactoryCallHome().SendUsage(coreutils.GetCliUserAgent(), command.CommandName(), serviceManager); err != nil { + if err := usage.NewArtifactoryCallHome().Send(coreutils.GetCliUserAgent(), command.CommandName(), serviceManager); err != nil { log.Debug(err.Error()) } } diff --git a/go.mod b/go.mod index 01ee183f8..a8cb53bdc 100644 --- a/go.mod +++ b/go.mod @@ -96,7 +96,7 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) -replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20241223175448-88f1089d0694 +replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20241225183733-80a5e1ba7a2c // replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd diff --git a/go.sum b/go.sum index be2367cd0..ed38a78c6 100644 --- a/go.sum +++ b/go.sum @@ -93,8 +93,8 @@ github.com/jfrog/build-info-go v1.10.7 h1:10NVHYg0193gJpQft+S4WQfvYMtj5jlwwhJRvk github.com/jfrog/build-info-go v1.10.7/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE= github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s= github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4= -github.com/jfrog/jfrog-client-go v1.28.1-0.20241223175448-88f1089d0694 h1:1EP8yAhB+SGTXhfGa/w4S748S12qJKXU4u/yiAJxnX0= -github.com/jfrog/jfrog-client-go v1.28.1-0.20241223175448-88f1089d0694/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec= +github.com/jfrog/jfrog-client-go v1.28.1-0.20241225183733-80a5e1ba7a2c h1:MoNDrchpG4Bf0Buy6OwTk9G+bCjPf1UjMK99mop0zxQ= +github.com/jfrog/jfrog-client-go v1.28.1-0.20241225183733-80a5e1ba7a2c/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= diff --git a/utils/usage/usage.go b/utils/usage/usage.go index b73cfd2b4..6834b63d3 100644 --- a/utils/usage/usage.go +++ b/utils/usage/usage.go @@ -169,7 +169,7 @@ func (ur *UsageReporter) reportToArtifactory(features ...ReportFeature) (err err if err != nil { return } - return usage.NewArtifactoryCallHome().SendUsageToArtifactory(ur.ProductId, serviceManager, converted...) + return usage.NewArtifactoryCallHome().SendToArtifactory(ur.ProductId, serviceManager, converted...) } func convertAttributesToMap(reportFeature ReportFeature) (converted map[string]string) { diff --git a/utils/usage/visibility_system_manager.go b/utils/usage/visibility_system_manager.go index 17fb6b6b6..d8baf29b1 100644 --- a/utils/usage/visibility_system_manager.go +++ b/utils/usage/visibility_system_manager.go @@ -39,7 +39,7 @@ func (vsm *VisibilitySystemManager) createMetric(commandName string) services.Vi } func (vsm *VisibilitySystemManager) SendUsage(commandName string) error { - manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails) + manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails, -1, 0) if err != nil { return err }