mirror of
https://github.com/status-im/consul.git
synced 2025-02-02 00:46:43 +00:00
agent: make docker client work on windows
This commit is contained in:
parent
748b5b1b00
commit
83577e0daa
@ -1734,6 +1734,7 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *structs.CheckType,
|
|||||||
a.logger.Printf("[ERR] agent: error creating docker client: %s", err)
|
a.logger.Printf("[ERR] agent: error creating docker client: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
a.logger.Printf("[DEBUG] agent: created docker client for %s", dc.host)
|
||||||
a.dockerClient = dc
|
a.dockerClient = dc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,16 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/armon/circbuf"
|
"github.com/armon/circbuf"
|
||||||
|
"github.com/docker/go-connections/sockets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DockerClient is a simplified client for the Docker Engine API
|
// DockerClient is a simplified client for the Docker Engine API
|
||||||
@ -21,44 +20,76 @@ import (
|
|||||||
// a ring buffer with a fixed limit to avoid excessive resource
|
// a ring buffer with a fixed limit to avoid excessive resource
|
||||||
// consumption.
|
// consumption.
|
||||||
type DockerClient struct {
|
type DockerClient struct {
|
||||||
network string
|
host string
|
||||||
addr string
|
scheme string
|
||||||
baseurl string
|
proto string
|
||||||
maxbuf int64
|
addr string
|
||||||
client *http.Client
|
basepath string
|
||||||
|
maxbuf int64
|
||||||
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerClient(host string, maxbuf int64) (*DockerClient, error) {
|
func NewDockerClient(host string, maxbuf int64) (*DockerClient, error) {
|
||||||
if host == "" {
|
if host == "" {
|
||||||
host = DefaultDockerHost
|
host = DefaultDockerHost
|
||||||
}
|
}
|
||||||
p := strings.SplitN(host, "://", 2)
|
|
||||||
if len(p) != 2 {
|
proto, addr, basepath, err := ParseHost(host)
|
||||||
return nil, fmt.Errorf("invalid docker host: %s", host)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
network, addr := p[0], p[1]
|
|
||||||
basepath := "http://" + addr
|
transport := new(http.Transport)
|
||||||
if network == "unix" {
|
sockets.ConfigureTransport(transport, proto, addr)
|
||||||
basepath = "http://unix"
|
client := &http.Client{Transport: transport}
|
||||||
|
|
||||||
|
return &DockerClient{
|
||||||
|
host: host,
|
||||||
|
scheme: "http",
|
||||||
|
proto: proto,
|
||||||
|
addr: addr,
|
||||||
|
basepath: basepath,
|
||||||
|
maxbuf: maxbuf,
|
||||||
|
client: client,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseHost verifies that the given host strings is valid.
|
||||||
|
// copied from github.com/docker/docker/client.go
|
||||||
|
func ParseHost(host string) (string, string, string, error) {
|
||||||
|
protoAddrParts := strings.SplitN(host, "://", 2)
|
||||||
|
if len(protoAddrParts) == 1 {
|
||||||
|
return "", "", "", fmt.Errorf("unable to parse docker host `%s`", host)
|
||||||
}
|
}
|
||||||
client := &http.Client{}
|
|
||||||
if network == "unix" {
|
var basePath string
|
||||||
client.Transport = &http.Transport{
|
proto, addr := protoAddrParts[0], protoAddrParts[1]
|
||||||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
if proto == "tcp" {
|
||||||
return net.Dial(network, addr)
|
parsed, err := url.Parse("tcp://" + addr)
|
||||||
},
|
if err != nil {
|
||||||
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
addr = parsed.Host
|
||||||
|
basePath = parsed.Path
|
||||||
}
|
}
|
||||||
return &DockerClient{network, addr, basepath, maxbuf, client}, nil
|
return proto, addr, basePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DockerClient) call(method, uri string, v interface{}) (*circbuf.Buffer, int, error) {
|
func (c *DockerClient) call(method, uri string, v interface{}) (*circbuf.Buffer, int, error) {
|
||||||
urlstr := c.baseurl + uri
|
req, err := http.NewRequest(method, uri, nil)
|
||||||
req, err := http.NewRequest(method, urlstr, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.proto == "unix" || c.proto == "npipe" {
|
||||||
|
// For local communications, it doesn't matter what the host is. We just
|
||||||
|
// need a valid and meaningful host name. (See #189)
|
||||||
|
req.Host = "docker"
|
||||||
|
}
|
||||||
|
|
||||||
|
req.URL.Host = c.addr
|
||||||
|
req.URL.Scheme = c.scheme
|
||||||
|
|
||||||
if v != nil {
|
if v != nil {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := json.NewEncoder(&b).Encode(v); err != nil {
|
if err := json.NewEncoder(&b).Encode(v); err != nil {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// +build linux freebsd solaris openbsd darwin
|
// +build !windows
|
||||||
|
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user