From 7bf8c00dfd8c34cc5552590b67b1e69eecaf5a51 Mon Sep 17 00:00:00 2001 From: nullday Date: Tue, 1 Nov 2022 12:08:47 +0800 Subject: [PATCH] feat: expose support listen addr (#1128) * Add listening_addr to expose fun Signed-off-by: nullday * Rename parameter Signed-off-by: nullday * feat: support cli arguements to specify listening port Signed-off-by: nullday Signed-off-by: nullday --- pkg/app/create.go | 8 +++++++- pkg/app/up.go | 15 +++++++++++++-- pkg/envd/docker.go | 11 ++++++----- pkg/envd/types.go | 3 ++- pkg/types/envd.go | 4 ++-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/app/create.go b/pkg/app/create.go index 678a973bb..5d9f79e28 100644 --- a/pkg/app/create.go +++ b/pkg/app/create.go @@ -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, } @@ -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, @@ -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") } diff --git a/pkg/app/up.go b/pkg/app/up.go index 5348e01d9..678979d04 100644 --- a/pkg/app/up.go +++ b/pkg/app/up.go @@ -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", @@ -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") @@ -177,7 +188,6 @@ 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, @@ -185,6 +195,7 @@ func up(clicontext *cli.Context) error { NumGPU: numGPU, Forced: clicontext.Bool("force"), Timeout: clicontext.Duration("timeout"), + SshdHost: clicontext.String("host"), } if c.Runner != types.RunnerTypeEnvdServer { startOptions.EngineSource = envd.EngineSource{ @@ -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") } diff --git a/pkg/envd/docker.go b/pkg/envd/docker.go index b3ea7b994..c621e51b3 100644 --- a/pkg/envd/docker.go +++ b/pkg/envd/docker.go @@ -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) @@ -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), }, } @@ -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), }, } @@ -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), }, } @@ -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 diff --git a/pkg/envd/types.go b/pkg/envd/types.go index 3fd1e05b2..886a767bd 100644 --- a/pkg/envd/types.go +++ b/pkg/envd/types.go @@ -23,7 +23,7 @@ import ( ) const ( - localhost = "127.0.0.1" + Localhost = "127.0.0.1" ) var ( @@ -37,6 +37,7 @@ type StartOptions struct { NumGPU int Timeout time.Duration Forced bool + SshdHost string EngineSource } diff --git a/pkg/types/envd.go b/pkg/types/envd.go index a503756eb..c9422b097 100644 --- a/pkg/types/envd.go +++ b/pkg/types/envd.go @@ -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.