From 60910eede18d394ee2a47644aa8e08daa38e42e3 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 9 Jan 2021 15:58:20 +0000 Subject: [PATCH] plume: add cosa2stream Part of implementing https://github.com/openshift/os/issues/477 I don't think for RHCOS (at least initially) we will go through the whole "release.json" middle ground, so let's add a command to directly turn 1 or more cosa build(s) into a stream JSON. Because the "cosa build" -> "release" bits are in Python, we semi-hackily fork it off as a subprocess. But, it works. --- mantle/cmd/plume/cosa2stream.go | 88 +++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 mantle/cmd/plume/cosa2stream.go diff --git a/mantle/cmd/plume/cosa2stream.go b/mantle/cmd/plume/cosa2stream.go new file mode 100644 index 0000000000..66e1a4487d --- /dev/null +++ b/mantle/cmd/plume/cosa2stream.go @@ -0,0 +1,88 @@ +// Copyright Red Hat, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "os/exec" + "time" + + "github.com/coreos/stream-metadata-go/release" + "github.com/coreos/stream-metadata-go/stream" + "github.com/spf13/cobra" +) + +var ( + cmdCosaBuildToStream = &cobra.Command{ + Use: "cosa2stream [options]", + Short: "Generate stream JSON from a coreos-assembler build", + RunE: runCosaBuildToStream, + + SilenceUsage: true, + } + + streamBaseURL string + streamName string +) + +func init() { + cmdCosaBuildToStream.Flags().StringVar(&streamBaseURL, "url", "", "Base URL for build") + cmdCosaBuildToStream.Flags().StringVar(&streamName, "name", "", "Stream name") + cmdCosaBuildToStream.MarkFlagRequired("name") + root.AddCommand(cmdCosaBuildToStream) +} + +func runCosaBuildToStream(cmd *cobra.Command, args []string) error { + releaseTmpf, err := ioutil.TempFile("", "release") + if err != nil { + return err + } + childArgs := []string{"generate-release-meta", "--name=" + streamName, "--output=" + releaseTmpf.Name()} + if streamBaseURL != "" { + childArgs = append(childArgs, "--stream-baseurl="+streamBaseURL) + } + childArgs = append(childArgs, args...) + c := exec.Command("cosa", childArgs...) + c.Stderr = os.Stderr + if err := c.Run(); err != nil { + return err + } + + var rel release.Release + buf, err := ioutil.ReadAll(releaseTmpf) + if err != nil { + return err + } + if err := json.Unmarshal(buf, &rel); err != nil { + return err + } + + // Generate output stream from release + outStream := stream.Stream{ + Stream: streamName, + Metadata: stream.Metadata{LastModified: time.Now().UTC().Format(time.RFC3339)}, + Architectures: rel.ToStreamArchitectures(), + } + + // Serialize to JSON + encoder := json.NewEncoder(os.Stdout) + if err := encoder.Encode(&outStream); err != nil { + return fmt.Errorf("Error while encoding: %v", err) + } + return nil +}