Skip to content

Commit

Permalink
refactor: use BOSH release manifest reader
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jul 5, 2023
1 parent 94c4193 commit 97af195
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 57 deletions.
63 changes: 9 additions & 54 deletions internal/builder/release_manifest_reader.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package builder

import (
"archive/tar"
"compress/gzip"
"crypto/sha1"
"fmt"
"io"
Expand All @@ -11,8 +9,7 @@ import (

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"

"gopkg.in/yaml.v2"
"github.com/pivotal-cf/kiln/pkg/tile"
)

type ReleaseManifest struct {
Expand All @@ -24,13 +21,6 @@ type ReleaseManifest struct {
StemcellVersion string `yaml:"-"`
}

// inputReleaseManifest is a subset of release.MF
type inputReleaseManifest struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
CompiledPackages []compiledPackage `yaml:"compiled_packages"`
}

type compiledPackage struct {
Stemcell string `yaml:"stemcell"`
}
Expand All @@ -54,54 +44,19 @@ func (r ReleaseManifestReader) Read(releaseTarball string) (Part, error) {
}
defer closeAndIgnoreError(file)

// TODO: use component.ReadReleaseManifest
// we could not do it yet due to a circular package reference where we import builder in the local release source

gr, err := gzip.NewReader(file)
if err != nil {
return Part{}, err
}
defer closeAndIgnoreError(gr)

tr := tar.NewReader(gr)

var header *tar.Header
for {
header, err = tr.Next()
if err != nil {
if err == io.EOF {
return Part{}, fmt.Errorf("could not find release.MF in %q", releaseTarball)
}

return Part{}, fmt.Errorf("error while reading %q: %s", releaseTarball, err)
}

if filepath.Base(header.Name) == "release.MF" {
break
}
}

var inputReleaseManifest inputReleaseManifest
inputReleaseManifestContents, err := io.ReadAll(tr)
if err != nil {
return Part{}, err // NOTE: cannot replicate this error scenario in a test
}

err = yaml.Unmarshal(inputReleaseManifestContents, &inputReleaseManifest)
inputReleaseManifest, err := tile.ReadProductTemplatePartFromBOSHReleaseTarball(file)
if err != nil {
return Part{}, err
}

var stemcellOS, stemcellVersion string
compiledPackages := inputReleaseManifest.CompiledPackages
if len(compiledPackages) > 0 {
inputStemcell := inputReleaseManifest.CompiledPackages[0].Stemcell
stemcellParts := strings.Split(inputStemcell, "/")
if len(stemcellParts) != 2 {
return Part{}, fmt.Errorf("Invalid format for compiled package stemcell inside release.MF (expected 'os/version'): %s", inputStemcell)
var stemcellOS,
stemcellVersion string
if len(inputReleaseManifest.CompiledPackages) > 0 {
var found bool
stemcellOS, stemcellVersion, found = strings.Cut(inputReleaseManifest.CompiledPackages[0].Stemcell, "/")
if !found {
return Part{}, fmt.Errorf("%s/%s has invalid stemcell: %q", inputReleaseManifest.Name, inputReleaseManifest.Version, inputReleaseManifest.CompiledPackages[0].Stemcell)
}
stemcellOS = stemcellParts[0]
stemcellVersion = stemcellParts[1]
}

outputReleaseManifest := ReleaseManifest{
Expand Down
6 changes: 3 additions & 3 deletions internal/builder/release_manifest_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ version: 1.2.3
Expect(gw.Close()).NotTo(HaveOccurred())

_, err = reader.Read(tarball.Name())
Expect(err).To(MatchError(fmt.Sprintf("could not find release.MF in %q", tarball.Name())))
Expect(err).To(MatchError("failed to find release.MF in tarball"))
})
})

Expand Down Expand Up @@ -220,7 +220,7 @@ version: 1.2.3
Expect(err).NotTo(HaveOccurred())

_, err = reader.Read(tarball.Name())
Expect(err).To(MatchError(fmt.Sprintf("could not find release.MF in %q", tarball.Name())))
Expect(err).To(MatchError(fmt.Sprintf("failed to find release.MF in tarball")))
})
})

Expand All @@ -242,7 +242,7 @@ version: 1.2.3
Expect(err).NotTo(HaveOccurred())

_, err = reader.Read(tarball.Name())
Expect(err).To(MatchError(fmt.Sprintf("error while reading %q: unexpected EOF", tarball.Name())))
Expect(err).To(MatchError("unexpected EOF"))
})
})

Expand Down

0 comments on commit 97af195

Please sign in to comment.