diff --git a/cli/support.go b/cli/support.go index 07290a7e63..8501e0e8e9 100644 --- a/cli/support.go +++ b/cli/support.go @@ -71,9 +71,9 @@ func (r *RootCmd) supportBundle() *serpent.Command { var templateName string var pprof bool cmd := &serpent.Command{ - Use: "bundle []", + Use: "bundle [] []", Short: "Generate a support bundle to troubleshoot issues connecting to a workspace.", - Long: `This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You must specify a single workspace (and optionally an agent name).`, + Long: `This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You may specify a single workspace (and optionally an agent name). When run inside a workspace, the workspace and agent are inferred from the environment if not provided.`, Middleware: serpent.Chain( serpent.RequireRangeArgs(0, 2), ), @@ -149,6 +149,38 @@ func (r *RootCmd) supportBundle() *serpent.Command { templateID uuid.UUID ) + if len(inv.Args) == 0 { + // When running inside a workspace, infer the workspace + // and agent from environment variables set by the agent. + // Prefer CODER_WORKSPACE_ID for a direct UUID lookup; + // fall back to owner/name for older agents that do not + // set the ID variable. + if inv.Environ.Get("CODER") == "true" { + var wsArg string + if v := inv.Environ.Get("CODER_WORKSPACE_ID"); v != "" { + wsArg = v + } else { + wsOwner := inv.Environ.Get("CODER_WORKSPACE_OWNER_NAME") + wsName := inv.Environ.Get("CODER_WORKSPACE_NAME") + if wsOwner != "" && wsName != "" { + wsArg = wsOwner + "/" + wsName + } + } + agtName := inv.Environ.Get("CODER_WORKSPACE_AGENT_NAME") + if wsArg != "" { + cliLog.Info(inv.Context(), "detected workspace from environment", + slog.F("workspace_arg", wsArg), + slog.F("agent_name", agtName), + ) + cliui.Info(inv.Stderr, "Detected workspace from environment: "+wsArg) + inv.Args = append(inv.Args, wsArg) + if agtName != "" { + inv.Args = append(inv.Args, agtName) + } + } + } + } + if len(inv.Args) == 0 { cliLog.Warn(inv.Context(), "no workspace specified") cliui.Warn(inv.Stderr, "No workspace specified. This will result in incomplete information.") diff --git a/cli/support_test.go b/cli/support_test.go index 58a7929d53..3edada4bfa 100644 --- a/cli/support_test.go +++ b/cli/support_test.go @@ -118,6 +118,43 @@ func TestSupportBundle(t *testing.T) { assertBundleContents(t, path, false, false, []string{secretValue}) }) + t.Run("InferWorkspaceFromEnvByID", func(t *testing.T) { + t.Parallel() + + d := t.TempDir() + path := filepath.Join(d, "bundle.zip") + // No workspace arg, but set env vars as if inside a workspace. + inv, root := clitest.New(t, "support", "bundle", "--output-file", path, "--yes") + inv.Environ.Set("CODER", "true") + inv.Environ.Set("CODER_WORKSPACE_ID", workspaceWithoutAgent.Workspace.ID.String()) + inv.Environ.Set("CODER_WORKSPACE_AGENT_NAME", "dev") + //nolint: gocritic // requires owner privilege + clitest.SetupConfig(t, client, root) + err := inv.Run() + require.NoError(t, err) + // The workspace should be resolved, but there is no running agent. + assertBundleContents(t, path, true, false, []string{secretValue}) + }) + + t.Run("InferWorkspaceFromEnvByName", func(t *testing.T) { + t.Parallel() + + d := t.TempDir() + path := filepath.Join(d, "bundle.zip") + // No workspace arg and no CODER_WORKSPACE_ID; fall back to + // owner/name resolution for older agents. + inv, root := clitest.New(t, "support", "bundle", "--output-file", path, "--yes") + inv.Environ.Set("CODER", "true") + inv.Environ.Set("CODER_WORKSPACE_NAME", workspaceWithoutAgent.Workspace.Name) + inv.Environ.Set("CODER_WORKSPACE_OWNER_NAME", coderdtest.FirstUserParams.Username) + inv.Environ.Set("CODER_WORKSPACE_AGENT_NAME", "dev") + //nolint: gocritic // requires owner privilege + clitest.SetupConfig(t, client, root) + err := inv.Run() + require.NoError(t, err) + assertBundleContents(t, path, true, false, []string{secretValue}) + }) + t.Run("NoAgent", func(t *testing.T) { t.Parallel() d := t.TempDir() diff --git a/cli/testdata/coder_support_bundle_--help.golden b/cli/testdata/coder_support_bundle_--help.golden index ed0973aa42..0843a43f56 100644 --- a/cli/testdata/coder_support_bundle_--help.golden +++ b/cli/testdata/coder_support_bundle_--help.golden @@ -1,13 +1,14 @@ coder v0.0.0-devel USAGE: - coder support bundle [flags] [] + coder support bundle [flags] [] [] Generate a support bundle to troubleshoot issues connecting to a workspace. This command generates a file containing detailed troubleshooting information - about the Coder deployment and workspace connections. You must specify a - single workspace (and optionally an agent name). + about the Coder deployment and workspace connections. You may specify a single + workspace (and optionally an agent name). When run inside a workspace, the + workspace and agent are inferred from the environment if not provided. OPTIONS: -O, --output-file string, $CODER_SUPPORT_BUNDLE_OUTPUT_FILE diff --git a/docs/reference/cli/support_bundle.md b/docs/reference/cli/support_bundle.md index 40744f819b..9c58131892 100644 --- a/docs/reference/cli/support_bundle.md +++ b/docs/reference/cli/support_bundle.md @@ -6,13 +6,13 @@ Generate a support bundle to troubleshoot issues connecting to a workspace. ## Usage ```console -coder support bundle [flags] [] +coder support bundle [flags] [] [] ``` ## Description ```console -This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You must specify a single workspace (and optionally an agent name). +This command generates a file containing detailed troubleshooting information about the Coder deployment and workspace connections. You may specify a single workspace (and optionally an agent name). When run inside a workspace, the workspace and agent are inferred from the environment if not provided. ``` ## Options