Skip to content

Commit

Permalink
fix: Remove ir direct access in ssh (#1271)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <[email protected]>

Signed-off-by: Ce Gao <[email protected]>
  • Loading branch information
gaocegege authored Dec 6, 2022
1 parent 06b4880 commit 235dde9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion pkg/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/ssh"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
"github.com/tensorchord/envd/pkg/types"
Expand Down Expand Up @@ -176,7 +177,8 @@ func create(clicontext *cli.Context) error {
}

go func() {
if err := sshClient.Attach(); err != nil {
if err := sshClient.Attach(ir.DefaultGraph.Shell,
ir.DefaultGraph.EnvironmentName); err != nil {
outputChannel <- errors.Wrap(err, "failed to attach to the container")
}
outputChannel <- nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/envd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ func (e dockerEngine) Attach(name, iface, privateKeyPath string,
}
opt.Server = iface

if err := sshClient.Attach(); err != nil {
if err := sshClient.Attach(
ir.DefaultGraph.Shell, ir.DefaultGraph.EnvironmentName); err != nil {
return errors.Wrap(err, "failed to attach to the container")
}
return nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/envd/envdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/tensorchord/envd-server/errdefs"
"github.com/tensorchord/envd-server/sshname"

"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/ssh"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
"github.com/tensorchord/envd/pkg/types"
Expand Down Expand Up @@ -180,7 +181,8 @@ func (e envdServerEngine) Attach(name, iface, privateKeyPath string, startResult
}

go func() {
if err := sshClient.Attach(); err != nil {
if err := sshClient.Attach(ir.DefaultGraph.Shell,
ir.DefaultGraph.EnvironmentName); err != nil {
outputChannel <- errors.Wrap(err, "failed to attach to the container")
}
outputChannel <- nil
Expand Down
10 changes: 4 additions & 6 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ import (
"golang.org/x/crypto/ssh/agent"
"golang.org/x/term"

"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/ssh/config"
)

type Client interface {
Attach() error
Attach(shell, envName string) error
ExecWithOutput(cmd string) ([]byte, error)
LocalForward(localAddress, targetAddress string) error
Close() error
Expand Down Expand Up @@ -181,7 +180,7 @@ func (c generalClient) ExecWithOutput(cmd string) ([]byte, error) {
return session.CombinedOutput(cmd)
}

func (c generalClient) Attach() error {
func (c generalClient) Attach(shell, envName string) error {
// open session
session, err := c.cli.NewSession()
if err != nil {
Expand Down Expand Up @@ -263,13 +262,12 @@ func (c generalClient) Attach() error {
}
}()

// TODO(gaocegege): Refactor it to avoid direct access to DefaultGraph
cmd := shellescape.QuoteCommand([]string{ir.DefaultGraph.Shell})
cmd := shellescape.QuoteCommand([]string{shell})
logrus.Debugf("executing command over ssh: '%s'", cmd)
err = session.Run(cmd)
if err == nil {
logrus.Infof("Detached successfully. You can attach to the container with command `ssh %s.envd`\n",
ir.DefaultGraph.EnvironmentName)
envName)
return nil
}
if strings.Contains(err.Error(), "status 130") || strings.Contains(err.Error(), "4294967295") {
Expand Down

0 comments on commit 235dde9

Please sign in to comment.