From bc50af442144d34c249077c2dd5012bc8521165f Mon Sep 17 00:00:00 2001 From: Daniel Linsley Date: Tue, 4 Jun 2024 22:10:06 +0000 Subject: [PATCH] do not download release for update stemcell unless required Signed-off-by: Daniel Linsley --- internal/commands/update_stemcell.go | 17 ++++++++++------- .../component/artifactory_release_source.go | 13 ++++++++++--- .../artifactory_release_source_test.go | 1 + internal/component/bosh_io_release_source.go | 15 ++++++++------- .../component/bosh_io_release_source_test.go | 5 +++-- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/internal/commands/update_stemcell.go b/internal/commands/update_stemcell.go index b70b68dbd..88e4505e5 100644 --- a/internal/commands/update_stemcell.go +++ b/internal/commands/update_stemcell.go @@ -97,16 +97,19 @@ func (update UpdateStemcell) Execute(args []string) error { update.Logger.Printf("No change for release %q\n", rel.Name) continue } - - local, err := releaseSource.DownloadRelease(update.Options.ReleasesDir, remote) - if err != nil { - return fmt.Errorf("while downloading release %q, encountered error: %w", rel.Name, err) - } - lock := &kilnfileLock.Releases[i] - lock.SHA1 = local.Lock.SHA1 + lock.RemotePath = remote.RemotePath lock.RemoteSource = remote.RemoteSource + lock.SHA1 = remote.SHA1 + if remote.SHA1 == "" || remote.SHA1 == "not-calculated" { + // release source needs to download. + local, err := releaseSource.DownloadRelease(update.Options.ReleasesDir, remote) + if err != nil { + return fmt.Errorf("while downloading release %q, encountered error: %w", rel.Name, err) + } + lock.SHA1 = local.Lock.SHA1 + } } kilnfileLock.Stemcell.Version = trimmedInputVersion diff --git a/internal/component/artifactory_release_source.go b/internal/component/artifactory_release_source.go index 67c5e11d5..4d8435b95 100644 --- a/internal/component/artifactory_release_source.go +++ b/internal/component/artifactory_release_source.go @@ -178,13 +178,20 @@ func (ars *ArtifactoryReleaseSource) GetMatchedRelease(spec cargo.BOSHReleaseTar default: return cargo.BOSHReleaseTarballLock{}, fmt.Errorf("unexpected http status: %s", http.StatusText(response.StatusCode)) } - - return cargo.BOSHReleaseTarballLock{ + + matchedRelease := cargo.BOSHReleaseTarballLock{ Name: spec.Name, Version: spec.Version, RemotePath: remotePath, RemoteSource: ars.ID, - }, nil + } + + matchedRelease.SHA1, err = ars.getFileSHA1(matchedRelease) + if err != nil { + return cargo.BOSHReleaseTarballLock{}, err + } + + return matchedRelease, nil } // FindReleaseVersion may use any of the fields on Requirement to return the best matching diff --git a/internal/component/artifactory_release_source_test.go b/internal/component/artifactory_release_source_test.go index 098c771e2..2337e5e16 100644 --- a/internal/component/artifactory_release_source_test.go +++ b/internal/component/artifactory_release_source_test.go @@ -106,6 +106,7 @@ var _ = Describe("interacting with BOSH releases on Artifactory", func() { // StemcellVersion: "9.9", RemotePath: "bosh-releases/smoothie/9.9/mango/mango-2.3.4-smoothie-9.9.tgz", RemoteSource: "some-mango-tree", + SHA1: "some-sha", })) }) diff --git a/internal/component/bosh_io_release_source.go b/internal/component/bosh_io_release_source.go index 492a21f82..0260de62e 100644 --- a/internal/component/bosh_io_release_source.go +++ b/internal/component/bosh_io_release_source.go @@ -92,13 +92,14 @@ func (src BOSHIOReleaseSource) GetMatchedRelease(requirement cargo.BOSHReleaseTa for _, repo := range repos { for _, suf := range suffixes { fullName := repo + "/" + requirement.Name + suf - exists, err := src.releaseExistOnBoshio(fullName, requirement.Version) + exists, remoteSha, err := src.releaseExistOnBoshio(fullName, requirement.Version) if err != nil { return cargo.BOSHReleaseTarballLock{}, err } if exists { builtRelease := src.createReleaseRemote(requirement, fullName) + builtRelease.SHA1 = remoteSha return builtRelease, nil } } @@ -135,7 +136,7 @@ func (src BOSHIOReleaseSource) FindReleaseVersion(spec cargo.BOSHReleaseTarballS } spec.Version = validReleases[0].Version lock := src.createReleaseRemote(spec, fullName) - lock.SHA1 = validReleases[0].SHA + lock.SHA1 = validReleases[0].SHA1 return lock, nil } } @@ -232,18 +233,18 @@ func (src BOSHIOReleaseSource) getReleases(name string) ([]releaseResponse, erro type releaseResponse struct { Version string `json:"version"` - SHA string `json:"sha1"` + SHA1 string `json:"sha1"` } -func (src BOSHIOReleaseSource) releaseExistOnBoshio(name, version string) (bool, error) { +func (src BOSHIOReleaseSource) releaseExistOnBoshio(name, version string) (bool, string, error) { releaseResponses, err := src.getReleases(name) if err != nil { - return false, err + return false, "", err } for _, rel := range releaseResponses { if rel.Version == version { - return true, nil + return true, rel.SHA1, nil } } - return false, nil + return false, "", nil } diff --git a/internal/component/bosh_io_release_source_test.go b/internal/component/bosh_io_release_source_test.go index a9c7bcc2d..447faf465 100644 --- a/internal/component/bosh_io_release_source_test.go +++ b/internal/component/bosh_io_release_source_test.go @@ -47,7 +47,7 @@ var _ = Describe("BOSHIOReleaseSource", func() { testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `null`)) path, _ = regexp.Compile(`/api/v1/releases/github.com/\S+/uaa.*`) - testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `[{"version": "73.3.0"}]`)) + testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `[{"version": "73.3.0", "sha1":"b6e8a9cbc8724edcecb8658fa9459ee6c8fc259e"}]`)) path, _ = regexp.Compile(`/api/v1/releases/github.com/\S+/metrics.*`) testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `[{"version": "2.3.0"}]`)) @@ -74,6 +74,7 @@ var _ = Describe("BOSHIOReleaseSource", func() { Version: "73.3.0", RemotePath: uaaURL, RemoteSource: component.ReleaseSourceTypeBOSHIO, + SHA1: "b6e8a9cbc8724edcecb8658fa9459ee6c8fc259e", })) foundRelease, err = releaseSource.GetMatchedRelease(rabbitmqRequirement) @@ -299,7 +300,7 @@ var _ = Describe("BOSHIOReleaseSource", func() { testServer = ghttp.NewServer() path, _ := regexp.Compile(`/api/v1/releases/github.com/\S+/cf-rabbitmq.*`) - testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `[{"name":"github.com/cloudfoundry/cf-rabbitmq-release","version":"309.0.5","url":"https://bosh.io/d/github.com/cloudfoundry/cf-rabbitmq-release?v=309.0.0","sha1":"5df538657c2cc830bda679420a9b162682018ded"},{"name":"github.com/cloudfoundry/cf-rabbitmq-release","version":"308.0.0","url":"https://bosh.io/d/github.com/cloudfoundry/cf-rabbitmq-release?v=308.0.0","sha1":"56202c9a466a8394683ae432ee2dea21ef6ef865"}]`)) + testServer.RouteToHandler("GET", path, ghttp.RespondWith(http.StatusOK, `[{"name":"github.com/cloudfoundry/cf-rabbitmq-release","version":"309.0.5","url":"https://bosh.io/d/github.com/cloudfoundry/cf-rabbitmq-release?v=309.0.0","sha1":"5df538657c2cc830bda679420a9b162682018ded","sha256": "49cad4a9758026cbae7a26f77921300595459dfa6bc0ca5332fc6ba52bd336b8"},{"name":"github.com/cloudfoundry/cf-rabbitmq-release","version":"308.0.0","url":"https://bosh.io/d/github.com/cloudfoundry/cf-rabbitmq-release?v=308.0.0","sha1":"56202c9a466a8394683ae432ee2dea21ef6ef865","sha256":"5970d4211d236896d9366b150a66e38cbbd757fed31895962e499bb5458e0937"}]`)) releaseSource = component.NewBOSHIOReleaseSource(cargo.ReleaseSourceConfig{ID: ID, Publishable: false}, testServer.URL(), logger) })