diff --git a/docs/cherryctl_server_reinstall.md b/docs/cherryctl_server_reinstall.md index a275ed3..3f2fcd8 100644 --- a/docs/cherryctl_server_reinstall.md +++ b/docs/cherryctl_server_reinstall.md @@ -7,7 +7,7 @@ Reinstall a server. Reinstall the specified server. ``` -cherryctl server reinstall ID --hostname --image --password [--ssh-keys ] [--os-partition-size ] [flags] +cherryctl server reinstall ID --hostname --image --password [--ssh-keys ] [--os-partition-size ] [--userdata-file ] [flags] ``` ### Examples @@ -26,6 +26,7 @@ cherryctl server reinstall ID --hostname --image --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 diff --git a/internal/servers/reinstall.go b/internal/servers/reinstall.go index 18852e2..df2a74c 100644 --- a/internal/servers/reinstall.go +++ b/internal/servers/reinstall.go @@ -1,7 +1,9 @@ package servers import ( + "encoding/base64" "fmt" + "os" "strconv" "github.com/cherryservers/cherrygo/v3" @@ -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 --image --password [--ssh-keys ] [--os-partition-size ]`, + Use: `reinstall ID --hostname --image --password [--ssh-keys ] [--os-partition-size ] [--userdata-file ]`, Args: cobra.ExactArgs(1), Short: "Reinstall a server.", Long: "Reinstall the specified server.", @@ -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, } @@ -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")