Skip to content

Commit

Permalink
cmd/ore: add scaleway create-image
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Tortuyaux <[email protected]>
  • Loading branch information
tormath1 committed Feb 19, 2024
1 parent 65deb2f commit d303c0c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/ore/scaleway.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/scaleway"
)

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

package scaleway

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var (
cmdCreate = &cobra.Command{
Use: "create-image",
Short: "Create Scaleway image",
RunE: runCreate,
Example: `IMAGE_ID=$(ore scaleway \
--scaleway-access-key "${SCALEWAY_ACCESS_KEY}" \
--scaleway-secret-key "${SCALEWAY_SECRET_KEY}" \
--scaleway-organization-id "${SCALEWAY_ORGANIZATION_ID}" \
create-image --channel beta)`,
}
channel string
version string
board string
file string
)

func init() {
Scaleway.AddCommand(cmdCreate)

cmdCreate.Flags().StringVar(&channel, "channel", "stable", "Flatcar channel")
cmdCreate.Flags().StringVar(&version, "version", "current", "Flatcar version")
cmdCreate.Flags().StringVar(&board, "board", "amd64-usr", "board used for naming with default prefix and AMI architecture")
cmdCreate.Flags().StringVar(&file, "file", "flatcar_production_scaleway_image.qcow2", "path to local Flatcar image (.qcow2)")
}

func runCreate(cmd *cobra.Command, args []string) error {
bucket := fmt.Sprintf("%s-%s-%s", channel, version, board)
if err := API.InitializeBucket(bucket); err != nil {
return fmt.Errorf("creating bucket %s: %v", bucket, err)
}

f, err := os.Open(file)
if err != nil {
return fmt.Errorf("opening Flatcar image file %s: %v", file, err)
}

defer f.Close()

key := filepath.Base(file)
if err := API.UploadObject(f, bucket, key, true); err != nil {
return fmt.Errorf("uploading Flatcar image file %s: %v", file, err)
}

ID, err := API.CreateSnapshot(context.Background(), bucket, file)
if err != nil {
return fmt.Errorf("creating Flatcar image: %v", err)
}

fmt.Println(ID)

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

import (
"fmt"
"os"

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

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

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

API *scaleway.API
region string
zone string
accessKey string
secretKey string
organizationID string
projectID string
)

func init() {
cli.WrapPreRun(Scaleway, preflightCheck)
Scaleway.PersistentFlags().StringVar(&region, "scaleway-region", "fr-par", "Scaleway region")
Scaleway.PersistentFlags().StringVar(&zone, "scaleway-zone", "fr-par-1", "Scaleway region")
Scaleway.PersistentFlags().StringVar(&accessKey, "scaleway-access-key", "", "Scaleway access key")
Scaleway.PersistentFlags().StringVar(&secretKey, "scaleway-secret-key", "", "Scaleway secret key")
Scaleway.PersistentFlags().StringVar(&organizationID, "scaleway-organization-id", "", "Scaleway organization ID")
Scaleway.PersistentFlags().StringVar(&projectID, "scaleway-project-id", "", "Scaleway project ID")
}

func preflightCheck(cmd *cobra.Command, args []string) error {
api, err := scaleway.New(&scaleway.Options{
Region: region,
Zone: zone,
AccessKey: accessKey,
SecretKey: secretKey,
OrganizationID: organizationID,
ProjectID: projectID,
Options: &platform.Options{},
})
if err != nil {
fmt.Fprintf(os.Stderr, "could not create Scaleway API client: %v\n", err)
os.Exit(1)
}

API = api
return nil
}

0 comments on commit d303c0c

Please sign in to comment.