2017-01-25 19:27:47 +01:00
|
|
|
// Copyright 2015 go-dockerclient authors. All rights reserved.
|
2016-01-29 20:53:56 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Exec is the type representing a `docker exec` instance and containing the
|
|
|
|
// instance ID
|
|
|
|
type Exec struct {
|
|
|
|
ID string `json:"Id,omitempty" yaml:"Id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateExecOptions specify parameters to the CreateExecContainer function.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/1KSIb7 for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
type CreateExecOptions struct {
|
2017-01-25 19:27:47 +01:00
|
|
|
AttachStdin bool `json:"AttachStdin,omitempty" yaml:"AttachStdin,omitempty"`
|
|
|
|
AttachStdout bool `json:"AttachStdout,omitempty" yaml:"AttachStdout,omitempty"`
|
|
|
|
AttachStderr bool `json:"AttachStderr,omitempty" yaml:"AttachStderr,omitempty"`
|
|
|
|
Tty bool `json:"Tty,omitempty" yaml:"Tty,omitempty"`
|
|
|
|
Cmd []string `json:"Cmd,omitempty" yaml:"Cmd,omitempty"`
|
|
|
|
Container string `json:"Container,omitempty" yaml:"Container,omitempty"`
|
|
|
|
User string `json:"User,omitempty" yaml:"User,omitempty"`
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateExec sets up an exec instance in a running container `id`, returning the exec
|
|
|
|
// instance, or an error in case of failure.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/1KSIb7 for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
func (c *Client) CreateExec(opts CreateExecOptions) (*Exec, error) {
|
|
|
|
path := fmt.Sprintf("/containers/%s/exec", opts.Container)
|
2017-01-25 19:27:47 +01:00
|
|
|
resp, err := c.do("POST", path, doOptions{data: opts})
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
|
|
|
|
return nil, &NoSuchContainer{ID: opts.Container}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var exec Exec
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&exec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &exec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartExecOptions specify parameters to the StartExecContainer function.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/iQCnto for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
type StartExecOptions struct {
|
2017-01-25 19:27:47 +01:00
|
|
|
Detach bool `json:"Detach,omitempty" yaml:"Detach,omitempty"`
|
|
|
|
|
|
|
|
Tty bool `json:"Tty,omitempty" yaml:"Tty,omitempty"`
|
|
|
|
|
2016-01-29 20:53:56 +01:00
|
|
|
InputStream io.Reader `qs:"-"`
|
|
|
|
OutputStream io.Writer `qs:"-"`
|
|
|
|
ErrorStream io.Writer `qs:"-"`
|
|
|
|
|
|
|
|
// Use raw terminal? Usually true when the container contains a TTY.
|
|
|
|
RawTerminal bool `qs:"-"`
|
|
|
|
|
|
|
|
// If set, after a successful connect, a sentinel will be sent and then the
|
|
|
|
// client will block on receive before continuing.
|
|
|
|
//
|
|
|
|
// It must be an unbuffered channel. Using a buffered channel can lead
|
|
|
|
// to unexpected behavior.
|
|
|
|
Success chan struct{} `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartExec starts a previously set up exec instance id. If opts.Detach is
|
|
|
|
// true, it returns after starting the exec command. Otherwise, it sets up an
|
|
|
|
// interactive session with the exec command.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/iQCnto for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
func (c *Client) StartExec(id string, opts StartExecOptions) error {
|
|
|
|
cw, err := c.StartExecNonBlocking(id, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cw != nil {
|
|
|
|
return cw.Wait()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartExecNonBlocking starts a previously set up exec instance id. If opts.Detach is
|
|
|
|
// true, it returns after starting the exec command. Otherwise, it sets up an
|
|
|
|
// interactive session with the exec command.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/iQCnto for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
func (c *Client) StartExecNonBlocking(id string, opts StartExecOptions) (CloseWaiter, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, &NoSuchExec{ID: id}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/exec/%s/start", id)
|
|
|
|
|
|
|
|
if opts.Detach {
|
2017-01-25 19:27:47 +01:00
|
|
|
resp, err := c.do("POST", path, doOptions{data: opts})
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
|
|
|
|
return nil, &NoSuchExec{ID: id}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.hijack("POST", path, hijackOptions{
|
|
|
|
success: opts.Success,
|
|
|
|
setRawTerminal: opts.RawTerminal,
|
|
|
|
in: opts.InputStream,
|
|
|
|
stdout: opts.OutputStream,
|
|
|
|
stderr: opts.ErrorStream,
|
|
|
|
data: opts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResizeExecTTY resizes the tty session used by the exec command id. This API
|
|
|
|
// is valid only if Tty was specified as part of creating and starting the exec
|
|
|
|
// command.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/e1JpsA for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
func (c *Client) ResizeExecTTY(id string, height, width int) error {
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("h", strconv.Itoa(height))
|
|
|
|
params.Set("w", strconv.Itoa(width))
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/exec/%s/resize?%s", id, params.Encode())
|
|
|
|
resp, err := c.do("POST", path, doOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecProcessConfig is a type describing the command associated to a Exec
|
|
|
|
// instance. It's used in the ExecInspect type.
|
|
|
|
type ExecProcessConfig struct {
|
2016-10-24 08:15:30 +02:00
|
|
|
Privileged bool `json:"privileged,omitempty" yaml:"privileged,omitempty"`
|
2017-01-25 19:27:47 +01:00
|
|
|
User string `json:"user,omitempty" yaml:"user,omitempty"`
|
2016-01-29 20:53:56 +01:00
|
|
|
Tty bool `json:"tty,omitempty" yaml:"tty,omitempty"`
|
|
|
|
EntryPoint string `json:"entrypoint,omitempty" yaml:"entrypoint,omitempty"`
|
|
|
|
Arguments []string `json:"arguments,omitempty" yaml:"arguments,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecInspect is a type with details about a exec instance, including the
|
|
|
|
// exit code if the command has finished running. It's returned by a api
|
|
|
|
// call to /exec/(id)/json
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/gPtX9R for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
type ExecInspect struct {
|
|
|
|
ID string `json:"ID,omitempty" yaml:"ID,omitempty"`
|
2016-10-24 08:15:30 +02:00
|
|
|
Running bool `json:"Running,omitempty" yaml:"Running,omitempty"`
|
2017-01-25 19:27:47 +01:00
|
|
|
ExitCode int `json:"ExitCode,omitempty" yaml:"ExitCode,omitempty"`
|
2016-01-29 20:53:56 +01:00
|
|
|
OpenStdin bool `json:"OpenStdin,omitempty" yaml:"OpenStdin,omitempty"`
|
|
|
|
OpenStderr bool `json:"OpenStderr,omitempty" yaml:"OpenStderr,omitempty"`
|
|
|
|
OpenStdout bool `json:"OpenStdout,omitempty" yaml:"OpenStdout,omitempty"`
|
|
|
|
ProcessConfig ExecProcessConfig `json:"ProcessConfig,omitempty" yaml:"ProcessConfig,omitempty"`
|
|
|
|
Container Container `json:"Container,omitempty" yaml:"Container,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InspectExec returns low-level information about the exec command id.
|
|
|
|
//
|
2017-01-25 19:27:47 +01:00
|
|
|
// See https://goo.gl/gPtX9R for more details
|
2016-01-29 20:53:56 +01:00
|
|
|
func (c *Client) InspectExec(id string) (*ExecInspect, error) {
|
|
|
|
path := fmt.Sprintf("/exec/%s/json", id)
|
|
|
|
resp, err := c.do("GET", path, doOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
|
|
|
|
return nil, &NoSuchExec{ID: id}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var exec ExecInspect
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&exec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &exec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoSuchExec is the error returned when a given exec instance does not exist.
|
|
|
|
type NoSuchExec struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *NoSuchExec) Error() string {
|
|
|
|
return "No such exec instance: " + err.ID
|
|
|
|
}
|