Everything has been ported to the client library :tada:
Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -1,11 +1,9 @@ |
| 1 | 1 |
package client |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "crypto/tls" |
|
| 5 | 4 |
"errors" |
| 6 | 5 |
"fmt" |
| 7 | 6 |
"io" |
| 8 |
- "net/http" |
|
| 9 | 7 |
"os" |
| 10 | 8 |
"runtime" |
| 11 | 9 |
|
| ... | ... |
@@ -44,22 +42,6 @@ type DockerCli struct {
|
| 44 | 44 |
isTerminalOut bool |
| 45 | 45 |
// client is the http client that performs all API operations |
| 46 | 46 |
client apiClient |
| 47 |
- |
|
| 48 |
- // DEPRECATED OPTIONS TO MAKE THE CLIENT COMPILE |
|
| 49 |
- // TODO: Remove |
|
| 50 |
- // proto holds the client protocol i.e. unix. |
|
| 51 |
- proto string |
|
| 52 |
- // addr holds the client address. |
|
| 53 |
- addr string |
|
| 54 |
- // basePath holds the path to prepend to the requests |
|
| 55 |
- basePath string |
|
| 56 |
- // tlsConfig holds the TLS configuration for the client, and will |
|
| 57 |
- // set the scheme to https in NewDockerCli if present. |
|
| 58 |
- tlsConfig *tls.Config |
|
| 59 |
- // scheme holds the scheme of the client i.e. https. |
|
| 60 |
- scheme string |
|
| 61 |
- // transport holds the client transport instance. |
|
| 62 |
- transport *http.Transport |
|
| 63 | 47 |
} |
| 64 | 48 |
|
| 65 | 49 |
// Initialize calls the init function that will setup the configuration for the client |
| ... | ... |
@@ -126,12 +108,6 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF |
| 126 | 126 |
} |
| 127 | 127 |
cli.client = client |
| 128 | 128 |
|
| 129 |
- // FIXME: Deprecated, only to keep the old code running. |
|
| 130 |
- cli.transport = client.HTTPClient.Transport.(*http.Transport) |
|
| 131 |
- cli.basePath = client.BasePath |
|
| 132 |
- cli.addr = client.Addr |
|
| 133 |
- cli.scheme = client.Scheme |
|
| 134 |
- |
|
| 135 | 129 |
if cli.in != nil {
|
| 136 | 130 |
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in) |
| 137 | 131 |
} |
| ... | ... |
@@ -1,22 +1,11 @@ |
| 1 | 1 |
package client |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "crypto/tls" |
|
| 5 |
- "errors" |
|
| 6 |
- "fmt" |
|
| 7 | 4 |
"io" |
| 8 |
- "net" |
|
| 9 |
- "net/http" |
|
| 10 |
- "net/http/httputil" |
|
| 11 | 5 |
"os" |
| 12 |
- "runtime" |
|
| 13 |
- "strings" |
|
| 14 |
- "time" |
|
| 15 | 6 |
|
| 16 | 7 |
"github.com/Sirupsen/logrus" |
| 17 |
- "github.com/docker/docker/api" |
|
| 18 | 8 |
"github.com/docker/docker/api/types" |
| 19 |
- "github.com/docker/docker/dockerversion" |
|
| 20 | 9 |
"github.com/docker/docker/pkg/stdcopy" |
| 21 | 10 |
"github.com/docker/docker/pkg/term" |
| 22 | 11 |
) |
| ... | ... |
@@ -87,239 +76,3 @@ func (cli *DockerCli) holdHijackedConnection(setRawTerminal bool, inputStream io |
| 87 | 87 |
|
| 88 | 88 |
return nil |
| 89 | 89 |
} |
| 90 |
- |
|
| 91 |
-type tlsClientCon struct {
|
|
| 92 |
- *tls.Conn |
|
| 93 |
- rawConn net.Conn |
|
| 94 |
-} |
|
| 95 |
- |
|
| 96 |
-func (c *tlsClientCon) CloseWrite() error {
|
|
| 97 |
- // Go standard tls.Conn doesn't provide the CloseWrite() method so we do it |
|
| 98 |
- // on its underlying connection. |
|
| 99 |
- if cwc, ok := c.rawConn.(interface {
|
|
| 100 |
- CloseWrite() error |
|
| 101 |
- }); ok {
|
|
| 102 |
- return cwc.CloseWrite() |
|
| 103 |
- } |
|
| 104 |
- return nil |
|
| 105 |
-} |
|
| 106 |
- |
|
| 107 |
-func tlsDial(network, addr string, config *tls.Config) (net.Conn, error) {
|
|
| 108 |
- return tlsDialWithDialer(new(net.Dialer), network, addr, config) |
|
| 109 |
-} |
|
| 110 |
- |
|
| 111 |
-// We need to copy Go's implementation of tls.Dial (pkg/cryptor/tls/tls.go) in |
|
| 112 |
-// order to return our custom tlsClientCon struct which holds both the tls.Conn |
|
| 113 |
-// object _and_ its underlying raw connection. The rationale for this is that |
|
| 114 |
-// we need to be able to close the write end of the connection when attaching, |
|
| 115 |
-// which tls.Conn does not provide. |
|
| 116 |
-func tlsDialWithDialer(dialer *net.Dialer, network, addr string, config *tls.Config) (net.Conn, error) {
|
|
| 117 |
- // We want the Timeout and Deadline values from dialer to cover the |
|
| 118 |
- // whole process: TCP connection and TLS handshake. This means that we |
|
| 119 |
- // also need to start our own timers now. |
|
| 120 |
- timeout := dialer.Timeout |
|
| 121 |
- |
|
| 122 |
- if !dialer.Deadline.IsZero() {
|
|
| 123 |
- deadlineTimeout := dialer.Deadline.Sub(time.Now()) |
|
| 124 |
- if timeout == 0 || deadlineTimeout < timeout {
|
|
| 125 |
- timeout = deadlineTimeout |
|
| 126 |
- } |
|
| 127 |
- } |
|
| 128 |
- |
|
| 129 |
- var errChannel chan error |
|
| 130 |
- |
|
| 131 |
- if timeout != 0 {
|
|
| 132 |
- errChannel = make(chan error, 2) |
|
| 133 |
- time.AfterFunc(timeout, func() {
|
|
| 134 |
- errChannel <- errors.New("")
|
|
| 135 |
- }) |
|
| 136 |
- } |
|
| 137 |
- |
|
| 138 |
- rawConn, err := dialer.Dial(network, addr) |
|
| 139 |
- if err != nil {
|
|
| 140 |
- return nil, err |
|
| 141 |
- } |
|
| 142 |
- // When we set up a TCP connection for hijack, there could be long periods |
|
| 143 |
- // of inactivity (a long running command with no output) that in certain |
|
| 144 |
- // network setups may cause ECONNTIMEOUT, leaving the client in an unknown |
|
| 145 |
- // state. Setting TCP KeepAlive on the socket connection will prohibit |
|
| 146 |
- // ECONNTIMEOUT unless the socket connection truly is broken |
|
| 147 |
- if tcpConn, ok := rawConn.(*net.TCPConn); ok {
|
|
| 148 |
- tcpConn.SetKeepAlive(true) |
|
| 149 |
- tcpConn.SetKeepAlivePeriod(30 * time.Second) |
|
| 150 |
- } |
|
| 151 |
- |
|
| 152 |
- colonPos := strings.LastIndex(addr, ":") |
|
| 153 |
- if colonPos == -1 {
|
|
| 154 |
- colonPos = len(addr) |
|
| 155 |
- } |
|
| 156 |
- hostname := addr[:colonPos] |
|
| 157 |
- |
|
| 158 |
- // If no ServerName is set, infer the ServerName |
|
| 159 |
- // from the hostname we're connecting to. |
|
| 160 |
- if config.ServerName == "" {
|
|
| 161 |
- // Make a copy to avoid polluting argument or default. |
|
| 162 |
- c := *config |
|
| 163 |
- c.ServerName = hostname |
|
| 164 |
- config = &c |
|
| 165 |
- } |
|
| 166 |
- |
|
| 167 |
- conn := tls.Client(rawConn, config) |
|
| 168 |
- |
|
| 169 |
- if timeout == 0 {
|
|
| 170 |
- err = conn.Handshake() |
|
| 171 |
- } else {
|
|
| 172 |
- go func() {
|
|
| 173 |
- errChannel <- conn.Handshake() |
|
| 174 |
- }() |
|
| 175 |
- |
|
| 176 |
- err = <-errChannel |
|
| 177 |
- } |
|
| 178 |
- |
|
| 179 |
- if err != nil {
|
|
| 180 |
- rawConn.Close() |
|
| 181 |
- return nil, err |
|
| 182 |
- } |
|
| 183 |
- |
|
| 184 |
- // This is Docker difference with standard's crypto/tls package: returned a |
|
| 185 |
- // wrapper which holds both the TLS and raw connections. |
|
| 186 |
- return &tlsClientCon{conn, rawConn}, nil
|
|
| 187 |
-} |
|
| 188 |
- |
|
| 189 |
-func (cli *DockerCli) dial() (net.Conn, error) {
|
|
| 190 |
- if cli.tlsConfig != nil && cli.proto != "unix" {
|
|
| 191 |
- // Notice this isn't Go standard's tls.Dial function |
|
| 192 |
- return tlsDial(cli.proto, cli.addr, cli.tlsConfig) |
|
| 193 |
- } |
|
| 194 |
- return net.Dial(cli.proto, cli.addr) |
|
| 195 |
-} |
|
| 196 |
- |
|
| 197 |
-func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer, data interface{}) error {
|
|
| 198 |
- return cli.hijackWithContentType(method, path, "text/plain", setRawTerminal, in, stdout, stderr, started, data) |
|
| 199 |
-} |
|
| 200 |
- |
|
| 201 |
-func (cli *DockerCli) hijackWithContentType(method, path, contentType string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer, data interface{}) error {
|
|
| 202 |
- defer func() {
|
|
| 203 |
- if started != nil {
|
|
| 204 |
- close(started) |
|
| 205 |
- } |
|
| 206 |
- }() |
|
| 207 |
- |
|
| 208 |
- params, err := cli.encodeData(data) |
|
| 209 |
- if err != nil {
|
|
| 210 |
- return err |
|
| 211 |
- } |
|
| 212 |
- req, err := http.NewRequest(method, fmt.Sprintf("%s/v%s%s", cli.basePath, api.Version, path), params)
|
|
| 213 |
- if err != nil {
|
|
| 214 |
- return err |
|
| 215 |
- } |
|
| 216 |
- |
|
| 217 |
- // Add CLI Config's HTTP Headers BEFORE we set the Docker headers |
|
| 218 |
- // then the user can't change OUR headers |
|
| 219 |
- for k, v := range cli.configFile.HTTPHeaders {
|
|
| 220 |
- req.Header.Set(k, v) |
|
| 221 |
- } |
|
| 222 |
- |
|
| 223 |
- req.Header.Set("User-Agent", "Docker-Client/"+dockerversion.Version+" ("+runtime.GOOS+")")
|
|
| 224 |
- req.Header.Set("Content-Type", contentType)
|
|
| 225 |
- req.Header.Set("Connection", "Upgrade")
|
|
| 226 |
- req.Header.Set("Upgrade", "tcp")
|
|
| 227 |
- req.Host = cli.addr |
|
| 228 |
- |
|
| 229 |
- dial, err := cli.dial() |
|
| 230 |
- if err != nil {
|
|
| 231 |
- if strings.Contains(err.Error(), "connection refused") {
|
|
| 232 |
- return fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
|
| 233 |
- } |
|
| 234 |
- return err |
|
| 235 |
- } |
|
| 236 |
- |
|
| 237 |
- // When we set up a TCP connection for hijack, there could be long periods |
|
| 238 |
- // of inactivity (a long running command with no output) that in certain |
|
| 239 |
- // network setups may cause ECONNTIMEOUT, leaving the client in an unknown |
|
| 240 |
- // state. Setting TCP KeepAlive on the socket connection will prohibit |
|
| 241 |
- // ECONNTIMEOUT unless the socket connection truly is broken |
|
| 242 |
- if tcpConn, ok := dial.(*net.TCPConn); ok {
|
|
| 243 |
- tcpConn.SetKeepAlive(true) |
|
| 244 |
- tcpConn.SetKeepAlivePeriod(30 * time.Second) |
|
| 245 |
- } |
|
| 246 |
- |
|
| 247 |
- clientconn := httputil.NewClientConn(dial, nil) |
|
| 248 |
- defer clientconn.Close() |
|
| 249 |
- |
|
| 250 |
- // Server hijacks the connection, error 'connection closed' expected |
|
| 251 |
- clientconn.Do(req) |
|
| 252 |
- |
|
| 253 |
- rwc, br := clientconn.Hijack() |
|
| 254 |
- defer rwc.Close() |
|
| 255 |
- |
|
| 256 |
- if started != nil {
|
|
| 257 |
- started <- rwc |
|
| 258 |
- } |
|
| 259 |
- |
|
| 260 |
- var oldState *term.State |
|
| 261 |
- if in != nil && setRawTerminal && cli.isTerminalIn && os.Getenv("NORAW") == "" {
|
|
| 262 |
- oldState, err = term.SetRawTerminal(cli.inFd) |
|
| 263 |
- if err != nil {
|
|
| 264 |
- return err |
|
| 265 |
- } |
|
| 266 |
- defer term.RestoreTerminal(cli.inFd, oldState) |
|
| 267 |
- } |
|
| 268 |
- |
|
| 269 |
- receiveStdout := make(chan error, 1) |
|
| 270 |
- if stdout != nil || stderr != nil {
|
|
| 271 |
- go func() {
|
|
| 272 |
- defer func() {
|
|
| 273 |
- if in != nil {
|
|
| 274 |
- if setRawTerminal && cli.isTerminalIn {
|
|
| 275 |
- term.RestoreTerminal(cli.inFd, oldState) |
|
| 276 |
- } |
|
| 277 |
- in.Close() |
|
| 278 |
- } |
|
| 279 |
- }() |
|
| 280 |
- |
|
| 281 |
- // When TTY is ON, use regular copy |
|
| 282 |
- if setRawTerminal && stdout != nil {
|
|
| 283 |
- _, err = io.Copy(stdout, br) |
|
| 284 |
- } else {
|
|
| 285 |
- _, err = stdcopy.StdCopy(stdout, stderr, br) |
|
| 286 |
- } |
|
| 287 |
- logrus.Debugf("[hijack] End of stdout")
|
|
| 288 |
- receiveStdout <- err |
|
| 289 |
- }() |
|
| 290 |
- } |
|
| 291 |
- |
|
| 292 |
- stdinDone := make(chan struct{})
|
|
| 293 |
- go func() {
|
|
| 294 |
- if in != nil {
|
|
| 295 |
- io.Copy(rwc, in) |
|
| 296 |
- logrus.Debugf("[hijack] End of stdin")
|
|
| 297 |
- } |
|
| 298 |
- |
|
| 299 |
- if conn, ok := rwc.(interface {
|
|
| 300 |
- CloseWrite() error |
|
| 301 |
- }); ok {
|
|
| 302 |
- if err := conn.CloseWrite(); err != nil {
|
|
| 303 |
- logrus.Debugf("Couldn't send EOF: %s", err)
|
|
| 304 |
- } |
|
| 305 |
- } |
|
| 306 |
- close(stdinDone) |
|
| 307 |
- }() |
|
| 308 |
- |
|
| 309 |
- select {
|
|
| 310 |
- case err := <-receiveStdout: |
|
| 311 |
- if err != nil {
|
|
| 312 |
- logrus.Debugf("Error receiveStdout: %s", err)
|
|
| 313 |
- return err |
|
| 314 |
- } |
|
| 315 |
- case <-stdinDone: |
|
| 316 |
- if stdout != nil || stderr != nil {
|
|
| 317 |
- if err := <-receiveStdout; err != nil {
|
|
| 318 |
- logrus.Debugf("Error receiveStdout: %s", err)
|
|
| 319 |
- return err |
|
| 320 |
- } |
|
| 321 |
- } |
|
| 322 |
- } |
|
| 323 |
- |
|
| 324 |
- return nil |
|
| 325 |
-} |
| ... | ... |
@@ -17,17 +17,17 @@ import ( |
| 17 | 17 |
// against a docker server. |
| 18 | 18 |
type Client struct {
|
| 19 | 19 |
// proto holds the client protocol i.e. unix. |
| 20 |
- Proto string |
|
| 20 |
+ proto string |
|
| 21 | 21 |
// addr holds the client address. |
| 22 |
- Addr string |
|
| 22 |
+ addr string |
|
| 23 | 23 |
// basePath holds the path to prepend to the requests |
| 24 |
- BasePath string |
|
| 24 |
+ basePath string |
|
| 25 | 25 |
// scheme holds the scheme of the client i.e. https. |
| 26 |
- Scheme string |
|
| 26 |
+ scheme string |
|
| 27 | 27 |
// tlsConfig holds the tls configuration to use in hijacked requests. |
| 28 | 28 |
tlsConfig *tls.Config |
| 29 | 29 |
// httpClient holds the client transport instance. Exported to keep the old code running. |
| 30 |
- HTTPClient *http.Client |
|
| 30 |
+ httpClient *http.Client |
|
| 31 | 31 |
// version of the server to talk to. |
| 32 | 32 |
version version.Version |
| 33 | 33 |
// custom http headers configured by users |
| ... | ... |
@@ -80,12 +80,12 @@ func NewClientWithVersion(host string, version version.Version, tlsOptions *tlsc |
| 80 | 80 |
sockets.ConfigureTCPTransport(transport, proto, addr) |
| 81 | 81 |
|
| 82 | 82 |
return &Client{
|
| 83 |
- Proto: proto, |
|
| 84 |
- Addr: addr, |
|
| 85 |
- BasePath: basePath, |
|
| 86 |
- Scheme: scheme, |
|
| 83 |
+ proto: proto, |
|
| 84 |
+ addr: addr, |
|
| 85 |
+ basePath: basePath, |
|
| 86 |
+ scheme: scheme, |
|
| 87 | 87 |
tlsConfig: tlsConfig, |
| 88 |
- HTTPClient: &http.Client{Transport: transport},
|
|
| 88 |
+ httpClient: &http.Client{Transport: transport},
|
|
| 89 | 89 |
version: version, |
| 90 | 90 |
customHTTPHeaders: httpHeaders, |
| 91 | 91 |
}, nil |
| ... | ... |
@@ -94,7 +94,7 @@ func NewClientWithVersion(host string, version version.Version, tlsOptions *tlsc |
| 94 | 94 |
// getAPIPath returns the versioned request path to call the api. |
| 95 | 95 |
// It appends the query parameters to the path if they are not empty. |
| 96 | 96 |
func (cli *Client) getAPIPath(p string, query url.Values) string {
|
| 97 |
- apiPath := fmt.Sprintf("%s/v%s%s", cli.BasePath, cli.version, p)
|
|
| 97 |
+ apiPath := fmt.Sprintf("%s/v%s%s", cli.basePath, cli.version, p)
|
|
| 98 | 98 |
if len(query) > 0 {
|
| 99 | 99 |
apiPath += "?" + query.Encode() |
| 100 | 100 |
} |
| ... | ... |
@@ -39,12 +39,12 @@ func (cli *Client) postHijacked(path string, query url.Values, body interface{},
|
| 39 | 39 |
if err != nil {
|
| 40 | 40 |
return types.HijackedResponse{}, err
|
| 41 | 41 |
} |
| 42 |
- req.Host = cli.Addr |
|
| 42 |
+ req.Host = cli.addr |
|
| 43 | 43 |
|
| 44 | 44 |
req.Header.Set("Connection", "Upgrade")
|
| 45 | 45 |
req.Header.Set("Upgrade", "tcp")
|
| 46 | 46 |
|
| 47 |
- conn, err := dial(cli.Proto, cli.Addr, cli.tlsConfig) |
|
| 47 |
+ conn, err := dial(cli.proto, cli.addr, cli.tlsConfig) |
|
| 48 | 48 |
if err != nil {
|
| 49 | 49 |
if strings.Contains(err.Error(), "connection refused") {
|
| 50 | 50 |
return types.HijackedResponse{}, fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
| ... | ... |
@@ -83,14 +83,14 @@ func (cli *Client) sendClientRequest(method, path string, query url.Values, body |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
req, err := cli.newRequest(method, path, query, body, headers) |
| 86 |
- req.URL.Host = cli.Addr |
|
| 87 |
- req.URL.Scheme = cli.Scheme |
|
| 86 |
+ req.URL.Host = cli.addr |
|
| 87 |
+ req.URL.Scheme = cli.scheme |
|
| 88 | 88 |
|
| 89 | 89 |
if expectedPayload && req.Header.Get("Content-Type") == "" {
|
| 90 | 90 |
req.Header.Set("Content-Type", "text/plain")
|
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 |
- resp, err := cli.HTTPClient.Do(req) |
|
| 93 |
+ resp, err := cli.httpClient.Do(req) |
|
| 94 | 94 |
if resp != nil {
|
| 95 | 95 |
serverResp.statusCode = resp.StatusCode |
| 96 | 96 |
} |
| ... | ... |
@@ -100,10 +100,10 @@ func (cli *Client) sendClientRequest(method, path string, query url.Values, body |
| 100 | 100 |
return serverResp, ErrConnectionFailed |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 |
- if cli.Scheme == "http" && strings.Contains(err.Error(), "malformed HTTP response") {
|
|
| 103 |
+ if cli.scheme == "http" && strings.Contains(err.Error(), "malformed HTTP response") {
|
|
| 104 | 104 |
return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
|
| 105 | 105 |
} |
| 106 |
- if cli.Scheme == "https" && strings.Contains(err.Error(), "remote error: bad certificate") {
|
|
| 106 |
+ if cli.scheme == "https" && strings.Contains(err.Error(), "remote error: bad certificate") {
|
|
| 107 | 107 |
return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
|
| 108 | 108 |
} |
| 109 | 109 |
|
| ... | ... |
@@ -1,164 +1,20 @@ |
| 1 | 1 |
package client |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "bytes" |
|
| 5 |
- "encoding/base64" |
|
| 6 |
- "encoding/json" |
|
| 7 |
- "errors" |
|
| 8 | 4 |
"fmt" |
| 9 |
- "io" |
|
| 10 |
- "io/ioutil" |
|
| 11 |
- "net/http" |
|
| 12 | 5 |
"os" |
| 13 | 6 |
gosignal "os/signal" |
| 14 | 7 |
"runtime" |
| 15 |
- "strings" |
|
| 16 | 8 |
"time" |
| 17 | 9 |
|
| 18 | 10 |
"github.com/Sirupsen/logrus" |
| 19 |
- "github.com/docker/docker/api" |
|
| 20 | 11 |
"github.com/docker/docker/api/client/lib" |
| 21 | 12 |
"github.com/docker/docker/api/types" |
| 22 |
- "github.com/docker/docker/cliconfig" |
|
| 23 |
- "github.com/docker/docker/dockerversion" |
|
| 24 |
- "github.com/docker/docker/pkg/jsonmessage" |
|
| 25 | 13 |
"github.com/docker/docker/pkg/signal" |
| 26 |
- "github.com/docker/docker/pkg/stdcopy" |
|
| 27 | 14 |
"github.com/docker/docker/pkg/term" |
| 28 | 15 |
"github.com/docker/docker/registry" |
| 29 |
- "github.com/docker/docker/utils" |
|
| 30 | 16 |
) |
| 31 | 17 |
|
| 32 |
-var ( |
|
| 33 |
- errConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
|
|
| 34 |
-) |
|
| 35 |
- |
|
| 36 |
-type serverResponse struct {
|
|
| 37 |
- body io.ReadCloser |
|
| 38 |
- header http.Header |
|
| 39 |
- statusCode int |
|
| 40 |
-} |
|
| 41 |
- |
|
| 42 |
-// HTTPClient creates a new HTTP client with the cli's client transport instance. |
|
| 43 |
-func (cli *DockerCli) HTTPClient() *http.Client {
|
|
| 44 |
- return &http.Client{Transport: cli.transport}
|
|
| 45 |
-} |
|
| 46 |
- |
|
| 47 |
-func (cli *DockerCli) encodeData(data interface{}) (*bytes.Buffer, error) {
|
|
| 48 |
- params := bytes.NewBuffer(nil) |
|
| 49 |
- if data != nil {
|
|
| 50 |
- if err := json.NewEncoder(params).Encode(data); err != nil {
|
|
| 51 |
- return nil, err |
|
| 52 |
- } |
|
| 53 |
- } |
|
| 54 |
- return params, nil |
|
| 55 |
-} |
|
| 56 |
- |
|
| 57 |
-func (cli *DockerCli) clientRequest(method, path string, in io.Reader, headers map[string][]string) (*serverResponse, error) {
|
|
| 58 |
- |
|
| 59 |
- serverResp := &serverResponse{
|
|
| 60 |
- body: nil, |
|
| 61 |
- statusCode: -1, |
|
| 62 |
- } |
|
| 63 |
- |
|
| 64 |
- expectedPayload := (method == "POST" || method == "PUT") |
|
| 65 |
- if expectedPayload && in == nil {
|
|
| 66 |
- in = bytes.NewReader([]byte{})
|
|
| 67 |
- } |
|
| 68 |
- req, err := http.NewRequest(method, fmt.Sprintf("%s/v%s%s", cli.basePath, api.Version, path), in)
|
|
| 69 |
- if err != nil {
|
|
| 70 |
- return serverResp, err |
|
| 71 |
- } |
|
| 72 |
- |
|
| 73 |
- // Add CLI Config's HTTP Headers BEFORE we set the Docker headers |
|
| 74 |
- // then the user can't change OUR headers |
|
| 75 |
- for k, v := range cli.configFile.HTTPHeaders {
|
|
| 76 |
- req.Header.Set(k, v) |
|
| 77 |
- } |
|
| 78 |
- |
|
| 79 |
- req.Header.Set("User-Agent", "Docker-Client/"+dockerversion.Version+" ("+runtime.GOOS+")")
|
|
| 80 |
- req.URL.Host = cli.addr |
|
| 81 |
- req.URL.Scheme = cli.scheme |
|
| 82 |
- |
|
| 83 |
- if headers != nil {
|
|
| 84 |
- for k, v := range headers {
|
|
| 85 |
- req.Header[k] = v |
|
| 86 |
- } |
|
| 87 |
- } |
|
| 88 |
- |
|
| 89 |
- if expectedPayload && req.Header.Get("Content-Type") == "" {
|
|
| 90 |
- req.Header.Set("Content-Type", "text/plain")
|
|
| 91 |
- } |
|
| 92 |
- |
|
| 93 |
- resp, err := cli.HTTPClient().Do(req) |
|
| 94 |
- if resp != nil {
|
|
| 95 |
- serverResp.statusCode = resp.StatusCode |
|
| 96 |
- } |
|
| 97 |
- |
|
| 98 |
- if err != nil {
|
|
| 99 |
- if utils.IsTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
|
|
| 100 |
- return serverResp, errConnectionFailed |
|
| 101 |
- } |
|
| 102 |
- |
|
| 103 |
- if cli.tlsConfig == nil && strings.Contains(err.Error(), "malformed HTTP response") {
|
|
| 104 |
- return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
|
|
| 105 |
- } |
|
| 106 |
- if cli.tlsConfig != nil && strings.Contains(err.Error(), "remote error: bad certificate") {
|
|
| 107 |
- return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
|
|
| 108 |
- } |
|
| 109 |
- |
|
| 110 |
- return serverResp, fmt.Errorf("An error occurred trying to connect: %v", err)
|
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- if serverResp.statusCode < 200 || serverResp.statusCode >= 400 {
|
|
| 114 |
- body, err := ioutil.ReadAll(resp.Body) |
|
| 115 |
- if err != nil {
|
|
| 116 |
- return serverResp, err |
|
| 117 |
- } |
|
| 118 |
- if len(body) == 0 {
|
|
| 119 |
- return serverResp, fmt.Errorf("Error: request returned %s for API route and version %s, check if the server supports the requested API version", http.StatusText(serverResp.statusCode), req.URL)
|
|
| 120 |
- } |
|
| 121 |
- return serverResp, fmt.Errorf("Error response from daemon: %s", bytes.TrimSpace(body))
|
|
| 122 |
- } |
|
| 123 |
- |
|
| 124 |
- serverResp.body = resp.Body |
|
| 125 |
- serverResp.header = resp.Header |
|
| 126 |
- return serverResp, nil |
|
| 127 |
-} |
|
| 128 |
- |
|
| 129 |
-// cmdAttempt builds the corresponding registry Auth Header from the given |
|
| 130 |
-// authConfig. It returns the servers body, status, error response |
|
| 131 |
-func (cli *DockerCli) cmdAttempt(authConfig cliconfig.AuthConfig, method, path string, in io.Reader, out io.Writer) (io.ReadCloser, int, error) {
|
|
| 132 |
- buf, err := json.Marshal(authConfig) |
|
| 133 |
- if err != nil {
|
|
| 134 |
- return nil, -1, err |
|
| 135 |
- } |
|
| 136 |
- registryAuthHeader := []string{
|
|
| 137 |
- base64.URLEncoding.EncodeToString(buf), |
|
| 138 |
- } |
|
| 139 |
- |
|
| 140 |
- // begin the request |
|
| 141 |
- serverResp, err := cli.clientRequest(method, path, in, map[string][]string{
|
|
| 142 |
- "X-Registry-Auth": registryAuthHeader, |
|
| 143 |
- }) |
|
| 144 |
- if err == nil && out != nil {
|
|
| 145 |
- // If we are streaming output, complete the stream since |
|
| 146 |
- // errors may not appear until later. |
|
| 147 |
- err = cli.streamBody(serverResp.body, serverResp.header.Get("Content-Type"), true, out, nil)
|
|
| 148 |
- } |
|
| 149 |
- if err != nil {
|
|
| 150 |
- // Since errors in a stream appear after status 200 has been written, |
|
| 151 |
- // we may need to change the status code. |
|
| 152 |
- if strings.Contains(err.Error(), "Authentication is required") || |
|
| 153 |
- strings.Contains(err.Error(), "Status 401") || |
|
| 154 |
- strings.Contains(err.Error(), "401 Unauthorized") || |
|
| 155 |
- strings.Contains(err.Error(), "status code 401") {
|
|
| 156 |
- serverResp.statusCode = http.StatusUnauthorized |
|
| 157 |
- } |
|
| 158 |
- } |
|
| 159 |
- return serverResp.body, serverResp.statusCode, err |
|
| 160 |
-} |
|
| 161 |
- |
|
| 162 | 18 |
func (cli *DockerCli) encodeRegistryAuth(index *registry.IndexInfo) (string, error) {
|
| 163 | 19 |
authConfig := registry.ResolveAuthConfig(cli.configFile, index) |
| 164 | 20 |
return authConfig.EncodeToBase64() |
| ... | ... |
@@ -174,85 +30,6 @@ func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registry.Index |
| 174 | 174 |
} |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 |
-func (cli *DockerCli) clientRequestAttemptLogin(method, path string, in io.Reader, out io.Writer, index *registry.IndexInfo, cmdName string) (io.ReadCloser, int, error) {
|
|
| 178 |
- |
|
| 179 |
- // Resolve the Auth config relevant for this server |
|
| 180 |
- authConfig := registry.ResolveAuthConfig(cli.configFile, index) |
|
| 181 |
- body, statusCode, err := cli.cmdAttempt(authConfig, method, path, in, out) |
|
| 182 |
- if statusCode == http.StatusUnauthorized {
|
|
| 183 |
- fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName) |
|
| 184 |
- if err = cli.CmdLogin(index.GetAuthConfigKey()); err != nil {
|
|
| 185 |
- return nil, -1, err |
|
| 186 |
- } |
|
| 187 |
- authConfig = registry.ResolveAuthConfig(cli.configFile, index) |
|
| 188 |
- return cli.cmdAttempt(authConfig, method, path, in, out) |
|
| 189 |
- } |
|
| 190 |
- return body, statusCode, err |
|
| 191 |
-} |
|
| 192 |
- |
|
| 193 |
-func (cli *DockerCli) callWrapper(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error) {
|
|
| 194 |
- sr, err := cli.call(method, path, data, headers) |
|
| 195 |
- return sr.body, sr.header, sr.statusCode, err |
|
| 196 |
-} |
|
| 197 |
- |
|
| 198 |
-func (cli *DockerCli) call(method, path string, data interface{}, headers map[string][]string) (*serverResponse, error) {
|
|
| 199 |
- params, err := cli.encodeData(data) |
|
| 200 |
- if err != nil {
|
|
| 201 |
- sr := &serverResponse{
|
|
| 202 |
- body: nil, |
|
| 203 |
- header: nil, |
|
| 204 |
- statusCode: -1, |
|
| 205 |
- } |
|
| 206 |
- return sr, nil |
|
| 207 |
- } |
|
| 208 |
- |
|
| 209 |
- if data != nil {
|
|
| 210 |
- if headers == nil {
|
|
| 211 |
- headers = make(map[string][]string) |
|
| 212 |
- } |
|
| 213 |
- headers["Content-Type"] = []string{"application/json"}
|
|
| 214 |
- } |
|
| 215 |
- |
|
| 216 |
- serverResp, err := cli.clientRequest(method, path, params, headers) |
|
| 217 |
- return serverResp, err |
|
| 218 |
-} |
|
| 219 |
- |
|
| 220 |
-type streamOpts struct {
|
|
| 221 |
- rawTerminal bool |
|
| 222 |
- in io.Reader |
|
| 223 |
- out io.Writer |
|
| 224 |
- err io.Writer |
|
| 225 |
- headers map[string][]string |
|
| 226 |
-} |
|
| 227 |
- |
|
| 228 |
-func (cli *DockerCli) stream(method, path string, opts *streamOpts) (*serverResponse, error) {
|
|
| 229 |
- serverResp, err := cli.clientRequest(method, path, opts.in, opts.headers) |
|
| 230 |
- if err != nil {
|
|
| 231 |
- return serverResp, err |
|
| 232 |
- } |
|
| 233 |
- return serverResp, cli.streamBody(serverResp.body, serverResp.header.Get("Content-Type"), opts.rawTerminal, opts.out, opts.err)
|
|
| 234 |
-} |
|
| 235 |
- |
|
| 236 |
-func (cli *DockerCli) streamBody(body io.ReadCloser, contentType string, rawTerminal bool, stdout, stderr io.Writer) error {
|
|
| 237 |
- defer body.Close() |
|
| 238 |
- |
|
| 239 |
- if api.MatchesContentType(contentType, "application/json") {
|
|
| 240 |
- return jsonmessage.DisplayJSONMessagesStream(body, stdout, cli.outFd, cli.isTerminalOut) |
|
| 241 |
- } |
|
| 242 |
- if stdout != nil || stderr != nil {
|
|
| 243 |
- // When TTY is ON, use regular copy |
|
| 244 |
- var err error |
|
| 245 |
- if rawTerminal {
|
|
| 246 |
- _, err = io.Copy(stdout, body) |
|
| 247 |
- } else {
|
|
| 248 |
- _, err = stdcopy.StdCopy(stdout, stderr, body) |
|
| 249 |
- } |
|
| 250 |
- logrus.Debugf("[stream] End of stdout")
|
|
| 251 |
- return err |
|
| 252 |
- } |
|
| 253 |
- return nil |
|
| 254 |
-} |
|
| 255 |
- |
|
| 256 | 177 |
func (cli *DockerCli) resizeTty(id string, isExec bool) {
|
| 257 | 178 |
height, width := cli.getTtySize() |
| 258 | 179 |
if height == 0 && width == 0 {
|
| ... | ... |
@@ -349,17 +126,3 @@ func (cli *DockerCli) getTtySize() (int, int) {
|
| 349 | 349 |
} |
| 350 | 350 |
return int(ws.Height), int(ws.Width) |
| 351 | 351 |
} |
| 352 |
- |
|
| 353 |
-func readBody(serverResp *serverResponse, err error) ([]byte, int, error) {
|
|
| 354 |
- if serverResp.body != nil {
|
|
| 355 |
- defer serverResp.body.Close() |
|
| 356 |
- } |
|
| 357 |
- if err != nil {
|
|
| 358 |
- return nil, serverResp.statusCode, err |
|
| 359 |
- } |
|
| 360 |
- body, err := ioutil.ReadAll(serverResp.body) |
|
| 361 |
- if err != nil {
|
|
| 362 |
- return nil, -1, err |
|
| 363 |
- } |
|
| 364 |
- return body, serverResp.statusCode, nil |
|
| 365 |
-} |