Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

cmd/plume/fcos: modify build metadata structure #1015

Merged
merged 1 commit into from
Jun 27, 2019
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
4 changes: 0 additions & 4 deletions cmd/plume/fcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var (

func AddFcosSpecFlags(flags *pflag.FlagSet) {
flags.StringVar(&specPolicy, "policy", "public-read", "Canned ACL policy")
flags.StringVar(&specCommitId, "commit-id", "", "OSTree Commit ID")
}

func FcosValidateArguments() {
Expand All @@ -41,9 +40,6 @@ func FcosValidateArguments() {
if specChannel == "" {
plog.Fatal("--channel is required")
}
if specCommitId == "" {
plog.Fatal("--commit-id is required")
}
}

func FcosChannelSpec() fcosChannelSpec {
Expand Down
78 changes: 73 additions & 5 deletions cmd/plume/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,62 @@ func modifyReleaseMetadataIndex(spec *fcosChannelSpec, commitId string) {
plog.Fatalf("unmarshaling release metadata json: %v", err)
}

url, err := url.Parse(fmt.Sprintf("https://%s.s3.amazonaws.com/prod/streams/%s/builds/%s/release.json", spec.Bucket, specChannel, specVersion))
releasePath := filepath.Join("prod", "streams", specChannel, "builds", specVersion, "release.json")
jlebon marked this conversation as resolved.
Show resolved Hide resolved
url, err := url.Parse(fmt.Sprintf("https://%s.s3.amazonaws.com/%s", spec.Bucket, releasePath))
if err != nil {
plog.Fatalf("creating metadata url: %v", err)
}

releaseFile, err := api.DownloadFile(spec.Bucket, releasePath)
if err != nil {
plog.Fatalf("downloading release metadata: %v", err)
}
defer releaseFile.Close()

releaseData, err := ioutil.ReadAll(releaseFile)
if err != nil {
plog.Fatalf("reading release metadata: %v", err)
}

var im IndividualReleaseMetadata
err = json.Unmarshal(releaseData, &im)
if err != nil {
plog.Fatalf("unmarshaling release metadata: %v", err)
}

var commits []Commit
for arch, vals := range im.Architectures {
commits = append(commits, Commit{
Architecture: arch,
Checksum: vals.Commit,
lucab marked this conversation as resolved.
Show resolved Hide resolved
})
}

newRel := BuildMetadata{
CommitHash: specCommitId,
CommitHash: commits,
Version: specVersion,
Endpoint: url.String(),
}

for i, rel := range m.Releases {
if rel == newRel {
if compareStaticReleaseInfo(rel, newRel) {
if i != (len(m.Releases) - 1) {
plog.Fatalf("build is already present and is not the latest release")
lucab marked this conversation as resolved.
Show resolved Hide resolved
}

// the build is already the latest release, exit
return
comp := compareCommits(rel.CommitHash, newRel.CommitHash)
if comp == 0 {
// the build is already the latest release, exit
return
} else if comp == -1 {
// the build is present and contains a subset of the new release data,
// pop the old entry and add the new version
m.Releases = m.Releases[:len(m.Releases)-1]
break
} else {
// the commit hash of the new build is not a superset of the current release
plog.Fatalf("build is present but commit hashes are not a superset of latest release")
}
}
}

Expand All @@ -555,3 +592,34 @@ func modifyReleaseMetadataIndex(spec *fcosChannelSpec, commitId string) {
plog.Fatalf("uploading release metadata json: %v", err)
}
}

func compareStaticReleaseInfo(a, b BuildMetadata) bool {
if a.Version != b.Version || a.Endpoint != b.Endpoint {
return false
}
return true
}

// returns -1 if a is a subset of b, 0 if equal, 1 if a is not a subset of b
func compareCommits(a, b []Commit) int {
if len(a) > len(b) {
lucab marked this conversation as resolved.
Show resolved Hide resolved
return 1
}
sameLength := len(a) == len(b)
for _, aHash := range a {
found := false
for _, bHash := range b {
if aHash.Architecture == bHash.Architecture && aHash.Checksum == bHash.Checksum {
found = true
break
}
}
if !found {
return 1
}
}
if sameLength {
return 0
}
return -1
}
19 changes: 16 additions & 3 deletions cmd/plume/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,24 @@ type ReleaseMetadata struct {
}

type BuildMetadata struct {
CommitHash string `json:"commit"`
Version string `json:"version"`
Endpoint string `json:"endpoint"`
CommitHash []Commit `json:"commits"`
Version string `json:"version"`
Endpoint string `json:"metadata"`
}

type Metadata struct {
LastModified string `json:"last-modified"`
}

type IndividualReleaseMetadata struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think at some later point (even in another PR) it would make sense to split structures belonging to different JSON-documents in different source files. It took me a bit to realize where this one was coming from.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like that might cause confusion though if the FCOS metadata types are defined in separate files while every other struct used in plume is inside of this file though. Is this maybe more of an issue with the individual names of these structs rather than the location?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine either ways (and also keeping as-is). It was just a minor sentiment when scanning through the source.

Architectures map[string]Architecture `json:"architectures"`
}

type Architecture struct {
Commit string `json:"commit"`
}

type Commit struct {
Architecture string `json:"architecture"`
Checksum string `json:"checksum"`
}