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

for update-stemcell, only download releases if necessary #493

Merged
merged 2 commits into from
Dec 9, 2024
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
17 changes: 10 additions & 7 deletions internal/commands/update_stemcell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Copy link
Contributor

@crhntr crhntr Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is "not-calculated" from? Maybe pull this out into a const and add a note on what emits this value. If there was a test, that would also solve this question.

(It was cool to see the PR review request. I miss y'all. Hope you're all well.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The s3 and github sources can set this as not-calculated particularly when no-download specified.

// 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
Expand Down
74 changes: 66 additions & 8 deletions internal/commands/update_stemcell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ var _ = Describe("UpdateStemcell", func() {
release1Version = "1"
release2Name = "release2"
release2Version = "2"
release3Name = "release3"
release3Version = "3"

newRelease1SHA = "new-sha1-1"
newRelease1RemotePath = "new-remote-path-1"
newRelease2SHA = "new-sha1-2"
newRelease2RemotePath = "new-remote-path-2"
newRelease3SHA = "new-sha1-3"
newRelease3RemotePath = "new-remote-path-3"

publishableReleaseSourceID = "publishable"
unpublishableReleaseSourceID = "test-only"
Expand Down Expand Up @@ -66,8 +70,9 @@ var _ = Describe("UpdateStemcell", func() {
Version: "^1",
},
Releases: []cargo.BOSHReleaseTarballSpecification{
{Name: release1Name, GitHubRepository: "https://example.com/lemon", Version: "*"},
{Name: release1Name, GitHubRepository: "https://example.com/lemon", Version: "*"},
{Name: release2Name, GitHubRepository: "https://example.com/orange", Version: "*"},
{Name: release3Name, GitHubRepository: "https://example.com/pomelo", Version: "*"},
},
}
kilnfileLock = cargo.KilnfileLock{
Expand All @@ -86,6 +91,13 @@ var _ = Describe("UpdateStemcell", func() {
RemoteSource: "old-remote-source-2",
RemotePath: "old-remote-path-2",
},
{
Name: release3Name,
Version: release3Version,
SHA1: "old-sha-3",
RemoteSource: "old-remote-source-3",
RemotePath: "old-remote-path-3",
},
},
Stemcell: cargo.Stemcell{
OS: "old-os",
Expand All @@ -101,13 +113,23 @@ var _ = Describe("UpdateStemcell", func() {
Name: release1Name, Version: release1Version,
RemotePath: newRelease1RemotePath,
RemoteSource: publishableReleaseSourceID,
SHA1: "",
}
return remote, nil
case release2Name:
remote := cargo.BOSHReleaseTarballLock{
Name: release2Name, Version: release2Version,
RemotePath: newRelease2RemotePath,
RemoteSource: unpublishableReleaseSourceID,
SHA1: "not-calculated",
}
return remote, nil
case release3Name:
remote := cargo.BOSHReleaseTarballLock{
Name: release3Name, Version: release3Version,
RemotePath: newRelease3RemotePath,
RemoteSource: publishableReleaseSourceID,
SHA1: newRelease3SHA,
}
return remote, nil
default:
Expand All @@ -122,13 +144,23 @@ var _ = Describe("UpdateStemcell", func() {
Name: release1Name, Version: release1Version,
RemotePath: newRelease1RemotePath,
RemoteSource: publishableReleaseSourceID,
SHA1: "",
}
return remote, nil
case release2Name:
remote := cargo.BOSHReleaseTarballLock{
Name: release2Name, Version: release2Version,
RemotePath: newRelease2RemotePath,
RemoteSource: unpublishableReleaseSourceID,
SHA1: "not-calculated",
}
return remote, nil
case release3Name:
remote := cargo.BOSHReleaseTarballLock{
Name: release3Name, Version: release3Version,
RemotePath: newRelease3RemotePath,
RemoteSource: publishableReleaseSourceID,
SHA1: newRelease3SHA,
}
return remote, nil
default:
Expand All @@ -151,7 +183,7 @@ var _ = Describe("UpdateStemcell", func() {
}
return local, nil
default:
panic("unexpected release name '"+remote.Name +"'")
panic("unexpected release name '" + remote.Name + "'")
}
})

Expand Down Expand Up @@ -192,7 +224,7 @@ var _ = Describe("UpdateStemcell", func() {
OS: newStemcellOS,
Version: newStemcellVersion,
}))
Expect(updatedLockfile.Releases).To(HaveLen(2))
Expect(updatedLockfile.Releases).To(HaveLen(3))
Expect(updatedLockfile.Releases).To(ContainElement(
cargo.BOSHReleaseTarballLock{
Name: release1Name,
Expand All @@ -211,6 +243,15 @@ var _ = Describe("UpdateStemcell", func() {
RemotePath: newRelease2RemotePath,
},
))
Expect(updatedLockfile.Releases).To(ContainElement(
cargo.BOSHReleaseTarballLock{
Name: release3Name,
Version: release3Version,
SHA1: newRelease3SHA,
RemoteSource: publishableReleaseSourceID,
RemotePath: newRelease3RemotePath,
},
))
})

It("looks up the correct releases", func() {
Expand All @@ -219,7 +260,7 @@ var _ = Describe("UpdateStemcell", func() {
})
Expect(err).NotTo(HaveOccurred())

Expect(releaseSource.GetMatchedReleaseCallCount()).To(Equal(2))
Expect(releaseSource.GetMatchedReleaseCallCount()).To(Equal(3))

req1 := releaseSource.GetMatchedReleaseArgsForCall(0)
Expect(req1).To(Equal(cargo.BOSHReleaseTarballSpecification{
Expand All @@ -234,14 +275,21 @@ var _ = Describe("UpdateStemcell", func() {
StemcellOS: newStemcellOS, StemcellVersion: newStemcellVersion,
GitHubRepository: "https://example.com/orange",
}))

req3 := releaseSource.GetMatchedReleaseArgsForCall(2)
Expect(req3).To(Equal(cargo.BOSHReleaseTarballSpecification{
Name: release3Name, Version: release3Version,
StemcellOS: newStemcellOS, StemcellVersion: newStemcellVersion,
GitHubRepository: "https://example.com/pomelo",
}))
})
It("looks up the correct releases with --update-releases", func() {
err := update.Execute([]string{
"--kilnfile", kilnfilePath, "--version", "1.100", "--releases-directory", releasesDirPath, "--update-releases",
})
Expect(err).NotTo(HaveOccurred())

Expect(releaseSource.FindReleaseVersionCallCount()).To(Equal(2))
Expect(releaseSource.FindReleaseVersionCallCount()).To(Equal(3))

req1, noDownload1 := releaseSource.FindReleaseVersionArgsForCall(0)
Expect(req1).To(Equal(cargo.BOSHReleaseTarballSpecification{
Expand All @@ -260,8 +308,7 @@ var _ = Describe("UpdateStemcell", func() {
Expect(noDownload2).To(BeTrue())
})


It("downloads the correct releases", func() {
It("downloads 2 of the 3 correct releases, ", func() {
err := update.Execute([]string{
"--kilnfile", kilnfilePath, "--version", newStemcellVersion, "--releases-directory", releasesDirPath,
})
Expand All @@ -276,6 +323,7 @@ var _ = Describe("UpdateStemcell", func() {
Name: release1Name, Version: release1Version,
RemotePath: newRelease1RemotePath,
RemoteSource: publishableReleaseSourceID,
SHA1: "",
},
))

Expand All @@ -286,6 +334,7 @@ var _ = Describe("UpdateStemcell", func() {
Name: release2Name, Version: release2Version,
RemotePath: newRelease2RemotePath,
RemoteSource: unpublishableReleaseSourceID,
SHA1: "not-calculated",
},
))
})
Expand Down Expand Up @@ -390,7 +439,7 @@ var _ = Describe("UpdateStemcell", func() {
Version: newStemcellVersion,
}))

Expect(updatedLockfile.Releases).To(HaveLen(2))
Expect(updatedLockfile.Releases).To(HaveLen(3))
Expect(updatedLockfile.Releases).To(ContainElement(
cargo.BOSHReleaseTarballLock{
Name: release1Name,
Expand All @@ -409,6 +458,15 @@ var _ = Describe("UpdateStemcell", func() {
RemotePath: newRelease2RemotePath,
},
))
Expect(updatedLockfile.Releases).To(ContainElement(
cargo.BOSHReleaseTarballLock{
Name: release3Name,
Version: release3Version,
SHA1: newRelease3SHA,
RemoteSource: publishableReleaseSourceID,
RemotePath: newRelease3RemotePath,
},
))
})
})

Expand Down
13 changes: 10 additions & 3 deletions internal/component/artifactory_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/component/artifactory_release_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}))
})

Expand Down
15 changes: 8 additions & 7 deletions internal/component/bosh_io_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions internal/component/bosh_io_release_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]`))
Expand All @@ -74,6 +74,7 @@ var _ = Describe("BOSHIOReleaseSource", func() {
Version: "73.3.0",
RemotePath: uaaURL,
RemoteSource: component.ReleaseSourceTypeBOSHIO,
SHA1: "b6e8a9cbc8724edcecb8658fa9459ee6c8fc259e",
}))

foundRelease, err = releaseSource.GetMatchedRelease(rabbitmqRequirement)
Expand Down Expand Up @@ -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)
})
Expand Down
Loading