Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] UserData on reinstall #36

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/cherryctl_server_reinstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Reinstall a server.
Reinstall the specified server.

```
cherryctl server reinstall ID --hostname <hostname> --image <image_slug> --password <password> [--ssh-keys <ssh_key_ids>] [--os-partition-size <size>] [flags]
cherryctl server reinstall ID --hostname <hostname> --image <image_slug> --password <password> [--ssh-keys <ssh_key_ids>] [--os-partition-size <size>] [--userdata-file <filepath>] [flags]
```

### Examples
Expand All @@ -26,6 +26,7 @@ cherryctl server reinstall ID --hostname <hostname> --image <image_slug> --passw
--os-partition-size int OS partition size in GB.
--password string Server password.
--ssh-keys strings Comma separated list of SSH key IDs to be embed in the Server.
--userdata-file string Path to a userdata file for server initialization.
```

### Options inherited from parent commands
Expand Down
16 changes: 15 additions & 1 deletion internal/servers/reinstall.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package servers

import (
"encoding/base64"
"fmt"
"os"
"strconv"

"github.com/cherryservers/cherrygo/v3"
Expand All @@ -15,11 +17,13 @@ func (c *Client) Reinstall() *cobra.Command {
image string
password string
sshKeys []string
userDataFile string
userdata string
osPartitionSize int
)

reinstallServerCmd := &cobra.Command{
Use: `reinstall ID --hostname <hostname> --image <image_slug> --password <password> [--ssh-keys <ssh_key_ids>] [--os-partition-size <size>]`,
Use: `reinstall ID --hostname <hostname> --image <image_slug> --password <password> [--ssh-keys <ssh_key_ids>] [--os-partition-size <size>] [--userdata-file <filepath>]`,
Args: cobra.ExactArgs(1),
Short: "Reinstall a server.",
Long: "Reinstall the specified server.",
Expand All @@ -29,11 +33,20 @@ func (c *Client) Reinstall() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if userDataFile != "" {
userdataRaw, readErr := os.ReadFile(userDataFile)
if readErr != nil {
return errors.Wrap(readErr, "Could not read userdata-file")
}
userdata = base64.StdEncoding.EncodeToString(userdataRaw)
}

request := &cherrygo.ReinstallServerFields{
Image: image,
Hostname: hostname,
Password: password,
SSHKeys: sshKeys,
UserData: userdata,
OSPartitionSize: osPartitionSize,
}

Expand All @@ -57,6 +70,7 @@ func (c *Client) Reinstall() *cobra.Command {
reinstallServerCmd.Flags().StringVarP(&password, "password", "", "", "Server password.")
reinstallServerCmd.Flags().StringSliceVarP(&sshKeys, "ssh-keys", "", []string{}, "Comma separated list of SSH key IDs to be embed in the Server.")
reinstallServerCmd.Flags().IntVarP(&osPartitionSize, "os-partition-size", "", 0, "OS partition size in GB.")
reinstallServerCmd.Flags().StringVarP(&userDataFile, "userdata-file", "", "", "Path to a userdata file for server initialization.")

_ = reinstallServerCmd.MarkFlagRequired("hostname")
_ = reinstallServerCmd.MarkFlagRequired("image")
Expand Down
Loading