Skip to content

Commit

Permalink
feat: expose support listen addr (#1128)
Browse files Browse the repository at this point in the history
* Add listening_addr to expose fun

Signed-off-by: nullday <aseaday@hotmail.com>

* Rename parameter

Signed-off-by: nullday <aseaday@hotmail.com>

* feat: support cli arguements to specify listening port

Signed-off-by: nullday <aseaday@hotmail.com>

Signed-off-by: nullday <aseaday@hotmail.com>
  • Loading branch information
aseaday committed Nov 1, 2022
1 parent 8776a25 commit 7bf8c00
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
8 changes: 7 additions & 1 deletion pkg/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ var CommandCreate = &cli.Command{
Value: sshconfig.GetPrivateKeyOrPanic(),
Hidden: true,
},
&cli.StringFlag{
Name: "host",
Usage: "Assign the host address for environment ssh acesss server listening",
Value: envd.Localhost,
},
},
Action: create,
}
Expand All @@ -90,6 +95,7 @@ func create(clicontext *cli.Context) error {
name = strings.ToLower(randomdata.SillyName())
}
opt := envd.StartOptions{
SshdHost: clicontext.String("host"),
Image: clicontext.String("image"),
Timeout: clicontext.Duration("timeout"),
EnvironmentName: name,
Expand All @@ -105,7 +111,7 @@ func create(clicontext *cli.Context) error {
logrus.Debugf("container %s is running", res.Name)

logrus.Debugf("add entry %s to SSH config.", res.Name)
hostname, err := c.GetSSHHostname()
hostname, err := c.GetSSHHostname(opt.SshdHost)
if err != nil {
return errors.Wrap(err, "failed to get the ssh hostname")
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ var CommandUp = &cli.Command{
Usage: "Force rebuild and run the container although the previous container is running",
Value: false,
},
&cli.StringFlag{
Name: "host",
Usage: "Assign the host address for environment ssh acesss server listening",
Value: envd.Localhost,
},
// https://github.com/urfave/cli/issues/1134#issuecomment-1191407527
&cli.StringFlag{
Name: "export-cache",
Expand Down Expand Up @@ -132,6 +137,12 @@ func up(clicontext *cli.Context) error {
buildOpt.OutputOpts = fmt.Sprintf("type=image,name=%s,push=true", buildOpt.Tag)
}
start := time.Now()
// Unable to modify sshd host when runner is envd-server.
if c.Runner == types.RunnerTypeEnvdServer {
if clicontext.String("host") != envd.Localhost {
return errors.New("Failed to modify the sshd host when runner is envd-server.")
}
}

ctr := filepath.Base(buildOpt.BuildContextDir)
detach := clicontext.Bool("detach")
Expand Down Expand Up @@ -177,14 +188,14 @@ func up(clicontext *cli.Context) error {
if err != nil {
return errors.Wrap(err, "failed to create the docker client")
}

startOptions := envd.StartOptions{
EnvironmentName: filepath.Base(buildOpt.BuildContextDir),
BuildContext: buildOpt.BuildContextDir,
Image: buildOpt.Tag,
NumGPU: numGPU,
Forced: clicontext.Bool("force"),
Timeout: clicontext.Duration("timeout"),
SshdHost: clicontext.String("host"),
}
if c.Runner != types.RunnerTypeEnvdServer {
startOptions.EngineSource = envd.EngineSource{
Expand All @@ -204,7 +215,7 @@ func up(clicontext *cli.Context) error {
logrus.Debugf("container %s is running", res.Name)

logrus.Debugf("add entry %s to SSH config.", ctr)
hostname, err := c.GetSSHHostname()
hostname, err := c.GetSSHHostname(startOptions.SshdHost)
if err != nil {
return errors.Wrap(err, "failed to get the ssh hostname")
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/envd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (e dockerEngine) GenerateSSHConfig(name, iface, privateKeyPath string,
func (e dockerEngine) Attach(name, iface, privateKeyPath string,
startResult *StartResult) error {
opt := ssh.DefaultOptions()
opt.Server = iface
opt.PrivateKeyPath = privateKeyPath
opt.Port = startResult.SSHPort
sshClient, err := ssh.NewClient(opt)
Expand Down Expand Up @@ -363,7 +364,7 @@ func (e dockerEngine) StartEnvd(ctx context.Context, so StartOptions) (*StartRes
natPort := nat.Port(fmt.Sprintf("%d/tcp", envdconfig.SSHPortInContainer))
hostConfig.PortBindings[natPort] = []nat.PortBinding{
{
HostIP: localhost,
HostIP: so.SshdHost,
HostPort: strconv.Itoa(sshPortInHost),
},
}
Expand All @@ -384,7 +385,7 @@ func (e dockerEngine) StartEnvd(ctx context.Context, so StartOptions) (*StartRes
natPort := nat.Port(fmt.Sprintf("%d/tcp", envdconfig.JupyterPortInContainer))
hostConfig.PortBindings[natPort] = []nat.PortBinding{
{
HostIP: localhost,
HostIP: Localhost,
HostPort: strconv.Itoa(jupyterPortInHost),
},
}
Expand All @@ -400,7 +401,7 @@ func (e dockerEngine) StartEnvd(ctx context.Context, so StartOptions) (*StartRes
natPort := nat.Port(fmt.Sprintf("%d/tcp", envdconfig.RStudioServerPortInContainer))
hostConfig.PortBindings[natPort] = []nat.PortBinding{
{
HostIP: localhost,
HostIP: Localhost,
HostPort: strconv.Itoa(rStudioPortInHost),
},
}
Expand Down Expand Up @@ -631,11 +632,11 @@ func labels(name string, g ir.Graph,
res[types.ContainerLabelSSHPort] = strconv.Itoa(sshPortInHost)
if g.JupyterConfig != nil {
res[types.ContainerLabelJupyterAddr] =
fmt.Sprintf("http://%s:%d", localhost, jupyterPortInHost)
fmt.Sprintf("http://%s:%d", Localhost, jupyterPortInHost)
}
if g.RStudioServerConfig != nil {
res[types.ContainerLabelRStudioServerAddr] =
fmt.Sprintf("http://%s:%d", localhost, rstudioServerPortInHost)
fmt.Sprintf("http://%s:%d", Localhost, rstudioServerPortInHost)
}

return res
Expand Down
3 changes: 2 additions & 1 deletion pkg/envd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const (
localhost = "127.0.0.1"
Localhost = "127.0.0.1"
)

var (
Expand All @@ -37,6 +37,7 @@ type StartOptions struct {
NumGPU int
Timeout time.Duration
Forced bool
SshdHost string

EngineSource
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ func parsePyPICommands(lst string) ([]string, error) {
return pkgs, err
}

func (c Context) GetSSHHostname() (string, error) {
func (c Context) GetSSHHostname(sshdHost string) (string, error) {
if c.RunnerAddress == nil {
return "localhost", nil
return sshdHost, nil
}

// TODO(gaocegege): Check ENVD_SERVER_HOST.
Expand Down

0 comments on commit 7bf8c00

Please sign in to comment.