Skip to content

feat: coder-attach: add support for external workspaces #19178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3ea541e
Add attach command and API endpoints for init-script and external age…
kacpersaw Jul 23, 2025
4818df1
add has_external_agents column to template_versions table
kacpersaw Jul 24, 2025
4bfdb83
add external workspace creation and agent instruction commands to cli
kacpersaw Jul 28, 2025
1044051
add has_external_agent to workspace builds
kacpersaw Jul 29, 2025
fd2458b
add list command for external workspaces
kacpersaw Jul 30, 2025
0c39f50
add AgentExternal component to display external agent connection deta…
kacpersaw Jul 30, 2025
23e555a
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Jul 31, 2025
f9f5be1
add tests
kacpersaw Jul 31, 2025
e281f0e
Delete coder attach golden
kacpersaw Aug 5, 2025
d77522d
Hide agent apps when connecting & is external agent
kacpersaw Aug 5, 2025
451c806
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 5, 2025
f9274fe
Reformat code
kacpersaw Aug 5, 2025
00b6f26
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 6, 2025
c019a31
bump provisionerd proto version to v1.9
kacpersaw Aug 6, 2025
7d07857
Add beforeCreate and afterCreate to create handler, apply review sugg…
kacpersaw Aug 6, 2025
c462a69
Refactor init-script endpoint to use path params instead of query params
kacpersaw Aug 6, 2025
2d2dfec
Refactor init-script endpoint, apply review suggestions for db
kacpersaw Aug 6, 2025
387fc04
Apply FE review suggestions
kacpersaw Aug 6, 2025
c2588ea
Return 404 if workspace agent is authenticated through instance id
kacpersaw Aug 6, 2025
33dd778
Merge UpdateTemplateVersionAITaskByJobID and UpdateTemplateVersionExt…
kacpersaw Aug 7, 2025
c413479
update external agent credentials to include command in response
kacpersaw Aug 7, 2025
3c1d694
Bump terraform-provider-coder to v2.10.0
kacpersaw Aug 8, 2025
73acd0f
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 8, 2025
7cc6861
Regenerate sql
kacpersaw Aug 8, 2025
da68c20
Fix lint & tests
kacpersaw Aug 8, 2025
2e24741
Regenerate dump.sql
kacpersaw Aug 8, 2025
682ea60
Fix provision test
kacpersaw Aug 8, 2025
51967a5
update external agent credentials summary and adjust authorization ch…
kacpersaw Aug 8, 2025
f060324
merge UpdateWorkspaceBuildAITaskByID with UpdateWorkspaceBuildExterna…
kacpersaw Aug 8, 2025
ff6e8fa
Apply suggestions from code review
kacpersaw Aug 8, 2025
e2a7182
Apply review suggestions
kacpersaw Aug 8, 2025
141bc54
Merge branch 'main' into kacpersaw/feat-coder-attach
kacpersaw Aug 8, 2025
a75c1f4
make gen
kacpersaw Aug 8, 2025
22f2c00
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add list command for external workspaces
  • Loading branch information
kacpersaw committed Jul 30, 2025
commit fd2458bcc25448d42a77be31f0cc3c856c85289b
82 changes: 74 additions & 8 deletions cli/external_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (r *RootCmd) externalWorkspaces() *serpent.Command {
Children: []*serpent.Command{
r.externalWorkspaceCreate(),
r.externalWorkspaceAgentInstructions(),
r.externalWorkspaceList(),
},
}

Expand Down Expand Up @@ -138,9 +139,9 @@ func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command {
}

var output strings.Builder
output.WriteString(fmt.Sprintf("Please run the following commands to attach agent %s:\n", cliui.Keyword(agent.AgentName)))
output.WriteString(fmt.Sprintf("%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("export CODER_AGENT_TOKEN=%s", agent.AuthToken))))
output.WriteString(pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("curl -fsSL %s | sh", agent.InitScript)))
_, _ = output.WriteString(fmt.Sprintf("Please run the following commands to attach agent %s:\n", cliui.Keyword(agent.AgentName)))
_, _ = output.WriteString(fmt.Sprintf("%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("export CODER_AGENT_TOKEN=%s", agent.AuthToken))))
_, _ = output.WriteString(pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("curl -fsSL %s | sh", agent.InitScript)))

return output.String(), nil
}),
Expand Down Expand Up @@ -204,6 +205,71 @@ func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command {
return cmd
}

func (r *RootCmd) externalWorkspaceList() *serpent.Command {
var (
filter cliui.WorkspaceFilter
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]workspaceListRow{},
[]string{
"workspace",
"template",
"status",
"healthy",
"last built",
"current version",
"outdated",
},
),
cliui.JSONFormat(),
)
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "list",
Short: "List external workspaces",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
baseFilter := filter.Filter()

if baseFilter.FilterQuery == "" {
baseFilter.FilterQuery = "has-external-agent:true"
} else {
baseFilter.FilterQuery += " has-external-agent:true"
}

res, err := queryConvertWorkspaces(inv.Context(), client, baseFilter, workspaceListRowFromWorkspace)
if err != nil {
return err
}

if len(res) == 0 && formatter.FormatID() != cliui.JSONFormat().ID() {
pretty.Fprintf(inv.Stderr, cliui.DefaultStyles.Prompt, "No workspaces found! Create one:\n")
_, _ = fmt.Fprintln(inv.Stderr)
_, _ = fmt.Fprintln(inv.Stderr, " "+pretty.Sprint(cliui.DefaultStyles.Code, "coder external-workspaces create <name>"))
_, _ = fmt.Fprintln(inv.Stderr)
return nil
}

out, err := formatter.Format(inv.Context(), res)
if err != nil {
return err
}

_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
filter.AttachOptions(&cmd.Options)
formatter.AttachOptions(&cmd.Options)
return cmd
}

// fetchExternalAgents fetches the external agents for a workspace.
func fetchExternalAgents(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, resources []codersdk.WorkspaceResource) ([]externalAgent, error) {
if len(resources) == 0 {
Expand Down Expand Up @@ -241,18 +307,18 @@ func fetchExternalAgents(inv *serpent.Invocation, client *codersdk.Client, works

// printExternalAgents prints the instructions for an external agent.
func printExternalAgents(inv *serpent.Invocation, workspaceName string, externalAgents []externalAgent) error {
fmt.Fprintf(inv.Stdout, "\nPlease run the following commands to attach external agent to the workspace %s:\n\n", cliui.Keyword(workspaceName))
_, _ = fmt.Fprintf(inv.Stdout, "\nPlease run the following commands to attach external agent to the workspace %s:\n\n", cliui.Keyword(workspaceName))

for i, agent := range externalAgents {
if len(externalAgents) > 1 {
fmt.Fprintf(inv.Stdout, "For agent %s:\n", cliui.Keyword(agent.AgentName))
_, _ = fmt.Fprintf(inv.Stdout, "For agent %s:\n", cliui.Keyword(agent.AgentName))
}

fmt.Fprintf(inv.Stdout, "%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("export CODER_AGENT_TOKEN=%s", agent.AuthToken)))
fmt.Fprintf(inv.Stdout, "%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("curl -fsSL %s | sh", agent.InitScript)))
_, _ = fmt.Fprintf(inv.Stdout, "%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("export CODER_AGENT_TOKEN=%s", agent.AuthToken)))
_, _ = fmt.Fprintf(inv.Stdout, "%s\n", pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("curl -fsSL %s | sh", agent.InitScript)))

if i < len(externalAgents)-1 {
fmt.Fprintf(inv.Stdout, "\n")
_, _ = fmt.Fprintf(inv.Stdout, "\n")
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/testdata/coder_external-workspaces_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ USAGE:
SUBCOMMANDS:
agent-instructions Get the instructions for an external agent
create Create a new external workspace
list List external workspaces

OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Expand Down
24 changes: 24 additions & 0 deletions cli/testdata/coder_external-workspaces_list_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
coder v0.0.0-devel

USAGE:
coder external-workspaces list [flags]

List external workspaces

Aliases: ls

OPTIONS:
-a, --all bool
Specifies whether all workspaces will be listed or not.

-c, --column [favorite|workspace|organization id|organization name|template|status|healthy|last built|current version|outdated|starts at|starts next|stops after|stops next|daily cost] (default: workspace,template,status,healthy,last built,current version,outdated)
Columns to display in table output.

-o, --output table|json (default: table)
Output format.

--search string (default: owner:me)
Search for a workspace with a query.

———
Run `coder --help` for a list of global options.
3 changes: 2 additions & 1 deletion cli/testdata/coder_list_--output_json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"most_recently_seen": null
},
"template_version_preset_id": null,
"has_ai_task": false
"has_ai_task": false,
"has_external_agent": false
},
"latest_app_status": null,
"outdated": false,
Expand Down
6 changes: 6 additions & 0 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,11 @@ func (api *API) convertWorkspaceBuild(
aiTasksSidebarAppID = &build.AITaskSidebarAppID.UUID
}

var hasExternalAgent *bool
if build.HasExternalAgent.Valid {
hasExternalAgent = &build.HasExternalAgent.Bool
}

apiJob := convertProvisionerJob(job)
transition := codersdk.WorkspaceTransition(build.Transition)
return codersdk.WorkspaceBuild{
Expand Down Expand Up @@ -1185,6 +1190,7 @@ func (api *API) convertWorkspaceBuild(
TemplateVersionPresetID: presetID,
HasAITask: hasAITask,
AITaskSidebarAppID: aiTasksSidebarAppID,
HasExternalAgent: hasExternalAgent,
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,11 @@
"description": "Create a new external workspace",
"path": "reference/cli/external-workspaces_create.md"
},
{
"title": "external-workspaces list",
"description": "List external workspaces",
"path": "reference/cli/external-workspaces_list.md"
},
{
"title": "favorite",
"description": "Add a workspace to your favorites",
Expand Down
1 change: 1 addition & 0 deletions docs/reference/cli/external-workspaces.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions docs/reference/cli/external-workspaces_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.