diff --git a/app/gitspace/orchestrator/container/embedded_docker.go b/app/gitspace/orchestrator/container/embedded_docker.go index b01cde30c..7c9ebefc0 100644 --- a/app/gitspace/orchestrator/container/embedded_docker.go +++ b/app/gitspace/orchestrator/container/embedded_docker.go @@ -136,6 +136,17 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace( return nil, startErr } + devcontainer := &Devcontainer{ + ContainerName: containerName, + WorkingDir: e.getWorkingDir(repoName), + DockerClient: dockerClient, + } + + err = e.runIDE(ctx, devcontainer, ideService, logStreamInstance) + if err != nil { + return nil, err + } + // TODO: Add gitspace status reporting. log.Debug().Msg("started gitspace") @@ -154,8 +165,6 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace( } }() - workingDirectory := "/" + repoName - startErr := e.startGitspace( ctx, gitspaceConfig, @@ -165,7 +174,7 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace( ideService, logStreamInstance, infra.Storage, - workingDirectory, + e.getWorkingDir(repoName), ) if startErr != nil { return nil, fmt.Errorf("failed to start gitspace %s: %w", containerName, startErr) @@ -190,6 +199,10 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace( }, nil } +func (e *EmbeddedDockerOrchestrator) getWorkingDir(repoName string) string { + return "/" + repoName +} + func (e *EmbeddedDockerOrchestrator) startGitspace( ctx context.Context, gitspaceConfig *types.GitspaceConfig, @@ -246,6 +259,11 @@ func (e *EmbeddedDockerOrchestrator) startGitspace( return err } + err = e.runIDE(ctx, devcontainer, ideService, logStreamInstance) + if err != nil { + return err + } + err = e.executePostCreateCommand(ctx, devcontainerConfig, devcontainer, logStreamInstance) if err != nil { return err @@ -254,6 +272,45 @@ func (e *EmbeddedDockerOrchestrator) startGitspace( return nil } +// TODO: Instead of explicitly running IDE related processes, we can explore service to run the service on boot. + +func (e *EmbeddedDockerOrchestrator) runIDE( + ctx context.Context, + devcontainer *Devcontainer, + ideService IDE, + logStreamInstance *logutil.LogStreamInstance, +) error { + loggingErr := logStreamInstance.Write("Running the IDE inside container: " + string(ideService.Type())) + if loggingErr != nil { + return fmt.Errorf("logging error: %w", loggingErr) + } + + output, err := ideService.Run(ctx, devcontainer) + if err != nil { + loggingErr = logStreamInstance.Write("Error while running IDE inside container: " + err.Error()) + + err = fmt.Errorf("failed to run the IDE for gitspace %s: %w", devcontainer.ContainerName, err) + + if loggingErr != nil { + err = fmt.Errorf("original error: %w; logging error: %w", err, loggingErr) + } + + return err + } + + loggingErr = logStreamInstance.Write("IDE run output...\n" + string(output)) + if loggingErr != nil { + return fmt.Errorf("logging error: %w", loggingErr) + } + + loggingErr = logStreamInstance.Write("Successfully run the IDE inside container") + if loggingErr != nil { + return fmt.Errorf("logging error: %w", loggingErr) + } + + return nil +} + func (e *EmbeddedDockerOrchestrator) setupIDE( ctx context.Context, gitspaceInstance *types.GitspaceInstance, @@ -489,8 +546,6 @@ func (e *EmbeddedDockerOrchestrator) createContainer( portBindings[natPort] = hostPortBindings } - entryPoint := []string{"/bin/bash", "-c", `trap "exit 0" 15; sleep infinity`} - loggingErr := logStreamInstance.Write("Creating container: " + containerName) if loggingErr != nil { return fmt.Errorf("logging error: %w", loggingErr) @@ -498,7 +553,8 @@ func (e *EmbeddedDockerOrchestrator) createContainer( _, err := dockerClient.ContainerCreate(ctx, &container.Config{ Image: imageName, - Entrypoint: entryPoint, + Entrypoint: []string{"/bin/bash"}, + Cmd: []string{"-c", "trap \"exit 0\" 15;\n sleep infinity & wait $!"}, ExposedPorts: exposedPorts, }, &container.HostConfig{ PortBindings: portBindings, diff --git a/app/gitspace/orchestrator/container/ide.go b/app/gitspace/orchestrator/container/ide.go index 638a75837..05f52b6c4 100644 --- a/app/gitspace/orchestrator/container/ide.go +++ b/app/gitspace/orchestrator/container/ide.go @@ -23,9 +23,12 @@ import ( type IDE interface { // Setup is responsible for doing all the operations for setting up the IDE in the container e.g. installation, - // copying settings and configurations, ensuring SSH server is running etc. + // copying settings and configurations. Setup(ctx context.Context, containerParams *Devcontainer, gitspaceInstance *types.GitspaceInstance) ([]byte, error) + // Run runs the IDE and supporting services. + Run(ctx context.Context, containerParams *Devcontainer) ([]byte, error) + // PortAndProtocol provides the port with protocol which will be used by this IDE. PortAndProtocol() string diff --git a/app/gitspace/orchestrator/container/template.go b/app/gitspace/orchestrator/container/template.go index aaffc0214..b8c4bbefd 100644 --- a/app/gitspace/orchestrator/container/template.go +++ b/app/gitspace/orchestrator/container/template.go @@ -40,7 +40,7 @@ type CloneGitPayload struct { Branch string } -type RunVSCodeWebPayload struct { +type InstallVSCodeWebPayload struct { Port string } diff --git a/app/gitspace/orchestrator/container/template/clone_git.sh b/app/gitspace/orchestrator/container/template/clone_git.sh index 4f046e722..99ff888fb 100644 --- a/app/gitspace/orchestrator/container/template/clone_git.sh +++ b/app/gitspace/orchestrator/container/template/clone_git.sh @@ -45,6 +45,4 @@ EOL fi else echo "devcontainer_present is set to true. Skipping .devcontainer creation." -fi - -echo "Script completed." +fi \ No newline at end of file diff --git a/app/gitspace/orchestrator/container/template/install_vscode_web.sh b/app/gitspace/orchestrator/container/template/install_vscode_web.sh index 090b27583..081cbad14 100644 --- a/app/gitspace/orchestrator/container/template/install_vscode_web.sh +++ b/app/gitspace/orchestrator/container/template/install_vscode_web.sh @@ -1,5 +1,17 @@ #!/bin/bash -echo "Installing code-server" +echo "Installing VSCode Web" -curl -fsSL https://code-server.dev/install.sh | sh \ No newline at end of file +curl -fsSL https://code-server.dev/install.sh | sh + +port={{ .Port }} + +# Ensure the configuration directory exists +mkdir -p /root/.config/code-server + +# Create or overwrite the config file with new settings +cat > /root/.config/code-server/config.yaml < /root/.config/code-server/config.yaml <