Skip to content

Commit

Permalink
Handling custom environment variables, user and command in password s…
Browse files Browse the repository at this point in the history
…cript (#57)
  • Loading branch information
moul committed Oct 13, 2015
1 parent 5748a5b commit 23a4a1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ $ docker run --privileged -v /var/lib/docker:/var/lib/docker -it --rm -p 2222:22

### master (unreleased)

* Handling custom environment variables when running a password hook script ([#57](https://github.com/moul/ssh2docker/issues/57))
* Handling custom environment variables, user and command in password script ([#57](https://github.com/moul/ssh2docker/issues/57))
* Replacing "_" by "/" on default image name to handle ControlMaster on clients
* Support of `--banner` option ([#26](https://github.com/moul/ssh2docker/issues/26))
* Add a not-yet-implemented warning for exec ([#51](https://github.com/moul/ssh2docker/issues/51))
Expand Down
15 changes: 14 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type ClientConfig struct {
Allowed bool `json:"allowed",omitempty`
Env Environment `json:"env",omitempty`
IsLocal bool `json:"is_local",omitempty`
Command []string `json:"command",omitempty`
User string `json:"user",omitempty`
}

// NewClient initializes a new client
Expand All @@ -54,6 +56,7 @@ func NewClient(conn *ssh.ServerConn, chans <-chan ssh.NewChannel, reqs <-chan *s
ImageName: strings.Replace(conn.User(), "_", "/", -1),
RemoteUser: "anonymous",
Env: Environment{},
Command: make([]string, 0),
},
}

Expand Down Expand Up @@ -149,6 +152,7 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
existingContainer := ""
if !c.Server.NoJoin {
cmd := exec.Command("docker", "ps", "--filter=label=ssh2docker", fmt.Sprintf("--filter=label=image=%s", c.Config.ImageName), fmt.Sprintf("--filter=label=user=%s", c.Config.RemoteUser), "--quiet", "--no-trunc")
cmd.Env = c.Config.Env.List()
buf, err := cmd.CombinedOutput()
if err != nil {
logrus.Warnf("docker ps ... failed: %v", err)
Expand All @@ -163,12 +167,21 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
args := []string{"exec", "-it", existingContainer, c.Server.DefaultShell}
logrus.Debugf("Executing 'docker %s'", strings.Join(args, " "))
cmd = exec.Command("docker", args...)
cmd.Env = c.Config.Env.List()
} else {
// Creating and attaching to a new container
args := []string{"run"}
args = append(args, c.Server.DockerRunArgs...)
args = append(args, "--label=ssh2docker", fmt.Sprintf("--label=user=%s", c.Config.RemoteUser), fmt.Sprintf("--label=image=%s", c.Config.ImageName))
args = append(args, c.Config.ImageName, c.Server.DefaultShell)
if c.Config.User != "" {
args = append(args, "-u", c.Config.User)
}
args = append(args, c.Config.ImageName)
if c.Config.Command != nil {
args = append(args, c.Config.Command...)
} else {
args = append(args, c.Server.DefaultShell)
}
logrus.Debugf("Executing 'docker %s'", strings.Join(args, " "))
cmd = exec.Command("docker", args...)
cmd.Env = c.Config.Env.List()
Expand Down

0 comments on commit 23a4a1a

Please sign in to comment.