|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/loft-sh/devpod/cmd/agent" |
| 9 | + proflags "github.com/loft-sh/devpod/cmd/pro/flags" |
| 10 | + "github.com/loft-sh/devpod/pkg/config" |
| 11 | + daemon "github.com/loft-sh/devpod/pkg/daemon/platform" |
| 12 | + providerpkg "github.com/loft-sh/devpod/pkg/provider" |
| 13 | + "github.com/loft-sh/log" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "tailscale.com/client/tailscale" |
| 16 | +) |
| 17 | + |
| 18 | +// NetcheckCmd holds the DevPod daemon flags |
| 19 | +type NetcheckCmd struct { |
| 20 | + *proflags.GlobalFlags |
| 21 | + |
| 22 | + Host string |
| 23 | + Log log.Logger |
| 24 | +} |
| 25 | + |
| 26 | +// NewNetcheckCmd creates a new command |
| 27 | +func NewNetcheckCmd(flags *proflags.GlobalFlags) *cobra.Command { |
| 28 | + cmd := &NetcheckCmd{ |
| 29 | + GlobalFlags: flags, |
| 30 | + Log: log.Default, |
| 31 | + } |
| 32 | + c := &cobra.Command{ |
| 33 | + Use: "netcheck", |
| 34 | + Short: "Get the status of the current network", |
| 35 | + RunE: func(cobraCmd *cobra.Command, args []string) error { |
| 36 | + devPodConfig, provider, err := findProProvider(cobraCmd.Context(), cmd.Context, cmd.Provider, cmd.Host, cmd.Log) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + return cmd.Run(cobraCmd.Context(), devPodConfig, provider) |
| 42 | + }, |
| 43 | + PersistentPreRun: func(cmd *cobra.Command, args []string) { |
| 44 | + root := cmd.Root() |
| 45 | + if root == nil { |
| 46 | + return |
| 47 | + } |
| 48 | + if root.Annotations == nil { |
| 49 | + root.Annotations = map[string]string{} |
| 50 | + } |
| 51 | + // Don't print debug message |
| 52 | + root.Annotations[agent.AgentExecutedAnnotation] = "true" |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + c.Flags().StringVar(&cmd.Host, "host", "", "The pro instance to use") |
| 57 | + _ = c.MarkFlagRequired("host") |
| 58 | + |
| 59 | + return c |
| 60 | +} |
| 61 | + |
| 62 | +func (cmd *NetcheckCmd) Run(ctx context.Context, devPodConfig *config.Config, provider *providerpkg.ProviderConfig) error { |
| 63 | + dir, err := providerpkg.GetDaemonDir(devPodConfig.DefaultContext, provider.Name) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("get daemon dir: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + tsClient := &tailscale.LocalClient{ |
| 69 | + Socket: daemon.GetSocketAddr(dir, provider.Name), |
| 70 | + UseSocketOnly: true, |
| 71 | + } |
| 72 | + |
| 73 | + dm, err := tsClient.CurrentDERPMap(ctx) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + for _, region := range dm.Regions { |
| 79 | + report, err := tsClient.DebugDERPRegion(ctx, strconv.Itoa(region.RegionID)) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + msg := fmt.Sprintf("DERP %d (%s)\n", region.RegionID, region.RegionCode) |
| 84 | + if len(report.Errors) > 0 { |
| 85 | + for _, error := range report.Errors { |
| 86 | + msg += fmt.Sprintf(" Error: %s\n", error) |
| 87 | + } |
| 88 | + } |
| 89 | + if len(report.Warnings) > 0 { |
| 90 | + for _, warning := range report.Warnings { |
| 91 | + msg += fmt.Sprintf(" Warning: %s\n", warning) |
| 92 | + } |
| 93 | + } |
| 94 | + if len(report.Info) > 0 { |
| 95 | + for _, info := range report.Info { |
| 96 | + msg += fmt.Sprintf(" Info: %s\n", info) |
| 97 | + } |
| 98 | + } |
| 99 | + fmt.Println(msg) |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments