feat: [CDE-227]: Change SSH port for VSCode. (#2445)

* feat: [CDE-227]: Change SSH port for VSCode.
This commit is contained in:
Dhruv Dhruv 2024-08-09 08:31:14 +00:00 committed by Harness
parent e400a6df44
commit 008aa5c17a
9 changed files with 58 additions and 19 deletions

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Running SSH Server"
/usr/sbin/sshd

View File

@ -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 {

View File

@ -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)
}

View File

@ -56,6 +56,10 @@ type SetupSSHServerPayload struct {
WorkingDirectory string
}
type RunSSHServerPayload struct {
Port string
}
func init() {
err := LoadTemplates()
if err != nil {

View File

@ -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

View File

@ -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{

View File

@ -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
}

View File

@ -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)

View File

@ -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 {