Skip to content

Commit a322548

Browse files
committed
Support of '--no-join' option (fix #6)
1 parent c707dca commit a322548

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

client.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,19 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
122122
ok = true
123123

124124
// checking if a container already exists for this user
125-
cmd := exec.Command("docker", "ps", "--filter=label=ssh2docker", "--filter=label=image:ubuntu", "--filter=label=user:nobody", "--quiet", "--no-trunc")
126-
buf, err := cmd.CombinedOutput()
127-
if err != nil {
128-
logrus.Warnf("docker ps ... failed: %v", err)
129-
continue
125+
existingContainer := ""
126+
if !c.Server.NoJoin {
127+
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")
128+
buf, err := cmd.CombinedOutput()
129+
if err != nil {
130+
logrus.Warnf("docker ps ... failed: %v", err)
131+
continue
132+
}
133+
existingContainer = strings.TrimSpace(string(buf))
130134
}
131-
existingContainer := strings.TrimSpace(string(buf))
135+
136+
var cmd *exec.Cmd
137+
var err error
132138

133139
// Opening Docker process
134140
if existingContainer != "" {

cmd/ssh2docker/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func main() {
8585
Usage: "'docker run' arguments",
8686
Value: "-it --rm",
8787
},
88+
cli.BoolFlag{
89+
Name: "no-join",
90+
Usage: "Do not join existing containers, always create new ones",
91+
},
8892
}
8993

9094
app.Action = Action
@@ -115,9 +119,10 @@ func Action(c *cli.Context) {
115119
server.AllowedImages = strings.Split(c.String("allowed-images"), ",")
116120
}
117121

118-
// Set defaults
122+
// Configure server
119123
server.DefaultShell = c.String("shell")
120124
server.DockerRunArgs = strings.Split(c.String("docker-run-args"), " ")
125+
server.NoJoin = c.Bool("no-join")
121126

122127
// Register the SSH host key
123128
hostKey := c.String("host-key")

server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Server struct {
1515
AllowedImages []string
1616
DefaultShell string
1717
DockerRunArgs []string
18+
NoJoin bool
1819
}
1920

2021
// NewServer initialize a new Server instance with default values

0 commit comments

Comments
 (0)