github.com/jfrog/jfrog-client-go@v1.40.2/distribution/services/distribute.go (about) 1 package services 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/jfrog/jfrog-client-go/auth" 7 "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" 8 clientUtils "github.com/jfrog/jfrog-client-go/utils" 9 "github.com/jfrog/jfrog-client-go/utils/distribution" 10 "github.com/jfrog/jfrog-client-go/utils/errorutils" 11 "github.com/jfrog/jfrog-client-go/utils/log" 12 ) 13 14 const defaultMaxWaitMinutes = 60 // 1 hour 15 const DefaultDistributeSyncSleepIntervalSeconds = 10 // 10 seconds 16 17 type DistributeReleaseBundleV1Service struct { 18 client *jfroghttpclient.JfrogHttpClient 19 DistDetails auth.ServiceDetails 20 DryRun bool 21 Sync bool 22 AutoCreateRepo bool 23 // Max time in minutes to wait for sync distribution to finish. 24 MaxWaitMinutes int 25 DistributeParams distribution.DistributionParams 26 } 27 28 func (dr *DistributeReleaseBundleV1Service) GetHttpClient() *jfroghttpclient.JfrogHttpClient { 29 return dr.client 30 } 31 32 func (dr *DistributeReleaseBundleV1Service) ServiceDetails() auth.ServiceDetails { 33 return dr.DistDetails 34 } 35 36 func (dr *DistributeReleaseBundleV1Service) IsDryRun() bool { 37 return dr.DryRun 38 } 39 40 func (dr *DistributeReleaseBundleV1Service) IsSync() bool { 41 return dr.Sync 42 } 43 44 func (dr *DistributeReleaseBundleV1Service) GetMaxWaitMinutes() int { 45 return dr.MaxWaitMinutes 46 } 47 48 func (dr *DistributeReleaseBundleV1Service) GetRestApi(name, version string) string { 49 return "api/v1/distribution/" + name + "/" + version 50 } 51 52 func (dr *DistributeReleaseBundleV1Service) GetDistributeBody() any { 53 return distribution.CreateDistributeV1Body(dr.DistributeParams.DistributionRules, dr.DryRun, dr.AutoCreateRepo) 54 } 55 56 func (dr *DistributeReleaseBundleV1Service) GetDistributionParams() distribution.DistributionParams { 57 return dr.DistributeParams 58 } 59 60 func (dr *DistributeReleaseBundleV1Service) GetProjectKey() string { 61 return "" 62 } 63 64 func NewDistributeReleaseBundleV1Service(client *jfroghttpclient.JfrogHttpClient) *DistributeReleaseBundleV1Service { 65 return &DistributeReleaseBundleV1Service{client: client} 66 } 67 68 func (dr *DistributeReleaseBundleV1Service) Distribute() error { 69 trackerId, err := distribution.DoDistribute(dr) 70 if err != nil || !dr.IsSync() || dr.IsDryRun() { 71 return err 72 } 73 74 // Sync distribution 75 return dr.waitForDistribution(&dr.DistributeParams, trackerId) 76 } 77 78 func (dr *DistributeReleaseBundleV1Service) waitForDistribution(distributeParams *distribution.DistributionParams, trackerId json.Number) error { 79 distributeBundleService := NewDistributionStatusService(dr.GetHttpClient()) 80 distributeBundleService.DistDetails = dr.ServiceDetails() 81 distributionStatusParams := DistributionStatusParams{ 82 Name: distributeParams.Name, 83 Version: distributeParams.Version, 84 TrackerId: trackerId.String(), 85 } 86 maxWaitMinutes := defaultMaxWaitMinutes 87 if dr.GetMaxWaitMinutes() >= 1 { 88 maxWaitMinutes = dr.GetMaxWaitMinutes() 89 } 90 distributingMessage := fmt.Sprintf("Sync: Distributing %s/%s...", distributeParams.Name, distributeParams.Version) 91 retryExecutor := &clientUtils.RetryExecutor{ 92 MaxRetries: maxWaitMinutes * 60 / DefaultDistributeSyncSleepIntervalSeconds, 93 RetriesIntervalMilliSecs: DefaultDistributeSyncSleepIntervalSeconds * 1000, 94 ErrorMessage: "", 95 LogMsgPrefix: distributingMessage, 96 ExecutionHandler: func() (bool, error) { 97 response, err := distributeBundleService.GetStatus(distributionStatusParams) 98 if err != nil { 99 return false, errorutils.CheckError(err) 100 } 101 if (*response)[0].Status == distribution.Failed { 102 bytes, err := json.Marshal(response) 103 if err != nil { 104 return false, errorutils.CheckError(err) 105 } 106 return false, errorutils.CheckErrorf("Distribution failed: " + clientUtils.IndentJson(bytes)) 107 } 108 if (*response)[0].Status == distribution.Completed { 109 log.Info("Distribution Completed!") 110 return false, nil 111 } 112 // Keep trying to get an answer 113 log.Info(distributingMessage) 114 return true, nil 115 }, 116 } 117 return retryExecutor.Execute() 118 }