From a322548249a136eb9c0910716aa23659c5885fd2 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sat, 26 Sep 2015 01:31:06 +0200 Subject: [PATCH] Support of '--no-join' option (fix #6) --- client.go | 18 ++++++++++++------ cmd/ssh2docker/main.go | 7 ++++++- server.go | 1 + 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 78f5039..0de7dd2 100644 --- a/client.go +++ b/client.go @@ -122,13 +122,19 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh ok = true // checking if a container already exists for this user - cmd := exec.Command("docker", "ps", "--filter=label=ssh2docker", "--filter=label=image:ubuntu", "--filter=label=user:nobody", "--quiet", "--no-trunc") - buf, err := cmd.CombinedOutput() - if err != nil { - logrus.Warnf("docker ps ... failed: %v", err) - continue + existingContainer := "" + if !c.Server.NoJoin { + cmd := exec.Command("docker", "ps", "--filter=label=ssh2docker", fmt.Sprintf("--filter=label=image:%s", c.ImageName), fmt.Sprintf("--filter=label=user:%s", c.RemoteUser), "--quiet", "--no-trunc") + buf, err := cmd.CombinedOutput() + if err != nil { + logrus.Warnf("docker ps ... failed: %v", err) + continue + } + existingContainer = strings.TrimSpace(string(buf)) } - existingContainer := strings.TrimSpace(string(buf)) + + var cmd *exec.Cmd + var err error // Opening Docker process if existingContainer != "" { diff --git a/cmd/ssh2docker/main.go b/cmd/ssh2docker/main.go index a93ac44..8eedb49 100644 --- a/cmd/ssh2docker/main.go +++ b/cmd/ssh2docker/main.go @@ -85,6 +85,10 @@ func main() { Usage: "'docker run' arguments", Value: "-it --rm", }, + cli.BoolFlag{ + Name: "no-join", + Usage: "Do not join existing containers, always create new ones", + }, } app.Action = Action @@ -115,9 +119,10 @@ func Action(c *cli.Context) { server.AllowedImages = strings.Split(c.String("allowed-images"), ",") } - // Set defaults + // Configure server server.DefaultShell = c.String("shell") server.DockerRunArgs = strings.Split(c.String("docker-run-args"), " ") + server.NoJoin = c.Bool("no-join") // Register the SSH host key hostKey := c.String("host-key") diff --git a/server.go b/server.go index 04ffcb8..e04489e 100644 --- a/server.go +++ b/server.go @@ -15,6 +15,7 @@ type Server struct { AllowedImages []string DefaultShell string DockerRunArgs []string + NoJoin bool } // NewServer initialize a new Server instance with default values