From 5cad630959da63f13018c2f78f64c1df0bb9a465 Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Tue, 3 Dec 2024 10:26:55 +0100 Subject: [PATCH] cmd/ore: add akamai create-image Signed-off-by: Mathieu Tortuyaux --- cmd/ore/akamai/akamai.go | 49 ++++++++++++++++++++++++++++++++++ cmd/ore/akamai/create-image.go | 43 +++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 cmd/ore/akamai/akamai.go create mode 100644 cmd/ore/akamai/create-image.go diff --git a/cmd/ore/akamai/akamai.go b/cmd/ore/akamai/akamai.go new file mode 100644 index 000000000..c29438b1c --- /dev/null +++ b/cmd/ore/akamai/akamai.go @@ -0,0 +1,49 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 + +package akamai + +import ( + "fmt" + "os" + + "github.com/coreos/pkg/capnslog" + "github.com/flatcar/mantle/cli" + "github.com/flatcar/mantle/platform" + "github.com/flatcar/mantle/platform/api/akamai" + "github.com/spf13/cobra" +) + +var ( + plog = capnslog.NewPackageLogger("github.com/flatcar/mantle", "ore/akamai") + + Akamai = &cobra.Command{ + Use: "akamai [command]", + Short: "akamai image utilities", + } + + API *akamai.API + region string + token string +) + +func init() { + cli.WrapPreRun(Akamai, preflightCheck) + Akamai.PersistentFlags().StringVar(®ion, "akamai-region", "us-ord", "Akamai region") + Akamai.PersistentFlags().StringVar(&token, "akamai-token", "", "Akamai access token") +} + +func preflightCheck(cmd *cobra.Command, args []string) error { + api, err := akamai.New(&akamai.Options{ + Region: region, + Token: token, + Options: &platform.Options{}, + }) + if err != nil { + fmt.Fprintf(os.Stderr, "could not create Akamai API client: %v\n", err) + os.Exit(1) + } + + API = api + return nil +} diff --git a/cmd/ore/akamai/create-image.go b/cmd/ore/akamai/create-image.go new file mode 100644 index 000000000..27a59d79b --- /dev/null +++ b/cmd/ore/akamai/create-image.go @@ -0,0 +1,43 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 + +package akamai + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" +) + +var ( + cmdCreate = &cobra.Command{ + Use: "create-image", + Short: "Create Akamai image", + RunE: runCreate, + Example: `IMAGE_ID=$(ore akamai \ + --akamai-token "${AKAMAI_TOKEN}" \ + --akamai-region "${AKAMAI_REGION}" \ + create-image --name my-image --file /path/to/flatcar_production_akamai_image.bin.gz)`, + } + file string + name string +) + +func init() { + Akamai.AddCommand(cmdCreate) + + cmdCreate.Flags().StringVar(&file, "file", "flatcar_production_akamai_image.bin.gz", "path to local Flatcar image (.bin.gz)") + cmdCreate.Flags().StringVar(&name, "name", "flatcar-kola-test", "name of the image") +} + +func runCreate(cmd *cobra.Command, args []string) error { + ID, err := API.UploadImage(context.Background(), name, file) + if err != nil { + return fmt.Errorf("creating Flatcar image: %v", err) + } + + fmt.Println(ID) + + return nil +}