2023-05-23 09:09:14 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2024-01-09 13:37:56 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-05-23 09:09:14 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
infoupload "github.com/edgelesssys/constellation/v2/internal/osimage/imageinfo"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewInfoCmd creates a new info parent command.
|
|
|
|
func NewInfoCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "info [flags] <image-info.json>...",
|
|
|
|
Short: "Uploads OS image info to S3",
|
|
|
|
Long: "Uploads OS image info to S3.",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: runInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SetOut(os.Stdout)
|
|
|
|
|
|
|
|
cmd.Flags().String("region", "eu-central-1", "AWS region of the archive S3 bucket")
|
|
|
|
cmd.Flags().String("bucket", "cdn-constellation-backend", "S3 bucket name of the archive")
|
2024-01-09 13:37:56 -05:00
|
|
|
cmd.Flags().String("distribution-id", constants.CDNDefaultDistributionID, "CloudFront distribution ID of the API")
|
2023-05-23 09:09:14 -04:00
|
|
|
cmd.Flags().Bool("verbose", false, "Enable verbose output")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInfo(cmd *cobra.Command, args []string) error {
|
|
|
|
workdir := os.Getenv("BUILD_WORKING_DIRECTORY")
|
|
|
|
if len(workdir) > 0 {
|
|
|
|
must(os.Chdir(workdir))
|
|
|
|
}
|
|
|
|
|
|
|
|
flags, err := parseS3Flags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log := logger.NewTextLogger(flags.logLevel)
|
2024-04-03 09:49:03 -04:00
|
|
|
log.Debug("Using flags", "region", flags.region, "bucket", flags.bucket, "distributionID", flags.distributionID)
|
2023-05-23 09:09:14 -04:00
|
|
|
info, err := readInfoArgs(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-02 05:20:01 -04:00
|
|
|
uploadC, uploadCClose, err := infoupload.New(cmd.Context(), flags.region, flags.bucket, flags.distributionID, log)
|
2023-05-23 09:09:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("uploading image info: %w", err)
|
|
|
|
}
|
2023-06-02 05:20:01 -04:00
|
|
|
defer func() {
|
|
|
|
if err := uploadCClose(cmd.Context()); err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error(fmt.Sprintf("closing upload client: %v", err))
|
2023-06-02 05:20:01 -04:00
|
|
|
}
|
|
|
|
}()
|
2023-05-23 09:09:14 -04:00
|
|
|
|
|
|
|
url, err := uploadC.Upload(cmd.Context(), info)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("uploading image info: %w", err)
|
|
|
|
}
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Info(fmt.Sprintf("Uploaded image info to %s", url))
|
2023-05-23 09:09:14 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readInfoArgs(paths []string) (versionsapi.ImageInfo, error) {
|
|
|
|
infos := make([]versionsapi.ImageInfo, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return versionsapi.ImageInfo{}, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := json.NewDecoder(f).Decode(&infos[i]); err != nil {
|
|
|
|
return versionsapi.ImageInfo{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return versionsapi.MergeImageInfos(infos...)
|
|
|
|
}
|