Skip to content

Commit

Permalink
ore/brightbox: add new subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Tortuyaux <mtortuyaux@microsoft.com>
  • Loading branch information
tormath1 committed Nov 15, 2023
1 parent c7aab09 commit 955a0f7
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/ore/brightbox.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions cmd/ore/brightbox/brightbox.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions cmd/ore/brightbox/create.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions cmd/ore/brightbox/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions cmd/ore/brightbox/gc.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 955a0f7

Please sign in to comment.