2014-03-28 19:21:55 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2014-12-05 19:50:56 -05:00
|
|
|
"errors"
|
2014-03-28 19:21:55 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-10-12 22:26:42 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2014-03-28 19:21:55 -04:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2014-10-12 22:26:42 -04:00
|
|
|
"time"
|
2014-03-28 19:21:55 -04:00
|
|
|
|
2015-02-06 13:18:49 -05:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2014-07-24 18:19:50 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/docker/registry"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// DockerCli represents the docker command line client.
|
|
|
|
// Instances of the client can be returned from NewDockerCli.
|
2014-08-10 00:31:59 -04:00
|
|
|
type DockerCli struct {
|
2015-04-14 09:14:33 -04:00
|
|
|
// proto holds the client protocol i.e. unix.
|
|
|
|
proto string
|
|
|
|
// addr holds the client address.
|
|
|
|
addr string
|
|
|
|
// configFile holds the configuration file (instance of registry.ConfigFile).
|
2014-08-10 00:31:59 -04:00
|
|
|
configFile *registry.ConfigFile
|
2015-04-14 09:14:33 -04:00
|
|
|
// in holds the input stream and closer (io.ReadCloser) for the client.
|
|
|
|
in io.ReadCloser
|
|
|
|
// out holds the output stream (io.Writer) for the client.
|
|
|
|
out io.Writer
|
|
|
|
// err holds the error stream (io.Writer) for the client.
|
|
|
|
err io.Writer
|
|
|
|
// keyFile holds the key file as a string.
|
|
|
|
keyFile string
|
|
|
|
// tlsConfig holds the TLS configuration for the client, and will
|
|
|
|
// set the scheme to https in NewDockerCli if present.
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
// scheme holds the scheme of the client i.e. https.
|
|
|
|
scheme string
|
|
|
|
// inFd holds the file descriptor of the client's STDIN (if valid).
|
2014-09-10 09:35:48 -04:00
|
|
|
inFd uintptr
|
2015-04-14 09:14:33 -04:00
|
|
|
// outFd holds file descriptor of the client's STDOUT (if valid).
|
2014-09-10 09:35:48 -04:00
|
|
|
outFd uintptr
|
2015-04-14 09:14:33 -04:00
|
|
|
// isTerminalIn indicates whether the client's STDIN is a TTY
|
2014-09-10 09:35:48 -04:00
|
|
|
isTerminalIn bool
|
2015-04-14 09:14:33 -04:00
|
|
|
// isTerminalOut dindicates whether the client's STDOUT is a TTY
|
2014-09-10 09:35:48 -04:00
|
|
|
isTerminalOut bool
|
2015-04-14 09:14:33 -04:00
|
|
|
// transport holds the client transport instance.
|
|
|
|
transport *http.Transport
|
2014-08-10 00:31:59 -04:00
|
|
|
}
|
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:44:15 -04:00
|
|
|
func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) {
|
|
|
|
camelArgs := make([]string, len(args))
|
|
|
|
for i, s := range args {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
|
2014-05-19 17:18:53 -04:00
|
|
|
}
|
2014-09-16 13:44:15 -04:00
|
|
|
methodName := "Cmd" + strings.Join(camelArgs, "")
|
2014-03-28 19:21:55 -04:00
|
|
|
method := reflect.ValueOf(cli).MethodByName(methodName)
|
|
|
|
if !method.IsValid() {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return method.Interface().(func(...string) error), true
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// Cmd executes the specified command.
|
2014-08-10 00:33:19 -04:00
|
|
|
func (cli *DockerCli) Cmd(args ...string) error {
|
2014-09-16 13:44:15 -04:00
|
|
|
if len(args) > 1 {
|
|
|
|
method, exists := cli.getMethod(args[:2]...)
|
|
|
|
if exists {
|
|
|
|
return method(args[2:]...)
|
|
|
|
}
|
|
|
|
}
|
2014-03-28 19:21:55 -04:00
|
|
|
if len(args) > 0 {
|
|
|
|
method, exists := cli.getMethod(args[0])
|
|
|
|
if !exists {
|
2014-10-15 17:14:12 -04:00
|
|
|
fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
|
|
|
|
os.Exit(1)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
return method(args[1:]...)
|
|
|
|
}
|
2014-11-03 11:46:01 -05:00
|
|
|
return cli.CmdHelp()
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
|
2014-11-24 11:34:13 -05:00
|
|
|
func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bool) *flag.FlagSet {
|
|
|
|
var errorHandling flag.ErrorHandling
|
|
|
|
if exitOnError {
|
|
|
|
errorHandling = flag.ExitOnError
|
|
|
|
} else {
|
|
|
|
errorHandling = flag.ContinueOnError
|
|
|
|
}
|
|
|
|
flags := flag.NewFlagSet(name, errorHandling)
|
2014-03-28 19:21:55 -04:00
|
|
|
flags.Usage = func() {
|
2014-08-24 21:53:31 -04:00
|
|
|
options := ""
|
2015-03-11 06:48:57 -04:00
|
|
|
if signature != "" {
|
|
|
|
signature = " " + signature
|
|
|
|
}
|
2014-08-24 21:53:31 -04:00
|
|
|
if flags.FlagCountUndeprecated() > 0 {
|
2015-03-11 06:48:57 -04:00
|
|
|
options = " [OPTIONS]"
|
2014-08-24 21:53:31 -04:00
|
|
|
}
|
2015-03-11 06:48:57 -04:00
|
|
|
fmt.Fprintf(cli.out, "\nUsage: docker %s%s%s\n\n%s\n\n", name, options, signature, description)
|
2014-10-15 17:14:12 -04:00
|
|
|
flags.SetOutput(cli.out)
|
2014-03-28 19:21:55 -04:00
|
|
|
flags.PrintDefaults()
|
2014-10-15 17:14:12 -04:00
|
|
|
os.Exit(0)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) LoadConfigFile() (err error) {
|
2015-02-06 13:18:49 -05:00
|
|
|
cli.configFile, err = registry.LoadConfig(homedir.Get())
|
2014-03-28 19:21:55 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(cli.err, "WARNING: %s\n", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-05 19:50:56 -05:00
|
|
|
func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
|
|
|
|
// In order to attach to a container tty, input stream for the client must
|
|
|
|
// be a tty itself: redirecting or piping the client standard input is
|
|
|
|
// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
|
|
|
|
if ttyMode && attachStdin && !cli.isTerminalIn {
|
|
|
|
return errors.New("cannot enable tty mode on non tty input")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
|
|
|
// The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
|
|
|
|
// is set the client scheme will be set to https.
|
|
|
|
// The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
|
2015-01-29 16:46:12 -05:00
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer, keyFile string, proto, addr string, tlsConfig *tls.Config) *DockerCli {
|
2014-03-28 19:21:55 -04:00
|
|
|
var (
|
2014-09-10 09:35:48 -04:00
|
|
|
inFd uintptr
|
|
|
|
outFd uintptr
|
|
|
|
isTerminalIn = false
|
|
|
|
isTerminalOut = false
|
|
|
|
scheme = "http"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2014-05-02 15:49:12 -04:00
|
|
|
if tlsConfig != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
2014-03-28 19:21:55 -04:00
|
|
|
if in != nil {
|
2015-03-06 20:04:35 -05:00
|
|
|
inFd, isTerminalIn = term.GetFdInfo(in)
|
2014-09-10 09:35:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if out != nil {
|
2015-03-06 20:04:35 -05:00
|
|
|
outFd, isTerminalOut = term.GetFdInfo(out)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = out
|
|
|
|
}
|
2014-09-10 09:35:48 -04:00
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// The transport is created here for reuse during the client session.
|
2014-10-12 22:26:42 -04:00
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
2014-10-08 14:18:42 -04:00
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// Why 32? See https://github.com/docker/docker/pull/8035.
|
2014-11-12 22:21:18 -05:00
|
|
|
timeout := 32 * time.Second
|
2014-10-12 22:26:42 -04:00
|
|
|
if proto == "unix" {
|
2015-04-14 09:14:33 -04:00
|
|
|
// No need for compression in local communications.
|
2014-10-12 22:26:42 -04:00
|
|
|
tr.DisableCompression = true
|
2014-11-14 14:31:52 -05:00
|
|
|
tr.Dial = func(_, _ string) (net.Conn, error) {
|
2014-11-12 22:21:18 -05:00
|
|
|
return net.DialTimeout(proto, addr, timeout)
|
2014-10-08 14:18:42 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-12 11:38:52 -05:00
|
|
|
tr.Proxy = http.ProxyFromEnvironment
|
2014-11-12 22:21:18 -05:00
|
|
|
tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
|
2014-10-12 22:26:42 -04:00
|
|
|
}
|
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
return &DockerCli{
|
2014-09-10 09:35:48 -04:00
|
|
|
proto: proto,
|
|
|
|
addr: addr,
|
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
err: err,
|
2015-01-29 16:46:12 -05:00
|
|
|
keyFile: keyFile,
|
2014-09-10 09:35:48 -04:00
|
|
|
inFd: inFd,
|
|
|
|
outFd: outFd,
|
|
|
|
isTerminalIn: isTerminalIn,
|
|
|
|
isTerminalOut: isTerminalOut,
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
scheme: scheme,
|
2014-10-12 22:26:42 -04:00
|
|
|
transport: tr,
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
}
|