Пример #1
0
// 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
0
// 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
0
// 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
0
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
0
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
0
// 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
0
// 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
0
// 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
0
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
0
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
0
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
0
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
0
// 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
0
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
0
func buildTestImage(c *C, endpoint string) {
	const dockerFile = `
FROM ubuntu:trusty

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()

	for {
		var p = make([]byte, 1024)
		_, err := resp.Body.Read(p)
		if err != nil {
			c.Assert(err, Equals, io.EOF)
			break
		}
	}
}
Пример #16
0
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)
}
Пример #17
0
func NewDriver(scope string, vtepdev string) (*Driver, error) {
	defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
	docker, err := dockerclient.NewClient("unix:///var/run/docker.sock", "v1.23", nil, defaultHeaders)
	if err != nil {
		log.Errorf("Error connecting to docker socket: %v", err)
		return nil, err
	}
	d := &Driver{
		scope:    scope,
		vtepdev:  vtepdev,
		networks: make(map[string]*NetworkState),
		docker:   docker,
	}
	return d, nil
}
Пример #18
0
func init() {
	logger = log.NewLogger(log.NewConcurrentWriter(os.Stdout), "docker")
	/*
		log = logrus.New()
		logger.Formatter = &logrus.TextFormatter{}
		logger.Out = os.Stdout
		logger.Level = logrus.DebugLevel
	*/

	defaultHeaders := map[string]string{"User-Agent": userAgent}
	var err error
	cli, err = client.NewClient(dockerDaemon, dockerVersion, nil, defaultHeaders)
	if err != nil {
		logger.Fatal("Failed to setup docker!", "err", err)
	}
}
// TODO(random-liu): Add more validating items.
func (d *DockerValidator) Validate(spec SysSpec) error {
	if spec.RuntimeSpec.DockerSpec == nil {
		// If DockerSpec is not specified, assume current runtime is not
		// docker, skip the docker configuration validation.
		return nil
	}
	c, err := client.NewClient(dockerEndpoint, "", nil, nil)
	if err != nil {
		return fmt.Errorf("failed to create docker client: %v", err)
	}
	info, err := c.Info(context.Background())
	if err != nil {
		return fmt.Errorf("failed to get docker info: %v", err)
	}
	return d.validateDockerInfo(spec.RuntimeSpec.DockerSpec, info)
}
Пример #20
0
// NewBox ...
func NewBox(ctx context.Context, cfg isolate.BoxConfig) (isolate.Box, error) {
	var config = &dockerBoxConfig{
		DockerEndpoint:   client.DefaultDockerHost,
		SpawnConcurrency: defaultSpawnConcurrency,
	}

	decoderConfig := mapstructure.DecoderConfig{
		WeaklyTypedInput: true,
		Result:           config,
		TagName:          "json",
	}

	decoder, err := mapstructure.NewDecoder(&decoderConfig)
	if err != nil {
		return nil, err
	}

	if err = decoder.Decode(cfg); err != nil {
		return nil, err
	}

	client, err := client.NewClient(config.DockerEndpoint, config.APIVersion, nil, defaultHeaders)
	if err != nil {
		return nil, err
	}

	ctx, cancellation := context.WithCancel(ctx)
	box := &Box{
		ctx:          ctx,
		cancellation: cancellation,

		client:     client,
		spawnSM:    semaphore.New(config.SpawnConcurrency),
		config:     config,
		containers: make(map[string]*process),
	}

	body, err := json.Marshal(config)
	if err != nil {
		return nil, err
	}
	dockerConfig.Set(string(body))

	go box.watchEvents()

	return box, nil
}
Пример #21
0
func (driver localPersistDriver) findExistingVolumesFromDockerDaemon() (error, map[string]string) {
	// set up the ability to make API calls to the daemon
	defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
	// need at least Docker 1.9 (API v1.21) for named Volume support
	cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.21", nil, defaultHeaders)
	if err != nil {
		return err, map[string]string{}
	}

	// grab ALL containers...
	options := types.ContainerListOptions{All: true}
	containers, err := cli.ContainerList(context.Background(), options)

	// ...and check to see if any of them belong to this driver and recreate their references
	var volumes = map[string]string{}
	for _, container := range containers {
		info, err := cli.ContainerInspect(context.Background(), container.ID)
		if err != nil {
			// something really weird happened here... PANIC
			panic(err)
		}

		for _, mount := range info.Mounts {
			if mount.Driver == driver.name {
				// @TODO there could be multiple volumes (mounts) with this { name: source } combo, and while that's okay
				// what if they is the same name with a different source? could that happen? if it could,
				// it'd be bad, so maybe we want to panic here?
				volumes[mount.Name] = mount.Source
			}
		}
	}

	if err != nil || len(volumes) == 0 {
		fmt.Print("Attempting to load from file state...   ")

		return driver.findExistingVolumesFromStateFile()
	}

	return nil, volumes
}
Пример #22
0
func TestImagePullFromRegistry(t *testing.T) {
	assert := assert.New(t)
	var endpoint string
	if endpoint = os.Getenv("DOCKER_HOST"); endpoint == "" {
		endpoint = client.DefaultDockerHost
	}
	client, err := client.NewClient(endpoint, "", nil, defaultHeaders)
	assert.NoError(err)

	ctx := context.Background()
	box := Box{
		ctx:    ctx,
		client: client,
		config: &dockerBoxConfig{},
	}

	var profile = isolate.Profile{
		"registry": "docker.io",
	}

	t.Logf("Clean up docker.io/alpine:latest if it exists")
	client.ImageRemove(ctx, "docker.io/alpine:latest", types.ImageRemoveOptions{Force: false, PruneChildren: false})
	t.Logf("Spool via box on 'clean' system")
	err = box.Spool(ctx, "alpine", profile)
	assert.NoError(err)
	imgs, err := client.ImageList(ctx, types.ImageListOptions{})
	found := false
	for _, img := range imgs {
		if strings.Contains(img.RepoTags[0], "alpine") {
			found = true
			break
		}
	}
	assert.NoError(err)
	assert.True(found)
	t.Logf("Spool an already spooled image")
	err = box.Spool(ctx, "alpine", profile)
	assert.NoError(err)
}
Пример #23
0
// New creates a new implementation of the STI Docker interface
func New(config *api.DockerConfig, auth api.AuthConfig) (Docker, error) {
	var client *dockerapi.Client
	var httpClient *http.Client
	if config.CertFile != "" && config.KeyFile != "" && config.CAFile != "" {
		tlscOptions := tlsconfig.Options{
			CAFile:   config.CAFile,
			CertFile: config.CertFile,
			KeyFile:  config.KeyFile,
		}
		tlsc, tlsErr := tlsconfig.Client(tlscOptions)
		if tlsErr != nil {
			return nil, tlsErr
		}
		httpClient = &http.Client{
			Transport: k8snet.SetTransportDefaults(&http.Transport{
				TLSClientConfig: tlsc,
			}),
		}
	}

	client, err := dockerapi.NewClient(config.Endpoint, "", httpClient, nil)
	if err != nil {
		return nil, err
	}
	k8sDocker := dockertools.ConnectToDockerOrDie(config.Endpoint, 0)
	return &stiDocker{
		kubeDockerClient: k8sDocker,
		client:           client,
		httpClient:       httpClient,
		dialer:           &net.Dialer{},
		pullAuth: dockertypes.AuthConfig{
			Username:      auth.Username,
			Password:      auth.Password,
			Email:         auth.Email,
			ServerAddress: auth.ServerAddress,
		},
		endpoint: config.Endpoint,
	}, nil
}
Пример #24
0
func newPlugin(dockerHost, configPath string) (*registryacl, error) {
	client, err := dockerclient.NewClient(dockerHost, dockerapi.DefaultVersion.String(), nil, nil)
	if err != nil {
		return nil, err
	}
	if configPath == "" {
		return nil, fmt.Errorf("Please specify a configuration file")
	}
	absConfig, err := filepath.Abs(configPath)
	if err != nil {
		return nil, err
	}
	b, err := ioutil.ReadFile(absConfig)
	if err != nil {
		return nil, err
	}
	var conf config
	if _, err := toml.Decode(string(b), &conf); err != nil {
		return nil, err
	}
	return &registryacl{client: client, config: conf}, nil
}
// TestControllerFlowIntegration simply runs the Controller flow against a docker
// instance to make sure we don't blow up.
//
// This is great for ad-hoc testing while doing development. We can add more
// verification but it solves the problem of not being able to run tasks
// without a swarm setup.
//
// Run with something like this:
//
//	go test -run TestControllerFlowIntegration -test.docker.addr unix:///var/run/docker.sock
//
func TestControllerFlowIntegration(t *testing.T) {
	if dockerTestAddr == "" {
		t.Skip("specify docker address to run integration")
	}

	ctx := context.Background()
	client, err := engineapi.NewClient(dockerTestAddr, "", nil, nil)
	assert.NoError(t, err)
	assert.NotNil(t, client)

	task := &api.Task{
		ID:        "dockerexec-integration-task-id",
		ServiceID: "dockerexec-integration-service-id",
		NodeID:    "dockerexec-integration-node-id",
		Spec: api.TaskSpec{
			Runtime: &api.TaskSpec_Container{
				Container: &api.ContainerSpec{
					Command: []string{"sh", "-c", "sleep 5"},
					Image:   "alpine",
				},
			},
		},
	}

	ctlr, err := newController(client, task)
	assert.NoError(t, err)
	assert.NotNil(t, ctlr)
	assert.NoError(t, ctlr.Prepare(ctx))
	assert.NoError(t, ctlr.Start(ctx))
	assert.NoError(t, ctlr.Wait(ctx))
	assert.NoError(t, ctlr.Shutdown(ctx))
	assert.NoError(t, ctlr.Remove(ctx))
	assert.NoError(t, ctlr.Close())

	// NOTE(stevvooe): testify has no clue how to correctly do error equality.
	if err := ctlr.Close(); err != exec.ErrControllerClosed {
		t.Fatalf("expected controller to be closed: %v", err)
	}
}
Пример #26
0
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(arg)
	if err != nil {
		logrus.Fatalf("inspecting container (%s) failed: %v", arg, err)
	}

	// get daemon info
	info, err := cli.Info()
	if err != nil {
		logrus.Fatalf("getting daemon info failed: %v", err)
	}

	t := native.New()
	spec, err := parse.Config(c, info, t.Capabilities)
	if err != nil {
		logrus.Fatalf("Spec config conversion for %s failed: %v", arg, err)
	}

	rspec, err := parse.RuntimeConfig(c)
	if err != nil {
		logrus.Fatalf("Spec runtime config conversion for %s failed: %v", arg, err)
	}

	// fill in hooks, if passed through command line
	rspec.Hooks = hooks
	if err := writeConfigs(spec, rspec); err != nil {
		logrus.Fatal(err)
	}

	fmt.Printf("%s and %s have been saved.", specConfig, runtimeConfig)
}
Пример #27
0
// newDockerClient creates a new docker client from client options
func newDockerClient(co *clientutil.ClientOptions) (DockerClient, error) {
	var httpClient *http.Client
	tlsConfig := co.TLSConfig()
	host := co.DaemonURL()

	if tlsConfig != nil {
		httpClient = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: tlsConfig,
			},
		}
	}

	apiClient, err := client.NewClient(host, os.Getenv("DOCKER_API_VERSION"), httpClient, nil)
	if err != nil {
		return DockerClient{}, err
	}

	return DockerClient{
		Client:  apiClient,
		options: co,
	}, nil
}
Пример #28
0
func main() {

	runContainer()

	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 {
		panic(err)
	}

	options := types.ContainerListOptions{All: true}
	containers, err := cli.ContainerList(context.Background(), options)
	if err != nil {
		panic(err)
	}

	for _, c := range containers {
		fmt.Println(c.ID)
		fmt.Println(c.Names)
		// fmt.Println(c)
	}
}
Пример #29
0
func runContainer() {
	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 {
		panic(err)
	}

	r, err := cli.ContainerCreate(context.Background(),
		&container.Config{
			Image: "ubuntu", Hostname: "",
			User:         "",
			AttachStdin:  true,
			AttachStdout: true,
			AttachStderr: true,
			Tty:          true,
			Cmd:          []string{"/bin/bash"},
			OpenStdin:    false,
			StdinOnce:    true,
			WorkingDir:   ""}, nil, nil, "")
	if err != nil {
		panic(err)
	}
	if r.ID != "container_id" {
		fmt.Printf("expected `container_id`, got %s \n", r.ID)
		fmt.Println(err)
		fmt.Println(r)
		fmt.Println()
		fmt.Println()
	}

	err = cli.ContainerStart(context.Background(), r.ID)
	if err != nil {
		panic(err)
	}

}
Пример #30
0
// NewAPIClientFromFlags creates a new APIClient from command line flags
func NewAPIClientFromFlags(clientFlags *cliflags.ClientFlags, configFile *configfile.ConfigFile) (client.APIClient, error) {
	host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
	if err != nil {
		return &client.Client{}, err
	}

	customHeaders := configFile.HTTPHeaders
	if customHeaders == nil {
		customHeaders = map[string]string{}
	}
	customHeaders["User-Agent"] = clientUserAgent()

	verStr := api.DefaultVersion
	if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
		verStr = tmpStr
	}

	httpClient, err := newHTTPClient(host, clientFlags.Common.TLSOptions)
	if err != nil {
		return &client.Client{}, err
	}

	return client.NewClient(host, verStr, httpClient, customHeaders)
}