本文整理汇总了Golang中github.com/docker/engine-api/client.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Gather
// Gather starts stats collection
func (d *Docker) Gather(acc telegraf.Accumulator) error {
if d.client == nil {
var c *client.Client
var err error
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
if d.Endpoint == "ENV" {
c, err = client.NewEnvClient()
if err != nil {
return err
}
} else if d.Endpoint == "" {
c, err = client.NewClient("unix:///var/run/docker.sock", "", nil, defaultHeaders)
if err != nil {
return err
}
} else {
c, err = client.NewClient(d.Endpoint, "", nil, defaultHeaders)
if err != nil {
return err
}
}
d.client = c
}
// Get daemon info
err := d.gatherInfo(acc)
if err != nil {
fmt.Println(err.Error())
}
// List containers
opts := types.ContainerListOptions{}
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
containers, err := d.client.ContainerList(ctx, opts)
if err != nil {
return err
}
// Get container data
var wg sync.WaitGroup
wg.Add(len(containers))
for _, container := range containers {
go func(c types.Container) {
defer wg.Done()
err := d.gatherContainer(c, acc)
if err != nil {
log.Printf("Error gathering container %s stats: %s\n",
c.Names, err.Error())
}
}(container)
}
wg.Wait()
return nil
}
示例2: Connect
// Connect will initialize a connection to the Docker daemon running on the
// host, gather machine specs (memory, cpu, ...) and monitor state changes.
func (e *Engine) Connect(config *tls.Config) error {
host, _, err := net.SplitHostPort(e.Addr)
if err != nil {
return err
}
addr, err := net.ResolveIPAddr("ip4", host)
if err != nil {
return err
}
e.IP = addr.IP.String()
// create the HTTP Client and URL
httpClient, url, err := NewHTTPClientTimeout("tcp://"+e.Addr, config, time.Duration(requestTimeout), setTCPUserTimeout)
if err != nil {
return err
}
e.httpClient = httpClient
e.url = url
// Use HTTP Client created above to create a dockerclient client
c := dockerclient.NewDockerClientFromHTTP(url, httpClient, config)
// Use HTTP Client used by dockerclient to create engine-api client
apiClient, err := engineapi.NewClient("tcp://"+e.Addr, "", c.HTTPClient, nil)
if err != nil {
return err
}
return e.ConnectWithClient(c, apiClient)
}
示例3: getDockerClient
// Get a *dockerapi.Client, either using the endpoint passed in, or using
// DOCKER_HOST, DOCKER_TLS_VERIFY, and DOCKER_CERT path per their spec
func getDockerClient(dockerEndpoint string) (*dockerapi.Client, error) {
if len(dockerEndpoint) > 0 {
glog.Infof("Connecting to docker on %s", dockerEndpoint)
return dockerapi.NewClient(dockerEndpoint, "", nil, nil)
}
return dockerapi.NewEnvClient()
}
示例4: createClient
func (provider *Docker) createClient() (client.APIClient, error) {
var httpClient *http.Client
httpHeaders := map[string]string{
"User-Agent": "Traefik " + version.Version,
}
if provider.TLS != nil {
config, err := provider.TLS.CreateTLSConfig()
if err != nil {
return nil, err
}
tr := &http.Transport{
TLSClientConfig: config,
}
proto, addr, _, err := client.ParseHost(provider.Endpoint)
if err != nil {
return nil, err
}
sockets.ConfigureTransport(tr, proto, addr)
httpClient = &http.Client{
Transport: tr,
}
}
var version string
if provider.SwarmMode {
version = SwarmAPIVersion
} else {
version = DockerAPIVersion
}
return client.NewClient(provider.Endpoint, version, httpClient, httpHeaders)
}
示例5: main
func main() {
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient(dockerHost, "", nil, defaultHeaders)
if err != nil {
panic(err)
}
// get container info
c, err := cli.ContainerInspect(context.Background(), arg)
if err != nil {
logrus.Fatalf("inspecting container (%s) failed: %v", arg, err)
}
t := native.New()
spec, err := parse.Config(c, platform.OSType, platform.Architecture, t.Capabilities, idroot, idlen)
if err != nil {
logrus.Fatalf("Spec config conversion for %s failed: %v", arg, err)
}
// fill in hooks, if passed through command line
spec.Hooks = hooks
if err := writeConfig(spec); err != nil {
logrus.Fatal(err)
}
fmt.Printf("%s has been saved.\n", specConfig)
}
示例6: NewEngineAPIClient
// NewEngineAPIClient creates a new Docker engine API client
func NewEngineAPIClient(config *api.DockerConfig) (*dockerapi.Client, error) {
var httpClient *http.Client
if config.UseTLS || config.TLSVerify {
tlscOptions := tlsconfig.Options{
InsecureSkipVerify: !config.TLSVerify,
}
if _, err := os.Stat(config.CAFile); !os.IsNotExist(err) {
tlscOptions.CAFile = config.CAFile
}
if _, err := os.Stat(config.CertFile); !os.IsNotExist(err) {
tlscOptions.CertFile = config.CertFile
}
if _, err := os.Stat(config.KeyFile); !os.IsNotExist(err) {
tlscOptions.KeyFile = config.KeyFile
}
tlsc, err := tlsconfig.Client(tlscOptions)
if err != nil {
return nil, err
}
httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsc,
},
}
}
return dockerapi.NewClient(config.Endpoint, os.Getenv("DOCKER_API_VERSION"), httpClient, nil)
}
示例7: Init
// Init Refcounts. Discover volume usage refcounts from Docker.
// This functions does not sync with mount/unmount handlers and should be called
// and completed BEFORE we start accepting Mount/unmount requests.
func (r refCountsMap) Init(d *vmdkDriver) {
c, err := client.NewClient(dockerUSocket, apiVersion, nil, defaultHeaders)
if err != nil {
log.Panicf("Failed to create client for Docker at %s.( %v)",
dockerUSocket, err)
}
log.Infof("Getting volume data from %s", dockerUSocket)
info, err := c.Info(context.Background())
if err != nil {
log.Infof("Can't connect to %s, skipping discovery", dockerUSocket)
// TODO: Issue #369
// Docker is not running, inform ESX to detach docker volumes, if any
// d.detachAllVolumes()
return
}
log.Debugf("Docker info: version=%s, root=%s, OS=%s",
info.ServerVersion, info.DockerRootDir, info.OperatingSystem)
// connects (and polls if needed) and then calls discovery
err = r.discoverAndSync(c, d)
if err != nil {
log.Errorf("Failed to discover mount refcounts(%v)", err)
return
}
log.Infof("Discovered %d volumes in use.", len(r))
for name, cnt := range r {
log.Infof("Volume name=%s count=%d mounted=%t device='%s'",
name, cnt.count, cnt.mounted, cnt.dev)
}
}
示例8: NewDockerCli
// 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).
func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
//创建cli对象
cli := &DockerCli{
in: in,
out: out,
err: err,
keyFile: clientFlags.Common.TrustKey,
}
//docker客户端模式的创建过程,如果需要安全认证,需要加载安全认证的证书。
cli.init = func() error {
clientFlags.PostParse()
configFile, e := cliconfig.Load(cliconfig.ConfigDir())
if e != nil {
fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
}
if !configFile.ContainsAuth() {
credentials.DetectDefaultStore(configFile)
}
cli.configFile = configFile
host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
if err != nil {
return err
}
customHeaders := cli.configFile.HTTPHeaders
if customHeaders == nil {
customHeaders = map[string]string{}
}
customHeaders["User-Agent"] = clientUserAgent()
verStr := api.DefaultVersion.String()
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
verStr = tmpStr
}
httpClient, err := newHTTPClient(host, clientFlags.Common.TLSOptions)
if err != nil {
return err
}
client, err := client.NewClient(host, verStr, httpClient, customHeaders)
if err != nil {
return err
}
cli.client = client
if cli.in != nil {
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
}
if cli.out != nil {
cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
}
return nil
}
return cli
}
示例9: main
func main() {
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.23", nil, nil)
if err != nil {
panic(err)
}
hostConfig := &container.HostConfig{
NetworkMode: "serverlessdockervotingapp_default",
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
}
http.Handle("/vote/", &dcgi.Handler{
Image: "bfirsh/serverless-vote",
Client: cli,
HostConfig: hostConfig,
Root: "/vote", // strip /vote from all URLs
})
http.Handle("/result/", &dcgi.Handler{
Image: "bfirsh/serverless-result",
Client: cli,
HostConfig: hostConfig,
Root: "/result",
})
http.ListenAndServe(":80", nil)
}
示例10: createClient
func (provider *Docker) createClient() (client.APIClient, error) {
var httpClient *http.Client
httpHeaders := map[string]string{
// FIXME(vdemeester) use version here O:)
"User-Agent": "Traefik",
}
if provider.TLS != nil {
tlsOptions := tlsconfig.Options{
CAFile: provider.TLS.CA,
CertFile: provider.TLS.Cert,
KeyFile: provider.TLS.Key,
InsecureSkipVerify: provider.TLS.InsecureSkipVerify,
}
config, err := tlsconfig.Client(tlsOptions)
if err != nil {
return nil, err
}
tr := &http.Transport{
TLSClientConfig: config,
}
proto, addr, _, err := client.ParseHost(provider.Endpoint)
if err != nil {
return nil, err
}
sockets.ConfigureTransport(tr, proto, addr)
httpClient = &http.Client{
Transport: tr,
}
}
return client.NewClient(provider.Endpoint, DockerAPIVersion, httpClient, httpHeaders)
}
示例11: main
func main() {
flags.Parse(os.Args)
if *images == "" {
log.Fatalf("Specify --images=foo:1.0,bar:2.0 etc")
}
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.21", nil, defaultHeaders)
if err != nil {
log.Fatal(err)
}
for _, image := range strings.Split(*images, ",") {
if len(strings.Split(image, ":")) != 2 {
log.Printf("Skipping untagged image %v", image)
continue
}
log.Printf("Pulling %v", image)
resp, err := cli.ImagePull(context.Background(), image, types.ImagePullOptions{})
if err != nil {
log.Fatalf("Failed to pull %v: %v", image, err)
}
var lines interface{}
for lineReader := bufio.NewReader(resp); err != io.EOF; {
err = json.NewDecoder(lineReader).Decode(&lines)
if m, ok := lines.(map[string]interface{}); ok {
log.Printf("%+v", m)
}
}
}
}
示例12: newPlugin
func newPlugin(dockerHost string) (*novolume, error) {
client, err := dockerclient.NewClient(dockerHost, dockerapi.DefaultVersion.String(), nil, nil)
if err != nil {
return nil, err
}
return &novolume{client: client}, nil
}
示例13: NewDockerCli
// 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).
func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
cli := &DockerCli{
in: in,
out: out,
err: err,
keyFile: clientFlags.Common.TrustKey,
}
cli.init = func() error {
clientFlags.PostParse()
configFile, e := cliconfig.Load(cliconfig.ConfigDir())
if e != nil {
fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
}
cli.configFile = configFile
host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
if err != nil {
return err
}
customHeaders := cli.configFile.HTTPHeaders
if customHeaders == nil {
customHeaders = map[string]string{}
}
customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
verStr := api.DefaultVersion.String()
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
verStr = tmpStr
}
clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
if err != nil {
return err
}
client, err := client.NewClient(host, verStr, clientTransport, customHeaders)
if err != nil {
return err
}
cli.client = client
if cli.in != nil {
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
}
if cli.out != nil {
cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
}
return nil
}
return cli
}
示例14: buildTestImage
func buildTestImage(c *C) {
var endpoint string
if endpoint = os.Getenv("DOCKER_HOST"); endpoint == "" {
endpoint = client.DefaultDockerHost
}
const dockerFile = `
FROM alpine
COPY worker.sh /usr/bin/worker.sh
`
cl, err := client.NewClient(endpoint, "", nil, nil)
c.Assert(err, IsNil)
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
files := []struct {
Name, Body string
Mode int64
}{
{"worker.sh", testsuite.ScriptWorkerSh, 0777},
{"Dockerfile", dockerFile, 0666},
}
for _, file := range files {
hdr := &tar.Header{
Name: file.Name,
Mode: file.Mode,
Size: int64(len(file.Body)),
}
c.Assert(tw.WriteHeader(hdr), IsNil)
_, err = tw.Write([]byte(file.Body))
c.Assert(err, IsNil)
}
c.Assert(tw.Close(), IsNil)
opts := types.ImageBuildOptions{
Tags: []string{"worker"},
}
resp, err := cl.ImageBuild(context.Background(), buf, opts)
c.Assert(err, IsNil)
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
err = cl.ImageTag(context.Background(), "worker", "localhost:5000/worker", types.ImageTagOptions{Force: true})
c.Assert(err, IsNil)
buildResp, err := cl.ImagePush(context.Background(), "localhost:5000/worker:latest", types.ImagePushOptions{RegistryAuth: "e30="})
c.Assert(err, IsNil)
defer buildResp.Close()
io.Copy(ioutil.Discard, buildResp)
}
示例15: init
func init() {
var err error
dockerClient, err = client.NewClient("unix:///var/run/docker.sock", "", nil, nil)
if err != nil {
return
}
v, err := dockerClient.ServerVersion(context.Background())
if err != nil {
dockerClient = nil
return
}
dockerClient.UpdateClientVersion(v.APIVersion)
}