* Post-create setup: provision Coder workspace and configure SSH. * This runs after mux persists workspace metadata, so build logs stream to UI.
(params: WorkspaceInitParams)
| 773 | * This runs after mux persists workspace metadata, so build logs stream to UI. |
| 774 | */ |
| 775 | async postCreateSetup(params: WorkspaceInitParams): Promise<void> { |
| 776 | const { initLogger, abortSignal } = params; |
| 777 | |
| 778 | // Create Coder workspace if not connecting to an existing one |
| 779 | if (!this.coderConfig.existingWorkspace) { |
| 780 | // Validate required fields (workspaceName is set by finalizeConfig during workspace creation) |
| 781 | const coderWorkspaceName = this.coderConfig.workspaceName; |
| 782 | if (!coderWorkspaceName) { |
| 783 | throw new Error("Coder workspace name is required (should be set by finalizeConfig)"); |
| 784 | } |
| 785 | if (!this.coderConfig.template) { |
| 786 | await this.coderService.disposeProvisioningSession(coderWorkspaceName); |
| 787 | throw new Error("Coder template is required for new workspaces"); |
| 788 | } |
| 789 | |
| 790 | initLogger.logStep(`Creating Coder workspace "${coderWorkspaceName}"...`); |
| 791 | |
| 792 | const provisioningSession = this.coderService.takeProvisioningSession(coderWorkspaceName); |
| 793 | |
| 794 | try { |
| 795 | for await (const line of this.coderService.createWorkspace( |
| 796 | coderWorkspaceName, |
| 797 | this.coderConfig.template, |
| 798 | this.coderConfig.preset, |
| 799 | abortSignal, |
| 800 | this.coderConfig.templateOrg, |
| 801 | provisioningSession |
| 802 | )) { |
| 803 | initLogger.logStdout(line); |
| 804 | } |
| 805 | initLogger.logStep("Coder workspace created successfully"); |
| 806 | |
| 807 | // Wait for startup scripts to complete |
| 808 | initLogger.logStep("Waiting for startup scripts..."); |
| 809 | for await (const line of this.coderService.waitForStartupScripts( |
| 810 | coderWorkspaceName, |
| 811 | abortSignal |
| 812 | )) { |
| 813 | initLogger.logStdout(line); |
| 814 | } |
| 815 | } catch (error) { |
| 816 | const errorMsg = getErrorMessage(error); |
| 817 | log.error("Failed to create Coder workspace", { error, config: this.coderConfig }); |
| 818 | initLogger.logStderr(`Failed to create Coder workspace: ${errorMsg}`); |
| 819 | throw new Error(`Failed to create Coder workspace: ${errorMsg}`); |
| 820 | } finally { |
| 821 | if (provisioningSession) { |
| 822 | await provisioningSession.dispose(); |
| 823 | } |
| 824 | } |
| 825 | } else if (this.coderConfig.workspaceName) { |
| 826 | // For existing workspaces, wait for "stopping"/"canceling" to clear before SSH |
| 827 | // (coder ssh --wait=yes can't autostart while a stop/cancel build is in progress) |
| 828 | const workspaceName = this.coderConfig.workspaceName; |
| 829 | const status = await this.coderService.getWorkspaceStatus(workspaceName, { |
| 830 | signal: abortSignal, |
| 831 | }); |
| 832 | await this.waitForStoppingOrCancelingToClear(workspaceName, status, { |
nothing calls this directly
no test coverage detected