-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathexternalworkspaces.go
More file actions
270 lines (229 loc) · 7.76 KB
/
Copy pathexternalworkspaces.go
File metadata and controls
270 lines (229 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package cli
import (
"context"
"fmt"
"strings"
"github.com/google/uuid"
"golang.org/x/xerrors"
agpl "github.com/coder/coder/v2/cli"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
"github.com/coder/serpent"
)
type externalAgent struct {
WorkspaceName string `json:"workspace_name"`
AgentName string `json:"agent_name"`
AuthType string `json:"auth_type"`
AuthToken string `json:"auth_token"`
InitScript string `json:"init_script"`
}
func (r *RootCmd) externalWorkspaces() *serpent.Command {
orgContext := agpl.NewOrganizationContext()
cmd := &serpent.Command{
Use: "external-workspaces [subcommand]",
Short: "Create or manage external workspaces",
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
r.externalWorkspaceCreate(),
r.externalWorkspaceAgentInstructions(),
r.externalWorkspaceList(),
},
}
orgContext.AttachOptions(cmd)
return cmd
}
// externalWorkspaceCreate extends `coder create` to create an external workspace.
func (r *RootCmd) externalWorkspaceCreate() *serpent.Command {
opts := agpl.CreateOptions{
BeforeCreate: func(ctx context.Context, client *codersdk.Client, _ codersdk.Template, templateVersionID uuid.UUID) error {
version, err := client.TemplateVersion(ctx, templateVersionID)
if err != nil {
return xerrors.Errorf("get template version: %w", err)
}
if !version.HasExternalAgent {
return xerrors.Errorf("template version %q does not have an external agent. Only templates with external agents can be used for external workspace creation", templateVersionID)
}
return nil
},
AfterCreate: func(ctx context.Context, inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace) error {
workspace, err := client.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{})
if err != nil {
return xerrors.Errorf("get workspace by name: %w", err)
}
externalAgents, err := fetchExternalAgents(inv, client, workspace, workspace.LatestBuild.Resources)
if err != nil {
return xerrors.Errorf("fetch external agents: %w", err)
}
formatted := formatExternalAgent(workspace.Name, externalAgents)
_, err = fmt.Fprintln(inv.Stdout, formatted)
return err
},
}
cmd := r.Create(opts)
cmd.Use = "create [workspace]"
cmd.Short = "Create a new external workspace"
newMiddlewares := []serpent.MiddlewareFunc{}
if cmd.Middleware != nil {
newMiddlewares = append(newMiddlewares, cmd.Middleware)
}
newMiddlewares = append(newMiddlewares, serpent.RequireNArgs(1))
cmd.Middleware = serpent.Chain(newMiddlewares...)
for i := range cmd.Options {
if cmd.Options[i].Flag == "template" {
cmd.Options[i].Required = true
}
}
return cmd
}
// externalWorkspaceAgentInstructions prints the instructions for an external agent.
func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command {
formatter := cliui.NewOutputFormatter(
cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) {
agent, ok := data.(externalAgent)
if !ok {
return "", xerrors.Errorf("expected externalAgent, got %T", data)
}
return formatExternalAgent(agent.WorkspaceName, []externalAgent{agent}), nil
}),
cliui.JSONFormat(),
)
cmd := &serpent.Command{
Use: "agent-instructions [user/]workspace[.agent]",
Short: "Get the instructions for an external agent",
Middleware: serpent.Chain(serpent.RequireNArgs(1)),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
workspace, workspaceAgent, _, err := agpl.GetWorkspaceAndAgent(inv.Context(), inv, client, false, inv.Args[0])
if err != nil {
return xerrors.Errorf("find workspace and agent: %w", err)
}
credentials, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, workspaceAgent.Name)
if err != nil {
return xerrors.Errorf("get external agent token for agent %q: %w", workspaceAgent.Name, err)
}
agentInfo := externalAgent{
WorkspaceName: workspace.Name,
AgentName: workspaceAgent.Name,
AuthType: "token",
AuthToken: credentials.AgentToken,
InitScript: credentials.Command,
}
out, err := formatter.Format(inv.Context(), agentInfo)
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (r *RootCmd) externalWorkspaceList() *serpent.Command {
var (
filter cliui.WorkspaceFilter
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]agpl.WorkspaceListRow{},
[]string{
"workspace",
"template",
"status",
"healthy",
"last built",
"current version",
"outdated",
},
),
cliui.JSONFormat(),
)
)
cmd := &serpent.Command{
Annotations: map[string]string{
"workspaces": "",
},
Use: "list",
Short: "List external workspaces",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
baseFilter := filter.Filter()
if baseFilter.FilterQuery == "" {
baseFilter.FilterQuery = "has_external_agent:true"
} else {
baseFilter.FilterQuery += " has_external_agent:true"
}
res, err := agpl.QueryConvertWorkspaces(inv.Context(), client, baseFilter, agpl.WorkspaceListRowFromWorkspace)
if err != nil {
return err
}
out, err := formatter.Format(inv.Context(), res)
if err != nil {
return err
}
if out == "" {
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
}
_, 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 {
return nil, xerrors.Errorf("no resources found for workspace")
}
var externalAgents []externalAgent
for _, resource := range resources {
if resource.Type != "coder_external_agent" || len(resource.Agents) == 0 {
continue
}
agent := resource.Agents[0]
credentials, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, agent.Name)
if err != nil {
return nil, xerrors.Errorf("get external agent token for agent %q: %w", agent.Name, err)
}
externalAgents = append(externalAgents, externalAgent{
AgentName: agent.Name,
AuthType: "token",
AuthToken: credentials.AgentToken,
InitScript: credentials.Command,
})
}
return externalAgents, nil
}
// formatExternalAgent formats the instructions for an external agent.
func formatExternalAgent(workspaceName string, externalAgents []externalAgent) string {
var output strings.Builder
_, _ = output.WriteString(fmt.Sprintf("\nPlease run the following command to attach external agent to the workspace %s:\n\n", cliui.Keyword(workspaceName)))
for i, agent := range externalAgents {
if len(externalAgents) > 1 {
_, _ = output.WriteString(fmt.Sprintf("For agent %s:\n", cliui.Keyword(agent.AgentName)))
}
_, _ = output.WriteString(fmt.Sprintf("%s\n", pretty.Sprint(cliui.DefaultStyles.Code, agent.InitScript)))
if i < len(externalAgents)-1 {
_, _ = output.WriteString("\n")
}
}
return output.String()
}