Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/devspace/config/versions/latest/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ type SSH struct {

// RemoteAddress is the address to listen to inside the container
RemoteAddress string `yaml:"remoteAddress,omitempty" json:"remoteAddress,omitempty" jsonschema_description:"RemoteAddress is the address to listen to inside the container."`

// UseInclude tells DevSpace to use a different file for ssh config
UseInclude bool `yaml:"useInclude,omitempty" json:"useInclude,omitempty"`
}

type EnvVar struct {
Expand Down
38 changes: 37 additions & 1 deletion pkg/devspace/services/ssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,43 @@ var (
MarkerEndPrefix = "# DevSpace End "
)

func configureSSHConfig(host, port string, log log.Logger) error {
func configureSSHConfig(host, port string, useInclude bool, log log.Logger) error {
if useInclude {
return configureSSHConfigSeparateFile(host, port, log)
}

return configureSSHConfigSameFile(host, port, log)
}

func configureSSHConfigSameFile(host, port string, log log.Logger) error {
configLock.Lock()
defer configLock.Unlock()

homeDir, err := homedir.Dir()
if err != nil {
return errors.Wrap(err, "get home dir")
}

sshConfigPath := filepath.Join(homeDir, ".ssh", "config")
newFile, err := addHost(sshConfigPath, host, port)
if err != nil {
return errors.Wrap(err, "parse ssh config")
}

err = os.MkdirAll(filepath.Dir(sshConfigPath), 0755)
if err != nil {
log.Debugf("error creating ssh directory: %v", err)
}

err = ioutil.WriteFile(sshConfigPath, []byte(newFile), 0600)
if err != nil {
return errors.Wrap(err, "write ssh config")
}

return nil
}

func configureSSHConfigSeparateFile(host, port string, log log.Logger) error {
configLock.Lock()
defer configLock.Unlock()

Expand Down
9 changes: 8 additions & 1 deletion pkg/devspace/services/ssh/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ func NewManager(log log.Logger) PortManager {
if err != nil {
log.Errorf("error parsing %s: %v", sshConfigPath, err)
}

reservedPorts := map[int]bool{}
for _, h := range hosts {
reservedPorts[h.Port] = true
}
sshConfigPath = filepath.Join(homeDir, ".ssh", "devspace_config")
hosts, err = ParseDevSpaceHosts(sshConfigPath)
if err != nil {
log.Errorf("error parsing %s: %v", sshConfigPath, err)
}
for _, h := range hosts {
reservedPorts[h.Port] = true
}

return &manager{
reservedPorts: reservedPorts,
Expand Down
9 changes: 6 additions & 3 deletions pkg/devspace/services/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func startSSH(ctx devspacecontext.Context, name, arch string, sshConfig *latest.
// get port
port := sshConfig.LocalPort
if port == 0 {
sshDevSpaceConfigPath := filepath.Join(homeDir, ".ssh", "devspace_config")
sshDevSpaceConfigPath := filepath.Join(homeDir, ".ssh", "config")
if sshConfig.UseInclude {
sshDevSpaceConfigPath = filepath.Join(homeDir, ".ssh", "devspace_config")
}
hosts, err := ParseDevSpaceHosts(sshDevSpaceConfigPath)
if err != nil {
ctx.Log().Debugf("error parsing %s: %v", sshDevSpaceConfigPath, err)
Expand All @@ -92,7 +95,7 @@ func startSSH(ctx devspacecontext.Context, name, arch string, sshConfig *latest.
}

// update ssh config
err = configureSSHConfig(sshHost, strconv.Itoa(port), ctx.Log())
err = configureSSHConfig(sshHost, strconv.Itoa(port), sshConfig.UseInclude, ctx.Log())
if err != nil {
return errors.Wrap(err, "update ssh config")
}
Expand All @@ -104,7 +107,7 @@ func startSSH(ctx devspacecontext.Context, name, arch string, sshConfig *latest.
}

// update ssh config
err = configureSSHConfig(sshHost, strconv.Itoa(port), ctx.Log())
err = configureSSHConfig(sshHost, strconv.Itoa(port), sshConfig.UseInclude, ctx.Log())
if err != nil {
return errors.Wrap(err, "update ssh config")
}
Expand Down