diff --git a/app/gitspace/orchestrator/ide/script/run_ssh_server.sh b/app/gitspace/orchestrator/ide/script/run_ssh_server.sh deleted file mode 100644 index 021ac5f61..000000000 --- a/app/gitspace/orchestrator/ide/script/run_ssh_server.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -echo "Running SSH Server" - -/usr/sbin/sshd \ No newline at end of file diff --git a/app/gitspace/orchestrator/ide/vscode.go b/app/gitspace/orchestrator/ide/vscode.go index 1e8d49d1a..e63af9ce7 100644 --- a/app/gitspace/orchestrator/ide/vscode.go +++ b/app/gitspace/orchestrator/ide/vscode.go @@ -17,27 +17,29 @@ package ide import ( "context" "fmt" + "strconv" "github.com/harness/gitness/app/gitspace/orchestrator/devcontainer" "github.com/harness/gitness/app/gitspace/orchestrator/template" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" - - _ "embed" ) var _ IDE = (*VSCode)(nil) -//go:embed script/run_ssh_server.sh -var runSSHScript string - -const sshPort int = 22 const templateSetupSSHServer string = "setup_ssh_server.sh" +const templateRunSSHServer string = "run_ssh_server.sh" -type VSCode struct{} +type VSCodeConfig struct { + Port int +} -func NewVsCodeService() *VSCode { - return &VSCode{} +type VSCode struct { + config *VSCodeConfig +} + +func NewVsCodeService(config *VSCodeConfig) *VSCode { + return &VSCode{config: config} } // Setup installs the SSH server inside the container. @@ -73,6 +75,15 @@ func (v *VSCode) Setup( func (v *VSCode) Run(ctx context.Context, devcontainer *devcontainer.Exec) ([]byte, error) { var output = "" + runSSHScript, err := template.GenerateScriptFromTemplate( + templateRunSSHServer, &template.RunSSHServerPayload{ + Port: strconv.Itoa(v.config.Port), + }) + if err != nil { + return nil, fmt.Errorf( + "failed to generate scipt to run ssh server from template %s: %w", templateRunSSHServer, err) + } + execOutput, err := devcontainer.ExecuteCommand(ctx, runSSHScript, false, rootUser) if err != nil { return nil, fmt.Errorf("failed to run SSH serverr: %w", err) @@ -85,7 +96,7 @@ func (v *VSCode) Run(ctx context.Context, devcontainer *devcontainer.Exec) ([]by // Port returns the port on which the ssh-server is listening. func (v *VSCode) Port() int { - return sshPort + return v.config.Port } func (v *VSCode) Type() enum.IDEType { diff --git a/app/gitspace/orchestrator/ide/wire.go b/app/gitspace/orchestrator/ide/wire.go index a1e6c3458..d5a1a7da4 100644 --- a/app/gitspace/orchestrator/ide/wire.go +++ b/app/gitspace/orchestrator/ide/wire.go @@ -27,6 +27,6 @@ func ProvideVSCodeWebService(config *VSCodeWebConfig) *VSCodeWeb { return NewVsCodeWebService(config) } -func ProvideVSCodeService() *VSCode { - return NewVsCodeService() +func ProvideVSCodeService(config *VSCodeConfig) *VSCode { + return NewVsCodeService(config) } diff --git a/app/gitspace/orchestrator/template/template.go b/app/gitspace/orchestrator/template/template.go index 8ffc5bb14..e500da5d0 100644 --- a/app/gitspace/orchestrator/template/template.go +++ b/app/gitspace/orchestrator/template/template.go @@ -56,6 +56,10 @@ type SetupSSHServerPayload struct { WorkingDirectory string } +type RunSSHServerPayload struct { + Port string +} + func init() { err := LoadTemplates() if err != nil { diff --git a/app/gitspace/orchestrator/template/templates/run_ssh_server.sh b/app/gitspace/orchestrator/template/templates/run_ssh_server.sh new file mode 100644 index 000000000..1e1f00df2 --- /dev/null +++ b/app/gitspace/orchestrator/template/templates/run_ssh_server.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +SSH_PORT={{ .Port }} + +config_file='/etc/ssh/sshd_config' + +# Change the default SSH port +sed -i "s/^#Port 22/Port $SSH_PORT/" $config_file +if ! grep -q "^Port $SSH_PORT" $config_file; then + echo "Port $SSH_PORT" >> $config_file +fi + +echo "Running SSH Server on port $SSH_PORT" + +/usr/sbin/sshd \ No newline at end of file diff --git a/cli/operations/server/config.go b/cli/operations/server/config.go index e6ad84001..12243dff9 100644 --- a/cli/operations/server/config.go +++ b/cli/operations/server/config.go @@ -406,6 +406,13 @@ func ProvideIDEVSCodeWebConfig(config *types.Config) *ide.VSCodeWebConfig { } } +// ProvideIDEVSCodeConfig loads the VSCode IDE config from the main config. +func ProvideIDEVSCodeConfig(config *types.Config) *ide.VSCodeConfig { + return &ide.VSCodeConfig{ + Port: config.IDE.VSCode.Port, + } +} + // ProvideGitspaceOrchestratorConfig loads the Gitspace orchestrator config from the main config. func ProvideGitspaceOrchestratorConfig(config *types.Config) *orchestrator.Config { return &orchestrator.Config{ diff --git a/cmd/gitness/wire.go b/cmd/gitness/wire.go index c9cf2944e..ccb3c3f20 100644 --- a/cmd/gitness/wire.go +++ b/cmd/gitness/wire.go @@ -235,6 +235,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e gitspaceinfraevents.WireSet, gitspaceservice.WireSet, cliserver.ProvideGitspaceInfraProvisionerConfig, + cliserver.ProvideIDEVSCodeConfig, ) return &cliserver.System{}, nil } diff --git a/cmd/gitness/wire_gen.go b/cmd/gitness/wire_gen.go index 1682fca33..260398b8a 100644 --- a/cmd/gitness/wire_gen.go +++ b/cmd/gitness/wire_gen.go @@ -361,7 +361,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro statefulLogger := logutil.ProvideStatefulLogger(logStream) containerOrchestrator := container.ProvideEmbeddedDockerOrchestrator(dockerClientFactory, statefulLogger) orchestratorConfig := server.ProvideGitspaceOrchestratorConfig(config) - vsCode := ide.ProvideVSCodeService() + vsCodeConfig := server.ProvideIDEVSCodeConfig(config) + vsCode := ide.ProvideVSCodeService(vsCodeConfig) vsCodeWebConfig := server.ProvideIDEVSCodeWebConfig(config) vsCodeWeb := ide.ProvideVSCodeWebService(vsCodeWebConfig) orchestratorOrchestrator := orchestrator.ProvideOrchestrator(scmSCM, infraProviderResourceStore, infraProvisioner, containerOrchestrator, reporter4, orchestratorConfig, vsCode, vsCodeWeb) diff --git a/types/config.go b/types/config.go index 137e92a42..c1b8d760a 100644 --- a/types/config.go +++ b/types/config.go @@ -400,9 +400,14 @@ type Config struct { IDE struct { VSCodeWeb struct { - // Port is the port on which the VS Code Web will be accessible. + // Port is the port on which the VSCode Web will be accessible. Port int `envconfig:"GITNESS_IDE_VSCODEWEB_PORT" default:"8089"` } + + VSCode struct { + // Port is the port on which the SSH server for VSCode will be accessible. + Port int `envconfig:"GITNESS_IDE_VSCODE_PORT" default:"8088"` + } } Gitspace struct {