| 251 | } |
| 252 | |
| 253 | func waitForAgents(ctx context.Context, w io.Writer, client *codersdk.Client, workspaceID uuid.UUID) error { |
| 254 | ctx, span := tracing.StartSpan(ctx) |
| 255 | defer span.End() |
| 256 | _, _ = fmt.Fprint(w, "Waiting for agents to connect...\n\n") |
| 257 | |
| 258 | for { |
| 259 | select { |
| 260 | case <-ctx.Done(): |
| 261 | return ctx.Err() |
| 262 | default: |
| 263 | } |
| 264 | |
| 265 | workspace, err := client.Workspace(ctx, workspaceID) |
| 266 | if err != nil { |
| 267 | return xerrors.Errorf("fetch workspace: %w", err) |
| 268 | } |
| 269 | |
| 270 | ok := true |
| 271 | for _, res := range workspace.LatestBuild.Resources { |
| 272 | for _, agent := range res.Agents { |
| 273 | if agent.Status != codersdk.WorkspaceAgentConnected { |
| 274 | ok = false |
| 275 | } |
| 276 | |
| 277 | _, _ = fmt.Fprintf(w, "\tAgent %q is %s\n", agent.Name, agent.Status) |
| 278 | } |
| 279 | } |
| 280 | if ok { |
| 281 | break |
| 282 | } |
| 283 | |
| 284 | _, _ = fmt.Fprintln(w, "") |
| 285 | time.Sleep(1 * time.Second) |
| 286 | } |
| 287 | |
| 288 | _, _ = fmt.Fprint(w, "\nAgents connected!\n\n") |
| 289 | return nil |
| 290 | } |