|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/coreos/stream-metadata-go/stream" |
| 12 | + "github.com/coreos/stream-metadata-go/stream/rhcos" |
| 13 | + |
| 14 | + "github.com/openshift/installer/pkg/rhcos/marketplace/azure" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + streamRHCOSJSON = "data/data/coreos/rhcos.json" |
| 19 | + streamMarketplaceRHCOSJSON = "data/data/coreos/marketplace-rhcos.json" |
| 20 | + |
| 21 | + x86 = "x86_64" |
| 22 | + arm64 = "aarch64" |
| 23 | +) |
| 24 | + |
| 25 | +// arch -> marketplace |
| 26 | +type marketplaceStream map[string]*rhcos.Marketplace |
| 27 | + |
| 28 | +func main() { |
| 29 | + ctx := context.Background() |
| 30 | + stream := marketplaceStream{} |
| 31 | + |
| 32 | + if err := stream.populate(ctx); err != nil { |
| 33 | + log.Fatalln("Failed to populate marketplace stream:", err) |
| 34 | + } |
| 35 | + |
| 36 | + if err := stream.write(); err != nil { |
| 37 | + log.Fatalln("Failed to write marketplace stream:", err) |
| 38 | + } |
| 39 | + log.Printf("Successfully wrote marketplace stream to %s", streamMarketplaceRHCOSJSON) |
| 40 | +} |
| 41 | + |
| 42 | +// populate gathers the marketplace images for each cloud |
| 43 | +// and adds them to the marketplace stream data structure. |
| 44 | +func (s marketplaceStream) populate(ctx context.Context) error { |
| 45 | + clouds := []func(ctx context.Context, arch string) error{ |
| 46 | + s.azure, |
| 47 | + } |
| 48 | + |
| 49 | + for _, supportedArch := range []string{arm64, x86} { |
| 50 | + s[supportedArch] = &rhcos.Marketplace{} |
| 51 | + for _, populateCloud := range clouds { |
| 52 | + if err := populateCloud(ctx, supportedArch); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// write serializes the marketplace stream to disk. |
| 61 | +func (s marketplaceStream) write() error { |
| 62 | + contents, err := json.MarshalIndent(s, "", " ") |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("error marshaling stream: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // TODO(padillon): dumb question time, git is still complaining \ No newline at end of file |
| 68 | + // what am I doing wrong? |
| 69 | + contents = append(contents, []byte("\n")...) |
| 70 | + |
| 71 | + if err := os.WriteFile(streamMarketplaceRHCOSJSON, contents, 0644); err != nil { |
| 72 | + return fmt.Errorf("error writing stream: %w", err) |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func (s marketplaceStream) azure(ctx context.Context, arch string) error { |
| 78 | + var err error |
| 79 | + |
| 80 | + s[arch].Azure = &rhcos.AzureMarketplace{} |
| 81 | + |
| 82 | + rel, err := getReleaseFromStream() |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to get release from rhcos stream: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + azClient, err := azure.NewStreamClient() |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to initialize azure client: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + if s[arch].Azure, err = azClient.Populate(ctx, arch, rel); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// getXYFromStream obtains the X.Y version from rhcos.json. |
| 100 | +func getReleaseFromStream() (string, error) { |
| 101 | + if rel, ok := os.LookupEnv("STREAM_RELEASE_OVERRIDE"); ok { |
| 102 | + log.Printf("Found STREAM_RELEASE_OVERRIDE %s", rel) |
| 103 | + return rel, nil |
| 104 | + } |
| 105 | + fileContents, err := os.ReadFile(streamRHCOSJSON) |
| 106 | + if err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + |
| 110 | + st := &stream.Stream{} |
| 111 | + if err := json.Unmarshal(fileContents, st); err != nil { |
| 112 | + return "", fmt.Errorf("failed to unmarshal RHCOS stream: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + return strings.Split(st.Stream, "-")[1], nil |
| 116 | +} |
0 commit comments