-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mathieu Tortuyaux <[email protected]>
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®ion, "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 | ||
} |