Skip to content

Commit

Permalink
feat: set server type explicitly (#36)
Browse files Browse the repository at this point in the history
- **CLI**: New flag `--server-type` that overrides the `--architecture`
flag and allows users to specify the server type they want
- **Lib**: New field in `UploadOptions`: `ServerType *hcloud.ServerType`
that overrides the `Architecture` field and allows users to specify the
server type they want

Closes #30
  • Loading branch information
apricote authored Jun 23, 2024
1 parent f244acd commit 42eeb00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
16 changes: 14 additions & 2 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
uploadFlagImagePath = "image-path"
uploadFlagCompression = "compression"
uploadFlagArchitecture = "architecture"
uploadFlagServerType = "server-type"
uploadFlagDescription = "description"
uploadFlagLabels = "labels"
)
Expand All @@ -43,12 +44,12 @@ This does cost a bit of money for the server.`,
imagePathString, _ := cmd.Flags().GetString(uploadFlagImagePath)
imageCompression, _ := cmd.Flags().GetString(uploadFlagCompression)
architecture, _ := cmd.Flags().GetString(uploadFlagArchitecture)
serverType, _ := cmd.Flags().GetString(uploadFlagServerType)
description, _ := cmd.Flags().GetString(uploadFlagDescription)
labels, _ := cmd.Flags().GetStringToString(uploadFlagLabels)

options := hcloudimages.UploadOptions{
ImageCompression: hcloudimages.Compression(imageCompression),
Architecture: hcloud.Architecture(architecture),
Description: hcloud.Ptr(description),
Labels: labels,
}
Expand All @@ -69,6 +70,12 @@ This does cost a bit of money for the server.`,
options.ImageReader = imageFile
}

if architecture != "" {
options.Architecture = hcloud.Architecture(architecture)
} else if serverType != "" {
options.ServerType = &hcloud.ServerType{Name: serverType}
}

image, err := client.Upload(ctx, options)
if err != nil {
return fmt.Errorf("failed to upload the image: %w", err)
Expand Down Expand Up @@ -99,7 +106,12 @@ func init() {
uploadFlagArchitecture,
cobra.FixedCompletions([]string{string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)}, cobra.ShellCompDirectiveNoFileComp),
)
_ = uploadCmd.MarkFlagRequired(uploadFlagArchitecture)

uploadCmd.Flags().String(uploadFlagServerType, "", "Explicitly use this server type to generate the image. Mutually exclusive with --architecture.")

// Only one of them needs to be set
uploadCmd.MarkFlagsOneRequired(uploadFlagArchitecture, uploadFlagServerType)
uploadCmd.MarkFlagsMutuallyExclusive(uploadFlagArchitecture, uploadFlagServerType)

uploadCmd.Flags().String(uploadFlagDescription, "", "Description for the resulting image")

Expand Down
21 changes: 18 additions & 3 deletions hcloudimages/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ type UploadOptions struct {
// used with [hcloud.ArchitectureX86] or [hcloud.ArchitectureARM] servers.
//
// Internally this decides what server type is used for the temporary server.
//
// Optional if [UploadOptions.ServerType] is set.
Architecture hcloud.Architecture

// ServerType can be optionally set to override the default server type for the architecture.
// Situations where this makes sense:
//
// - Your image is larger than the root disk of the default server types.
// - The default server type is no longer available, or not temporarily out of stock.
ServerType *hcloud.ServerType

// Description is an optional description that the resulting image (snapshot) will have. There is no way to
// select images by its description, you should use Labels if you need to identify your image later.
Description *string
Expand Down Expand Up @@ -159,9 +168,15 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima

// 2. Create Server
logger.InfoContext(ctx, "# Step 2: Creating Server")
serverType, ok := serverTypePerArchitecture[options.Architecture]
if !ok {
return nil, fmt.Errorf("unknown architecture %q, valid options: %q, %q", options.Architecture, hcloud.ArchitectureX86, hcloud.ArchitectureARM)
var serverType *hcloud.ServerType
if options.ServerType != nil {
serverType = options.ServerType
} else {
var ok bool
serverType, ok = serverTypePerArchitecture[options.Architecture]
if !ok {
return nil, fmt.Errorf("unknown architecture %q, valid options: %q, %q", options.Architecture, hcloud.ArchitectureX86, hcloud.ArchitectureARM)
}
}

logger.DebugContext(ctx, "creating server with config",
Expand Down

0 comments on commit 42eeb00

Please sign in to comment.