Skip to content

Commit e18f072

Browse files
committed
refactor: use ~/.ssh/config as default
1 parent 29797e3 commit e18f072

4 files changed

Lines changed: 54 additions & 5 deletions

File tree

pkg/devspace/config/versions/latest/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ type SSH struct {
956956

957957
// RemoteAddress is the address to listen to inside the container
958958
RemoteAddress string `yaml:"remoteAddress,omitempty" json:"remoteAddress,omitempty" jsonschema_description:"RemoteAddress is the address to listen to inside the container."`
959+
960+
// UseInclude tells DevSpace to use a different file for ssh config
961+
UseInclude bool `yaml:"useInclude,omitempty" json:"useInclude,omitempty"`
959962
}
960963

961964
type EnvVar struct {

pkg/devspace/services/ssh/config.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,43 @@ var (
2121
MarkerEndPrefix = "# DevSpace End "
2222
)
2323

24-
func configureSSHConfig(host, port string, log log.Logger) error {
24+
func configureSSHConfig(host, port string, useInclude bool, log log.Logger) error {
25+
if useInclude {
26+
return configureSSHConfigSeparateFile(host, port, log)
27+
}
28+
29+
return configureSSHConfigSameFile(host, port, log)
30+
}
31+
32+
func configureSSHConfigSameFile(host, port string, log log.Logger) error {
33+
configLock.Lock()
34+
defer configLock.Unlock()
35+
36+
homeDir, err := homedir.Dir()
37+
if err != nil {
38+
return errors.Wrap(err, "get home dir")
39+
}
40+
41+
sshConfigPath := filepath.Join(homeDir, ".ssh", "config")
42+
newFile, err := addHost(sshConfigPath, host, port)
43+
if err != nil {
44+
return errors.Wrap(err, "parse ssh config")
45+
}
46+
47+
err = os.MkdirAll(filepath.Dir(sshConfigPath), 0755)
48+
if err != nil {
49+
log.Debugf("error creating ssh directory: %v", err)
50+
}
51+
52+
err = ioutil.WriteFile(sshConfigPath, []byte(newFile), 0600)
53+
if err != nil {
54+
return errors.Wrap(err, "write ssh config")
55+
}
56+
57+
return nil
58+
}
59+
60+
func configureSSHConfigSeparateFile(host, port string, log log.Logger) error {
2561
configLock.Lock()
2662
defer configLock.Unlock()
2763

pkg/devspace/services/ssh/port.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ func NewManager(log log.Logger) PortManager {
4040
if err != nil {
4141
log.Errorf("error parsing %s: %v", sshConfigPath, err)
4242
}
43-
4443
reservedPorts := map[int]bool{}
4544
for _, h := range hosts {
4645
reservedPorts[h.Port] = true
4746
}
47+
sshConfigPath = filepath.Join(homeDir, ".ssh", "devspace_config")
48+
hosts, err = ParseDevSpaceHosts(sshConfigPath)
49+
if err != nil {
50+
log.Errorf("error parsing %s: %v", sshConfigPath, err)
51+
}
52+
for _, h := range hosts {
53+
reservedPorts[h.Port] = true
54+
}
4855

4956
return &manager{
5057
reservedPorts: reservedPorts,

pkg/devspace/services/ssh/ssh.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ func startSSH(ctx devspacecontext.Context, name, arch string, sshConfig *latest.
7373
// get port
7474
port := sshConfig.LocalPort
7575
if port == 0 {
76-
sshDevSpaceConfigPath := filepath.Join(homeDir, ".ssh", "devspace_config")
76+
sshDevSpaceConfigPath := filepath.Join(homeDir, ".ssh", "config")
77+
if sshConfig.UseInclude {
78+
sshDevSpaceConfigPath = filepath.Join(homeDir, ".ssh", "devspace_config")
79+
}
7780
hosts, err := ParseDevSpaceHosts(sshDevSpaceConfigPath)
7881
if err != nil {
7982
ctx.Log().Debugf("error parsing %s: %v", sshDevSpaceConfigPath, err)
@@ -92,7 +95,7 @@ func startSSH(ctx devspacecontext.Context, name, arch string, sshConfig *latest.
9295
}
9396

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

106109
// update ssh config
107-
err = configureSSHConfig(sshHost, strconv.Itoa(port), ctx.Log())
110+
err = configureSSHConfig(sshHost, strconv.Itoa(port), sshConfig.UseInclude, ctx.Log())
108111
if err != nil {
109112
return errors.Wrap(err, "update ssh config")
110113
}

0 commit comments

Comments
 (0)