createDevcontainer should run in its own goroutine and is responsible for recreating a devcontainer based on the provided devcontainer configuration. It updates the devcontainer status and logs the process. The configPath is passed as a parameter for the odd chance that the container being recreated
(workspaceFolder, configPath string, opts ...DevcontainerCLIUpOptions)
| 1440 | // The devcontainer state must be set to starting and the asyncWg must be |
| 1441 | // incremented before calling this function. |
| 1442 | func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...DevcontainerCLIUpOptions) error { |
| 1443 | api.mu.Lock() |
| 1444 | if api.closed { |
| 1445 | api.mu.Unlock() |
| 1446 | return nil |
| 1447 | } |
| 1448 | |
| 1449 | dc, found := api.knownDevcontainers[workspaceFolder] |
| 1450 | if !found { |
| 1451 | api.mu.Unlock() |
| 1452 | return xerrors.Errorf("devcontainer not found") |
| 1453 | } |
| 1454 | |
| 1455 | var ( |
| 1456 | ctx = api.ctx |
| 1457 | logger = api.logger.With( |
| 1458 | slog.F("devcontainer_id", dc.ID), |
| 1459 | slog.F("devcontainer_name", dc.Name), |
| 1460 | slog.F("workspace_folder", dc.WorkspaceFolder), |
| 1461 | slog.F("config_path", dc.ConfigPath), |
| 1462 | ) |
| 1463 | ) |
| 1464 | |
| 1465 | // Send logs via agent logging facilities. |
| 1466 | logSourceID := api.devcontainerLogSourceIDs[dc.WorkspaceFolder] |
| 1467 | if logSourceID == uuid.Nil { |
| 1468 | api.logger.Debug(api.ctx, "devcontainer log source ID not found, falling back to external log source ID") |
| 1469 | logSourceID = agentsdk.ExternalLogSourceID |
| 1470 | } |
| 1471 | |
| 1472 | api.asyncWg.Add(1) |
| 1473 | defer api.asyncWg.Done() |
| 1474 | api.mu.Unlock() |
| 1475 | |
| 1476 | if dc.ConfigPath != configPath { |
| 1477 | logger.Warn(ctx, "devcontainer config path mismatch", |
| 1478 | slog.F("config_path_param", configPath), |
| 1479 | ) |
| 1480 | } |
| 1481 | |
| 1482 | scriptLogger := api.scriptLogger(logSourceID) |
| 1483 | defer func() { |
| 1484 | flushCtx, cancel := context.WithTimeout(api.ctx, 5*time.Second) |
| 1485 | defer cancel() |
| 1486 | if err := scriptLogger.Flush(flushCtx); err != nil { |
| 1487 | logger.Error(flushCtx, "flush devcontainer logs failed during recreation", slog.Error(err)) |
| 1488 | } |
| 1489 | }() |
| 1490 | infoW := agentsdk.LogsWriter(ctx, scriptLogger.Send, logSourceID, codersdk.LogLevelInfo) |
| 1491 | defer infoW.Close() |
| 1492 | errW := agentsdk.LogsWriter(ctx, scriptLogger.Send, logSourceID, codersdk.LogLevelError) |
| 1493 | defer errW.Close() |
| 1494 | |
| 1495 | logger.Debug(ctx, "starting devcontainer recreation") |
| 1496 | |
| 1497 | upOptions := []DevcontainerCLIUpOptions{WithUpOutput(infoW, errW)} |
| 1498 | upOptions = append(upOptions, opts...) |
| 1499 |