Skip to content

Commit

Permalink
retrieve port from SSH config if the port is not defined in the manif…
Browse files Browse the repository at this point in the history
…est. (#1146)

* retrieve port from SSH config if the port is not defined.

Signed-off-by: Ramiro Berrelleza <rberrelleza@gmail.com>
  • Loading branch information
rberrelleza committed Nov 2, 2020
1 parent 1fc68f0 commit 8d5de6b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,21 @@ func executeExec(ctx context.Context, dev *model.Dev, args []string) error {

if dev.RemoteModeEnabled() {
log.Infof("executing remote command over SSH")
if dev.RemotePort == 0 {
p, err := ssh.GetPort(dev.Name)
if err != nil {
log.Infof("failed to get the SSH port for %s: %s", dev.Name, err)
return errors.UserError{
E: fmt.Errorf("development mode is not enabled on your deployment"),
Hint: "Run `okteto up` to enable it and try again",
}
}

dev.RemotePort = p
}

dev.LoadRemote(ssh.GetPublicKey())

return ssh.Exec(ctx, dev.Interface, dev.RemotePort, true, os.Stdin, os.Stdout, os.Stderr, wrapped)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetBinaryFullPath() string {
func GetOktetoHome() string {
if v, ok := os.LookupEnv("OKTETO_FOLDER"); ok {
if !model.FileExists(v) {
log.Fatalf("OKTETO_FOLDER points to a non-existing directory: %s", v)
log.Fatalf("OKTETO_FOLDER doesn't exist: %s", v)
}

return v
Expand Down
26 changes: 26 additions & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ func RemoveEntry(name string) error {
return remove(getSSHConfigPath(), buildHostname(name))
}

// GetPort returns the corresponding SSH port for the dev env
func GetPort(name string) (int, error) {
cfg, err := getConfig(getSSHConfigPath())
if err != nil {
return 0, err
}

hostname := buildHostname(name)
i, found := findHost(cfg, hostname)
if !found {
return 0, fmt.Errorf("development container not found")
}

param := cfg.hosts[i].getParam(portKeyword)
if param == nil {
return 0, fmt.Errorf("port not found")
}

port, err := strconv.Atoi(param.value())
if err != nil {
return 0, fmt.Errorf("invalid port: %s", param.value())
}

return port, nil
}

func remove(path, name string) error {
cfg, err := getConfig(path)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,39 @@ func Test_removeHost(t *testing.T) {
}
}

func TestGetPort(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(dir)

if err := os.Setenv("OKTETO_HOME", dir); err != nil {
t.Fatal(err)
}

defer os.Unsetenv("OKTETO_HOME")

if _, err := GetPort(t.Name()); err == nil {
t.Fatal("expected error on non existing host")
}

if err := AddEntry(t.Name(), "localhost", 123456); err != nil {
t.Fatal(err)
}

p, err := GetPort(t.Name())
if err != nil {
t.Fatal(err)
}

if p != 123456 {
t.Errorf("got %d, expected %d", p, 123456)
}

}

func Test_getSSHConfigPath(t *testing.T) {
ssh := getSSHConfigPath()
parts := strings.Split(ssh, string(os.PathSeparator))
Expand Down

0 comments on commit 8d5de6b

Please sign in to comment.