cosa/build: add more fields and helper functions
Prep for more code that will leverage those.
package cosa
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "os"
+
+ "github.com/pkg/errors"
+)
+
// Build is the coreos-assembler `meta.json` which defines a build.
// This code was copied from openshift-installer's
// https://github.com/openshift/installer/blob/a0350404997b0493d7bb16aa2875e5c42879b069/pkg/rhcos/builds.go
@@ -21,6 +31,7 @@ package cosa
type Build struct {
Ref string `json:"ref"`
OSTreeVersion string `json:"ostree-version"`
+ OSTreeCommit string `json:"ostree-commit"`
AMIs []struct {
Region string `json:"name"`
HVM string `json:"hvm"`
@@ -35,6 +46,10 @@ type Build struct {
}
BaseURI string `json:"baseURI"`
Images struct {
+ OSTree struct {
+ Path string `json:"path"`
+ SHA256 string `json:"sha256"`
+ } `json:"ostree"`
QEMU struct {
Path string `json:"path"`
SHA256 string `json:"sha256"`
@@ -48,3 +63,50 @@ type Build struct {
} `json:"images"`
FedoraCoreOSParentVersion string `json:"fedora-coreos.parent-version"`
}
+
+func ParseBuild(path string) (*Build, error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to open %s", path)
+ }
+ defer f.Close()
+
+ dec := json.NewDecoder(f)
+ // FIXME enable this to prevent schema regressions https://github.com/coreos/coreos-assembler/pull/1059
+ // dec.DisallowUnknownFields()
+ var cosaBuild *Build
+ if err := dec.Decode(&cosaBuild); err != nil {
+ return nil, errors.Wrapf(err, "failed to parse %s", path)
+ }
+
+ return cosaBuild, nil
+}
+
+func FetchAndParseBuild(url string) (*Build, error) {
+ res, err := http.Get(url)
+ if err != nil {
+ return nil, err
+ }
+
+ body, err := ioutil.ReadAll(res.Body)
+ res.Body.Close()
+ if err != nil {
+ return nil, err
+ }
+
+ var cosaBuild *Build
+ if err = json.Unmarshal(body, &cosaBuild); err != nil {
+ return nil, err
+ }
+
+ return cosaBuild, nil
+}
+
+func (build *Build) FindAMI(region string) (string, error) {
+ for _, ami := range build.AMIs {
+ if ami.Region == region {
+ return ami.HVM, nil
+ }
+ }
+ return "", fmt.Errorf("no AMI found for region %s", region)
+}
