MakeNodeToken creates the node-token command
()
| 15 | |
| 16 | // MakeNodeToken creates the node-token command |
| 17 | func MakeNodeToken() *cobra.Command { |
| 18 | var command = &cobra.Command{ |
| 19 | Use: "node-token", |
| 20 | Short: "Retrieve the node token from a server", |
| 21 | Long: `Retrieve the node token from a server required for a |
| 22 | server or agent to join the cluster. |
| 23 | |
| 24 | ` + pkg.SupportMessageShort + ` |
| 25 | `, |
| 26 | Example: ` # Get the node token from the server and pipe it to a file |
| 27 | k3sup node-token --ip IP --user USER > token.txt |
| 28 | `, |
| 29 | SilenceUsage: true, |
| 30 | } |
| 31 | |
| 32 | command.Flags().IP("ip", net.ParseIP("127.0.0.1"), "Public IP of node") |
| 33 | command.Flags().String("user", "root", "Username for SSH login") |
| 34 | |
| 35 | command.Flags().String("host", "", "Public hostname of node on which to install agent") |
| 36 | |
| 37 | command.Flags().Bool("local", false, "Use local machine instead of ssh client") |
| 38 | command.Flags().String("ssh-key", "~/.ssh/id_rsa", "The ssh key to use for remote login") |
| 39 | command.Flags().Int("ssh-port", 22, "The port on which to connect for ssh") |
| 40 | command.Flags().Bool("sudo", true, "Use sudo for installation. e.g. set to false when using the root user and no sudo is available.") |
| 41 | |
| 42 | command.Flags().Bool("print-command", false, "Print the command to be executed") |
| 43 | command.Flags().String("server-data-dir", "/var/lib/rancher/k3s/", "Override the path used to fetch the node-token from the server") |
| 44 | |
| 45 | command.PreRunE = func(command *cobra.Command, args []string) error { |
| 46 | local, err := command.Flags().GetBool("local") |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | if !local { |
| 52 | _, err = command.Flags().GetString("host") |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | if _, err := command.Flags().GetIP("ip"); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | if _, err := command.Flags().GetInt("ssh-port"); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | } |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | command.RunE = func(command *cobra.Command, args []string) error { |
| 69 | |
| 70 | fmt.Fprintf(os.Stderr, "Fetching: /etc/rancher/k3s/k3s.yaml\n") |
| 71 | |
| 72 | useSudo, err := command.Flags().GetBool("sudo") |
| 73 | if err != nil { |
| 74 | return err |
no test coverage detected