Skip to content

Commit

Permalink
ore/hetzner: add garbage collection
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Tortuyaux <mtortuyaux@microsoft.com>
  • Loading branch information
tormath1 committed Jun 10, 2024
1 parent e27a6b3 commit 6d377e6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/ore/hetzner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright The Mantle Authors.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"github.com/flatcar/mantle/cmd/ore/hetzner"
)

func init() {
root.AddCommand(hetzner.Hetzner)
}
36 changes: 36 additions & 0 deletions cmd/ore/hetzner/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The Mantle Authors.
// SPDX-License-Identifier: Apache-2.0

package hetzner

import (
"context"
"fmt"
"time"

"github.com/spf13/cobra"
)

var (
cmdGC = &cobra.Command{
Use: "gc",
Short: "GC resources in Hetzner",
Long: `Delete instances and images created over the given duration ago`,
RunE: runGC,
}

gcDuration time.Duration
)

func init() {
Hetzner.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(context.Background(), gcDuration); err != nil {
return fmt.Errorf("running garbage collection: %w", err)
}

return nil
}
42 changes: 42 additions & 0 deletions cmd/ore/hetzner/hetzner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The Mantle Authors.
// SPDX-License-Identifier: Apache-2.0
package hetzner

import (
"fmt"
"os"

"github.com/coreos/pkg/capnslog"
"github.com/flatcar/mantle/cli"
"github.com/flatcar/mantle/platform/api/hetzner"
"github.com/spf13/cobra"
)

var (
plog = capnslog.NewPackageLogger("github.com/flatcar/mantle", "ore/hetzner")

Hetzner = &cobra.Command{
Use: "hetzner [command]",
Short: "hetzner image utilities",
}

API *hetzner.API
options hetzner.Options
)

func init() {
cli.WrapPreRun(Hetzner, preflightCheck)
Hetzner.PersistentFlags().StringVar(&options.Token, "hetzner-token", "", "Hetzner token for client authentication")
Hetzner.PersistentFlags().StringVar(&options.Location, "hetzner-location", "", "Hetzner location name")
}

func preflightCheck(cmd *cobra.Command, args []string) error {
api, err := hetzner.New(&options)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create Hetzner API client: %v\n", err)
os.Exit(1)
}

API = api
return nil
}

0 comments on commit 6d377e6

Please sign in to comment.