Skip to content

Commit

Permalink
feat(ssh): Add/Remove SSH config entry (#172)
Browse files Browse the repository at this point in the history
* add ssh config entry

* address comment

Co-authored-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
  • Loading branch information
VoVAllen and VoVAllen committed May 19, 2022
1 parent 858b2fd commit 161927c
Show file tree
Hide file tree
Showing 5 changed files with 459 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/envd-ssh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/remote/sshd"
"github.com/tensorchord/envd/pkg/ssh"
"github.com/tensorchord/envd/pkg/version"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func sshServer(c *cli.Context) error {
logrus.Fatal(err.Error())
}

port := 2222
port := ssh.DefaultSSHPort
// TODO(gaocegege): Set it as a flag.
if p, ok := os.LookupEnv(envPort); ok {
var err error
Expand Down
6 changes: 6 additions & 0 deletions cmd/envd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
cli "github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/ssh"
"github.com/tensorchord/envd/pkg/util/fileutil"
)

Expand Down Expand Up @@ -57,6 +58,11 @@ func destroy(clicontext *cli.Context) error {
if err := dockerClient.Destroy(clicontext.Context, ctr); err != nil {
return errors.Wrapf(err, "failed to destroy the environment: %s", ctr)
}

if err = ssh.RemoveEntry(ctr); err != nil {
logrus.Infof("failed to remove entry %s from your SSH config file: %s", ctr, err)
return errors.Wrap(err, "failed to remove entry from your SSH config file")
}
logrus.Info("envd environment destroyed")
return nil
}
8 changes: 7 additions & 1 deletion cmd/envd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,15 @@ func up(clicontext *cli.Context) error {
}
logrus.Debugf("container %s is running", containerID)

logrus.Debugf("Add entry %s to SSH config. at %s", buildContext, containerIP)
if err = ssh.AddEntry(ctr, containerIP, ssh.DefaultSSHPort); err != nil {
logrus.Infof("failed to add entry %s to your SSH config file: %s", ctr, err)
return errors.Wrap(err, "failed to add entry to your SSH config file")
}

if !detach {
sshClient, err := ssh.NewClient(
containerIP, "root", 2222, clicontext.Bool("auth"), clicontext.Path("private-key"), "")
containerIP, "root", ssh.DefaultSSHPort, clicontext.Bool("auth"), clicontext.Path("private-key"), "")
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"golang.org/x/term"
)

const DefaultSSHPort = 2222

type Client interface {
Attach() error
}
Expand Down
Loading

0 comments on commit 161927c

Please sign in to comment.