2021-11-04 23:08:45 +01:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
2021-11-07 11:15:32 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2023-11-19 22:37:04 +01:00
|
|
|
"tailscale.com/types/key"
|
2021-11-04 23:08:45 +01:00
|
|
|
)
|
|
|
|
|
2021-11-27 19:59:39 +01:00
|
|
|
const (
|
2022-11-05 09:07:22 +01:00
|
|
|
errPreAuthKeyMalformed = Error("key is malformed. expected 64 hex characters with `nodekey` prefix")
|
2021-11-27 19:59:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
|
|
|
|
type Error string
|
|
|
|
|
|
|
|
func (e Error) Error() string { return string(e) }
|
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(debugCmd)
|
|
|
|
|
|
|
|
createNodeCmd.Flags().StringP("name", "", "", "Name")
|
|
|
|
err := createNodeCmd.MarkFlagRequired("name")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("")
|
|
|
|
}
|
2023-01-26 08:41:21 +01:00
|
|
|
createNodeCmd.Flags().StringP("user", "u", "", "User")
|
|
|
|
|
|
|
|
createNodeCmd.Flags().StringP("namespace", "n", "", "User")
|
|
|
|
createNodeNamespaceFlag := createNodeCmd.Flags().Lookup("namespace")
|
|
|
|
createNodeNamespaceFlag.Deprecated = deprecateNamespaceMessage
|
|
|
|
createNodeNamespaceFlag.Hidden = true
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = createNodeCmd.MarkFlagRequired("user")
|
2021-11-04 23:08:45 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("")
|
|
|
|
}
|
|
|
|
createNodeCmd.Flags().StringP("key", "k", "", "Key")
|
|
|
|
err = createNodeCmd.MarkFlagRequired("key")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("")
|
|
|
|
}
|
2021-11-13 09:36:45 +01:00
|
|
|
createNodeCmd.Flags().
|
|
|
|
StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to advertise")
|
2021-11-04 23:08:45 +01:00
|
|
|
|
|
|
|
debugCmd.AddCommand(createNodeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var debugCmd = &cobra.Command{
|
|
|
|
Use: "debug",
|
|
|
|
Short: "debug and testing commands",
|
|
|
|
Long: "debug contains extra commands used for debugging and testing headscale",
|
|
|
|
}
|
|
|
|
|
|
|
|
var createNodeCmd = &cobra.Command{
|
|
|
|
Use: "create-node",
|
2023-09-24 13:42:05 +02:00
|
|
|
Short: "Create a node that can be registered with `nodes register <>` command",
|
2021-11-04 23:08:45 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user, err := cmd.Flags().GetString("user")
|
2021-11-04 23:08:45 +01:00
|
|
|
if err != nil {
|
2023-01-17 17:43:44 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting user: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:08:45 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
name, err := cmd.Flags().GetString("name")
|
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting node from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
machineKey, err := cmd.Flags().GetString("key")
|
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting key from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
return
|
|
|
|
}
|
2023-11-19 22:37:04 +01:00
|
|
|
|
|
|
|
var mkey key.MachinePublic
|
|
|
|
err = mkey.UnmarshalText([]byte(machineKey))
|
|
|
|
if err != nil {
|
2021-11-26 20:49:51 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2023-11-19 22:37:04 +01:00
|
|
|
fmt.Sprintf("Failed to parse machine key from flag: %s", err),
|
2021-11-26 20:49:51 +01:00
|
|
|
output,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:08:45 +01:00
|
|
|
|
|
|
|
routes, err := cmd.Flags().GetStringSlice("route")
|
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting routes from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
request := &v1.DebugCreateNodeRequest{
|
2023-01-17 20:36:46 +01:00
|
|
|
Key: machineKey,
|
|
|
|
Name: name,
|
|
|
|
User: user,
|
|
|
|
Routes: routes,
|
2021-11-04 23:08:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
response, err := client.DebugCreateNode(ctx, request)
|
2021-11-04 23:08:45 +01:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("Cannot create node: %s", status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:08:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
SuccessOutput(response.Node, "Node created", output)
|
2021-11-04 23:08:45 +01:00
|
|
|
},
|
|
|
|
}
|