MCPcopy Index your code
hub / github.com/coder/coder / createExternalWorkspace

Method createExternalWorkspace

scaletest/taskstatus/run.go:286–356  ·  view source on GitHub ↗

createExternalWorkspace creates an external workspace and returns the workspace ID and agent token for the first external agent found in the workspace resources.

(ctx context.Context, req codersdk.CreateWorkspaceRequest)

Source from the content-addressed store, hash-verified

284// createExternalWorkspace creates an external workspace and returns the workspace ID
285// and agent token for the first external agent found in the workspace resources.
286func (r *Runner) createExternalWorkspace(ctx context.Context, req codersdk.CreateWorkspaceRequest) (createExternalWorkspaceResult, error) {
287 // Create the workspace
288 workspace, err := r.client.CreateUserWorkspace(ctx, codersdk.Me, req)
289 if err != nil {
290 return createExternalWorkspaceResult{}, err
291 }
292
293 r.logger.Info(ctx, "waiting for workspace build to complete",
294 slog.F("workspace_name", workspace.Name),
295 slog.F("workspace_id", workspace.ID))
296
297 // Poll the workspace until the build is complete
298 var finalWorkspace codersdk.Workspace
299 buildComplete := xerrors.New("build complete") // sentinel error
300 waiter := r.clock.TickerFunc(ctx, 30*time.Second, func() error {
301 // Get the workspace with latest build details
302 workspace, err := r.client.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{})
303 if err != nil {
304 r.logger.Error(ctx, "failed to poll workspace while waiting for build to complete", slog.Error(err))
305 return nil
306 }
307
308 jobStatus := workspace.LatestBuild.Job.Status
309 r.logger.Debug(ctx, "checking workspace build status",
310 slog.F("status", jobStatus),
311 slog.F("build_id", workspace.LatestBuild.ID))
312
313 switch jobStatus {
314 case codersdk.ProvisionerJobSucceeded:
315 // Build succeeded
316 r.logger.Info(ctx, "workspace build succeeded")
317 finalWorkspace = workspace
318 return buildComplete
319 case codersdk.ProvisionerJobFailed:
320 return xerrors.Errorf("workspace build failed: %s", workspace.LatestBuild.Job.Error)
321 case codersdk.ProvisionerJobCanceled:
322 return xerrors.Errorf("workspace build was canceled")
323 case codersdk.ProvisionerJobPending, codersdk.ProvisionerJobRunning, codersdk.ProvisionerJobCanceling:
324 // Still in progress, continue polling
325 return nil
326 default:
327 return xerrors.Errorf("unexpected job status: %s", jobStatus)
328 }
329 }, "createExternalWorkspace")
330
331 err = waiter.Wait()
332 if err != nil && !xerrors.Is(err, buildComplete) {
333 return createExternalWorkspaceResult{}, xerrors.Errorf("wait for build completion: %w", err)
334 }
335
336 // Find external agents in resources
337 for _, resource := range finalWorkspace.LatestBuild.Resources {
338 if resource.Type != "coder_external_agent" || len(resource.Agents) == 0 {
339 continue
340 }
341
342 // Get credentials for the first agent
343 agent := resource.Agents[0]

Callers 1

RunMethod · 0.95

Calls 9

CreateUserWorkspaceMethod · 0.65
NewMethod · 0.65
WaitMethod · 0.65
InfoMethod · 0.45
ErrorMethod · 0.45
ErrorfMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected