add uncloud machine token command to print machine token to add machine to cluster

This commit is contained in:
Pavel Sviderski
2024-09-05 16:13:27 +10:00
parent 4656bdd132
commit 945eff471b
9 changed files with 327 additions and 42 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ func NewInitCommand() *cobra.Command {
opts := initOptions{}
cmd := &cobra.Command{
Use: "init",
Short: "Initialise a new cluster that consists of the local or remote machine",
Short: "Initialise a new cluster that consists of the local or remote machine.",
RunE: func(cmd *cobra.Command, args []string) error {
netPrefix, err := netip.ParsePrefix(opts.network)
if err != nil {
+1
View File
@@ -12,6 +12,7 @@ func NewRootCommand() *cobra.Command {
cmd.AddCommand(
NewAddCommand(),
NewInitCommand(),
NewTokenCommand(),
)
return cmd
}
+38
View File
@@ -0,0 +1,38 @@
package machine
import (
"fmt"
"github.com/spf13/cobra"
"uncloud/internal/machine"
"uncloud/internal/machine/daemon"
)
type tokenOptions struct {
dataDir string
}
func NewTokenCommand() *cobra.Command {
opts := tokenOptions{}
cmd := &cobra.Command{
Use: "token",
Short: "Print the local machine's token for adding it to a cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
token, err := daemon.MachineToken(opts.dataDir)
if err != nil {
return fmt.Errorf("get machine token: %w", err)
}
tokenStr, err := token.String()
if err != nil {
return fmt.Errorf("encode machine token: %w", err)
}
fmt.Println(tokenStr)
return nil
},
}
cmd.Flags().StringVarP(&opts.dataDir, "data-dir", "d", machine.DefaultDataDir,
"Directory for storing persistent machine state")
_ = cmd.MarkFlagDirname("data-dir")
return cmd
}