2015-05-14 13:05:39 -04:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-06-05 16:09:53 -04:00
|
|
|
"io"
|
2015-05-14 13:05:39 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-27 18:21:18 -04:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-06-12 09:25:32 -04:00
|
|
|
versionMimetype = "application/vnd.docker.plugins.v1.1+json"
|
2015-05-15 07:07:59 -04:00
|
|
|
defaultTimeOut = 30
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
type remoteError struct {
|
|
|
|
method string
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *remoteError) Error() string {
|
|
|
|
return fmt.Sprintf("Plugin Error: %s, %s", e.err, e.method)
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// NewClient creates a new plugin client (http).
|
2015-05-27 18:21:18 -04:00
|
|
|
func NewClient(addr string, tlsConfig tlsconfig.Options) (*Client, error) {
|
2015-05-14 13:05:39 -04:00
|
|
|
tr := &http.Transport{}
|
2015-05-27 18:21:18 -04:00
|
|
|
|
|
|
|
c, err := tlsconfig.Client(tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr.TLSClientConfig = c
|
|
|
|
|
2015-05-14 13:05:39 -04:00
|
|
|
protoAndAddr := strings.Split(addr, "://")
|
2015-05-27 18:21:18 -04:00
|
|
|
sockets.ConfigureTCPTransport(tr, protoAndAddr[0], protoAndAddr[1])
|
2015-09-22 15:54:29 -04:00
|
|
|
|
|
|
|
scheme := protoAndAddr[0]
|
|
|
|
if scheme != "https" {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
return &Client{&http.Client{Transport: tr}, scheme, protoAndAddr[1]}, nil
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Client represents a plugin client.
|
2015-05-14 13:05:39 -04:00
|
|
|
type Client struct {
|
2015-09-22 15:54:29 -04:00
|
|
|
http *http.Client // http client to use
|
|
|
|
scheme string // scheme protocol of the plugin
|
|
|
|
addr string // http address of the plugin
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Call calls the specified method with the specified arguments for the plugin.
|
|
|
|
// It will retry for 30 seconds if a failure occurs when calling.
|
2015-05-14 13:05:39 -04:00
|
|
|
func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
|
2015-06-05 16:09:53 -04:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
body, err := c.callWithRetry(serviceMethod, &buf, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer body.Close()
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := json.NewDecoder(body).Decode(&ret); err != nil {
|
|
|
|
logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:09:53 -04:00
|
|
|
// Stream calls the specified method with the specified arguments for the plugin and returns the response body
|
|
|
|
func (c *Client) Stream(serviceMethod string, args interface{}) (io.ReadCloser, error) {
|
2015-05-14 13:05:39 -04:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(args); err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return nil, err
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-06-05 16:09:53 -04:00
|
|
|
return c.callWithRetry(serviceMethod, &buf, true)
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-05 16:09:53 -04:00
|
|
|
// SendFile calls the specified method, and passes through the IO stream
|
|
|
|
func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{}) error {
|
|
|
|
body, err := c.callWithRetry(serviceMethod, data, true)
|
2015-05-14 13:05:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := json.NewDecoder(body).Decode(&ret); err != nil {
|
|
|
|
logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-05 16:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool) (io.ReadCloser, error) {
|
|
|
|
req, err := http.NewRequest("POST", "/"+serviceMethod, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
req.Header.Add("Accept", versionMimetype)
|
2015-09-22 15:54:29 -04:00
|
|
|
req.URL.Scheme = c.scheme
|
2015-05-14 13:05:39 -04:00
|
|
|
req.URL.Host = c.addr
|
|
|
|
|
|
|
|
var retries int
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
if !retry {
|
2015-06-05 16:09:53 -04:00
|
|
|
return nil, err
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 13:05:39 -04:00
|
|
|
timeOff := backoff(retries)
|
2015-05-19 16:05:25 -04:00
|
|
|
if abort(start, timeOff) {
|
2015-06-05 16:09:53 -04:00
|
|
|
return nil, err
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
retries++
|
2015-05-19 16:05:25 -04:00
|
|
|
logrus.Warnf("Unable to connect to plugin: %s, retrying in %v", c.addr, timeOff)
|
2015-05-14 13:05:39 -04:00
|
|
|
time.Sleep(timeOff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
remoteErr, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-06-05 16:09:53 -04:00
|
|
|
return nil, &remoteError{err.Error(), serviceMethod}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-06-05 16:09:53 -04:00
|
|
|
return nil, &remoteError{string(remoteErr), serviceMethod}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-06-05 16:09:53 -04:00
|
|
|
return resp.Body, nil
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func backoff(retries int) time.Duration {
|
2015-05-19 16:05:25 -04:00
|
|
|
b, max := 1, defaultTimeOut
|
2015-05-14 13:05:39 -04:00
|
|
|
for b < max && retries > 0 {
|
|
|
|
b *= 2
|
|
|
|
retries--
|
|
|
|
}
|
|
|
|
if b > max {
|
|
|
|
b = max
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
return time.Duration(b) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func abort(start time.Time, timeOff time.Duration) bool {
|
2015-07-09 19:31:43 -04:00
|
|
|
return timeOff+time.Since(start) >= time.Duration(defaultTimeOut)*time.Second
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|