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/http"
|
2015-04-01 18:39:37 -04:00
|
|
|
"path/filepath"
|
2014-03-28 19:21:55 -04:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
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"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/utils"
|
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
|
2015-04-22 08:06:58 -04:00
|
|
|
|
|
|
|
// configFile has the client configuration file
|
|
|
|
configFile *cliconfig.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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-09 14:33:06 -04:00
|
|
|
func (cli *DockerCli) Out() io.Writer {
|
|
|
|
return cli.out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) Err() io.Writer {
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-05-23 11:57:50 -04:00
|
|
|
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'.", args[0])
|
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
|
|
|
}
|
|
|
|
|
2015-04-22 02:45:18 -04:00
|
|
|
// Subcmd is a subcommand of the main "docker" command.
|
|
|
|
// A subcommand represents an action that can be performed
|
|
|
|
// from the Docker command line client.
|
|
|
|
//
|
2015-05-13 22:45:01 -04:00
|
|
|
// Multiple subcommand synopses may be provided with one 'Usage' line being
|
|
|
|
// printed for each in the following way:
|
|
|
|
//
|
|
|
|
// Usage: docker <subcmd-name> [OPTIONS] <synopsis 0>
|
|
|
|
// docker <subcmd-name> [OPTIONS] <synopsis 1>
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// If no undeprecated flags are added to the returned FlagSet, "[OPTIONS]" will
|
|
|
|
// not be included on the usage synopsis lines. If no synopses are given, only
|
|
|
|
// one usage synopsis line will be printed with nothing following the
|
|
|
|
// "[OPTIONS]" section
|
|
|
|
//
|
2015-04-22 02:45:18 -04:00
|
|
|
// To see all available subcommands, run "docker --help".
|
2015-05-13 22:45:01 -04:00
|
|
|
func (cli *DockerCli) Subcmd(name string, synopses []string, description string, exitOnError bool) *flag.FlagSet {
|
2014-11-24 11:34:13 -05:00
|
|
|
var errorHandling flag.ErrorHandling
|
|
|
|
if exitOnError {
|
|
|
|
errorHandling = flag.ExitOnError
|
|
|
|
} else {
|
|
|
|
errorHandling = flag.ContinueOnError
|
|
|
|
}
|
2015-05-13 22:45:01 -04:00
|
|
|
|
2014-11-24 11:34:13 -05:00
|
|
|
flags := flag.NewFlagSet(name, errorHandling)
|
2015-05-13 22:45:01 -04:00
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
flags.Usage = func() {
|
2015-03-31 03:11:03 -04:00
|
|
|
flags.ShortUsage()
|
|
|
|
flags.PrintDefaults()
|
|
|
|
}
|
2015-05-13 22:45:01 -04:00
|
|
|
|
2015-03-31 03:11:03 -04:00
|
|
|
flags.ShortUsage = func() {
|
2014-08-24 21:53:31 -04:00
|
|
|
options := ""
|
|
|
|
if flags.FlagCountUndeprecated() > 0 {
|
2015-03-11 06:48:57 -04:00
|
|
|
options = " [OPTIONS]"
|
2014-08-24 21:53:31 -04:00
|
|
|
}
|
2015-05-13 22:45:01 -04:00
|
|
|
|
|
|
|
if len(synopses) == 0 {
|
|
|
|
synopses = []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow for multiple command usage synopses.
|
|
|
|
for i, synopsis := range synopses {
|
|
|
|
lead := "\t"
|
|
|
|
if i == 0 {
|
|
|
|
// First line needs the word 'Usage'.
|
|
|
|
lead = "Usage:\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
if synopsis != "" {
|
|
|
|
synopsis = " " + synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(flags.Out(), "\n%sdocker %s%s%s", lead, name, options, synopsis)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(flags.Out(), "\n\n%s\n", description)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
2015-05-13 22:45:01 -04:00
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2015-04-22 02:45:18 -04:00
|
|
|
// CheckTtyInput checks if we are trying to attach to a container tty
|
|
|
|
// from a non-tty client input stream, and if so, returns an error.
|
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,
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
utils.ConfigureTCPTransport(tr, proto, addr)
|
2014-10-12 22:26:42 -04:00
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
configFile, e := cliconfig.Load(filepath.Join(homedir.Get(), ".docker"))
|
2015-04-01 18:39:37 -04:00
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
|
|
|
|
}
|
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
return &DockerCli{
|
2014-09-10 09:35:48 -04:00
|
|
|
proto: proto,
|
|
|
|
addr: addr,
|
2015-04-01 18:39:37 -04:00
|
|
|
configFile: configFile,
|
2014-09-10 09:35:48 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|