From 4226da3d6b65291ea433272a4b9769ee782fe2b5 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 4 Nov 2021 22:08:45 +0000 Subject: [PATCH] Add "debug" command This commit adds a debug command tree, intended to host commands used for debugging and testing. It adds a create node/machine command which will be used later to create machines that can be used to test the registration command. --- cmd/headscale/cli/debug.go | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 cmd/headscale/cli/debug.go diff --git a/cmd/headscale/cli/debug.go b/cmd/headscale/cli/debug.go new file mode 100644 index 00000000..8dfe0c41 --- /dev/null +++ b/cmd/headscale/cli/debug.go @@ -0,0 +1,93 @@ +package cli + +import ( + "context" + "fmt" + "time" + + v1 "github.com/juanfont/headscale/gen/go/headscale/v1" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(debugCmd) + + createNodeCmd.Flags().StringP("name", "", "", "Name") + err := createNodeCmd.MarkFlagRequired("name") + if err != nil { + log.Fatal().Err(err).Msg("") + } + createNodeCmd.Flags().StringP("namespace", "n", "", "Namespace") + err = createNodeCmd.MarkFlagRequired("namespace") + 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("") + } + createNodeCmd.Flags().StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to advertise") + + 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", + Short: "Create a node (machine) that can be registered with `nodes register <>` command", + Run: func(cmd *cobra.Command, args []string) { + output, _ := cmd.Flags().GetString("output") + + namespace, err := cmd.Flags().GetString("namespace") + if err != nil { + ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output) + return + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + client, conn := getHeadscaleGRPCClient(ctx) + defer conn.Close() + + name, err := cmd.Flags().GetString("name") + if err != nil { + ErrorOutput(err, fmt.Sprintf("Error getting node from flag: %s", err), output) + return + } + + machineKey, err := cmd.Flags().GetString("key") + if err != nil { + ErrorOutput(err, fmt.Sprintf("Error getting key from flag: %s", err), output) + return + } + + routes, err := cmd.Flags().GetStringSlice("route") + if err != nil { + ErrorOutput(err, fmt.Sprintf("Error getting routes from flag: %s", err), output) + return + } + + request := &v1.DebugCreateMachineRequest{ + Key: machineKey, + Name: name, + Namespace: namespace, + Routes: routes, + } + + response, err := client.DebugCreateMachine(ctx, request) + if err != nil { + ErrorOutput(err, fmt.Sprintf("Cannot create machine: %s", err), output) + return + } + + SuccessOutput(response.Machine, "Machine created", output) + }, +}