diff --git a/cmd/ore/brightbox.go b/cmd/ore/brightbox.go new file mode 100644 index 000000000..9ba56a90c --- /dev/null +++ b/cmd/ore/brightbox.go @@ -0,0 +1,11 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package main + +import ( + "github.com/flatcar/mantle/cmd/ore/brightbox" +) + +func init() { + root.AddCommand(brightbox.Brightbox) +} diff --git a/cmd/ore/brightbox/brightbox.go b/cmd/ore/brightbox/brightbox.go new file mode 100644 index 000000000..349033a5d --- /dev/null +++ b/cmd/ore/brightbox/brightbox.go @@ -0,0 +1,41 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package brightbox + +import ( + "fmt" + + "github.com/coreos/pkg/capnslog" + "github.com/spf13/cobra" + + "github.com/flatcar/mantle/cli" + "github.com/flatcar/mantle/platform/api/brightbox" +) + +var ( + plog = capnslog.NewPackageLogger("github.com/flatcar/mantle", "ore/brightbox") + + Brightbox = &cobra.Command{ + Use: "brightbox [command]", + Short: "Brightbox machine utilities", + } + + API *brightbox.API + options brightbox.Options +) + +func init() { + Brightbox.PersistentFlags().StringVar(&options.ClientID, "brightbox-client-id", "", "Brightbox client ID") + Brightbox.PersistentFlags().StringVar(&options.ClientSecret, "brightbox-client-secret", "", "Brightbox client secret") + cli.WrapPreRun(Brightbox, preflightCheck) +} + +func preflightCheck(cmd *cobra.Command, args []string) error { + api, err := brightbox.New(&options) + if err != nil { + return fmt.Errorf("creating Brightbox client: %w", err) + } + + API = api + return nil +} diff --git a/cmd/ore/brightbox/create.go b/cmd/ore/brightbox/create.go new file mode 100644 index 000000000..059012460 --- /dev/null +++ b/cmd/ore/brightbox/create.go @@ -0,0 +1,41 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package brightbox + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + cmdCreate = &cobra.Command{ + Use: "create-image", + Short: "Create image on Brightbox", + Long: `Upload an image to Brigthbox. + +After a successful run, the final line of output will be the ID of the image. +`, + RunE: runCreate, + } + + url, name string +) + +func init() { + Brightbox.AddCommand(cmdCreate) + cmdCreate.Flags().StringVar(&url, "url", + "https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_openstack_image.img", + "Flatcar image URL") + cmdCreate.Flags().StringVar(&name, "name", "", "image name") +} + +func runCreate(cmd *cobra.Command, args []string) error { + id, err := API.UploadImage(name, url) + if err != nil { + return fmt.Errorf("creating an image: %w", err) + } + + fmt.Println(id) + return nil +} diff --git a/cmd/ore/brightbox/delete.go b/cmd/ore/brightbox/delete.go new file mode 100644 index 000000000..324b7949d --- /dev/null +++ b/cmd/ore/brightbox/delete.go @@ -0,0 +1,33 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package brightbox + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + cmdDelete = &cobra.Command{ + Use: "delete-image", + Short: "Delete image on Brightbox", + Long: `Delete an image from Brightbox.`, + RunE: runDelete, + } + + id string +) + +func init() { + Brightbox.AddCommand(cmdDelete) + cmdDelete.Flags().StringVar(&id, "id", "", "image ID") +} + +func runDelete(cmd *cobra.Command, args []string) error { + if err := API.DeleteImage(id); err != nil { + return fmt.Errorf("deleting image: %w", err) + } + + return nil +} diff --git a/cmd/ore/brightbox/gc.go b/cmd/ore/brightbox/gc.go new file mode 100644 index 000000000..3ab98e345 --- /dev/null +++ b/cmd/ore/brightbox/gc.go @@ -0,0 +1,34 @@ +// Copyright The Mantle Authors. +// SPDX-License-Identifier: Apache-2.0 +package brightbox + +import ( + "fmt" + "time" + + "github.com/spf13/cobra" +) + +var ( + cmdGC = &cobra.Command{ + Use: "gc", + Short: "GC resources in Brightbox", + Long: `Delete instances created over the given duration ago`, + RunE: runGC, + } + + gcDuration time.Duration +) + +func init() { + Brightbox.AddCommand(cmdGC) + cmdGC.Flags().DurationVar(&gcDuration, "duration", 5*time.Hour, "how old resources must be before they're considered garbage") +} + +func runGC(cmd *cobra.Command, args []string) error { + if err := API.GC(gcDuration); err != nil { + return fmt.Errorf("running garbage collection: %w", err) + } + + return nil +}