當前位置: 首頁>>代碼示例>>Golang>>正文


Golang client.NewClient函數代碼示例

本文整理匯總了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
}
開發者ID:lizaoreo,項目名稱:telegraf,代碼行數:57,代碼來源:docker.go

示例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)
}
開發者ID:yehohanan7,項目名稱:swarm,代碼行數:33,代碼來源:engine.go

示例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()
}
開發者ID:copejon,項目名稱:origin,代碼行數:9,代碼來源:docker.go

示例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)

}
開發者ID:vdemeester,項目名稱:traefik,代碼行數:34,代碼來源:docker.go

示例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)
}
開發者ID:jfrazelle,項目名稱:riddler,代碼行數:27,代碼來源:main.go

示例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)
}
開發者ID:php-coder,項目名稱:source-to-image,代碼行數:32,代碼來源:docker.go

示例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)
	}
}
開發者ID:vmware,項目名稱:docker-volume-vsphere,代碼行數:34,代碼來源:refcnt.go

示例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
}
開發者ID:DCdrone,項目名稱:docker,代碼行數:64,代碼來源:cli.go

示例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)
}
開發者ID:40a,項目名稱:serverless-docker-voting-app,代碼行數:25,代碼來源:main.go

示例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)
}
開發者ID:goguardian,項目名稱:traefik,代碼行數:33,代碼來源:docker.go

示例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)
			}
		}
	}
}
開發者ID:bprashanth,項目名稱:tmp,代碼行數:30,代碼來源:puller.go

示例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
}
開發者ID:lsm5,項目名稱:docker-novolume-plugin,代碼行數:7,代碼來源:plugin.go

示例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
}
開發者ID:wanliang1221,項目名稱:docker,代碼行數:59,代碼來源:cli.go

示例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)
}
開發者ID:noxiouz,項目名稱:stout,代碼行數:53,代碼來源:box_test.go

示例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)
}
開發者ID:pombredanne,項目名稱:dockyard,代碼行數:15,代碼來源:container.go


注:本文中的github.com/docker/engine-api/client.NewClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。