Esempio n. 1
0
// NewClient returns a new instance of the HTTP client
func NewClient() Client {
	defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
	cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, defaultHeaders)

	if err != nil {
		log.Panic("Failed to create Docker client: ", err)
	}

	return Client(&HTTPClient{
		cli: cli,
	})
}
Esempio n. 2
0
// NewAPIClientFromFlags creates a new APIClient from command line flags
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
	host, err := getServerHost(opts.Hosts, opts.TLSOptions)
	if err != nil {
		return &client.Client{}, err
	}

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

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

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

	return client.NewClient(host, verStr, httpClient, customHeaders)
}
Esempio n. 3
0
			if cmd.Flags().Changed("unlock-key") {
				unlockKeyString, err := cmd.Flags().GetString("unlock-key")
				if err != nil {
					return err
				}
				unlockKey, err = encryption.ParseHumanReadableKey(unlockKeyString)
				if err != nil {
					return err
				}
			}

			// Create a cancellable context for our GRPC call
			ctx, cancel := context.WithCancel(ctx)
			defer cancel()

			client, err := engineapi.NewClient(engineAddr, "", nil, nil)
			if err != nil {
				return err
			}

			executor := container.NewExecutor(client)

			if debugAddr != "" {
				go func() {
					// setup listening to give access to pprof, expvar, etc.
					if err := http.ListenAndServe(debugAddr, nil); err != nil {
						panic(err)
					}
				}()
			}
// 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",
		ServiceAnnotations: api.Annotations{
			Name: "dockerexec-integration",
		},
		Spec: api.TaskSpec{
			Runtime: &api.TaskSpec_Container{
				Container: &api.ContainerSpec{
					Command: []string{"sh", "-c", "sleep 5; echo hello; echo stderr >&2"},
					Image:   "alpine",
				},
			},
		},
	}

	var receivedLogs bool
	publisher := exec.LogPublisherFunc(func(ctx context.Context, message api.LogMessage) error {
		receivedLogs = true

		switch message.Stream {
		case api.LogStreamStdout:
			assert.Equal(t, "hello\n", string(message.Data))
		case api.LogStreamStderr:
			assert.Equal(t, "stderr\n", string(message.Data))
		}

		t.Log(message)
		return nil
	})

	ctlr, err := newController(client, task, nil)
	assert.NoError(t, err)
	assert.NotNil(t, ctlr)
	assert.NoError(t, ctlr.Prepare(ctx))
	assert.NoError(t, ctlr.Start(ctx))
	assert.NoError(t, ctlr.(exec.ControllerLogs).Logs(ctx, publisher, api.LogSubscriptionOptions{
		Follow: true,
	}))
	assert.NoError(t, ctlr.Wait(ctx))
	assert.True(t, receivedLogs)
	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)
	}
}
Esempio n. 5
0
// Create creates a docker client based on the specified options.
func Create(c Options) (client.APIClient, error) {
	if c.Host == "" {
		if os.Getenv("DOCKER_API_VERSION") == "" {
			os.Setenv("DOCKER_API_VERSION", DefaultAPIVersion)
		}
		client, err := client.NewEnvClient()
		if err != nil {
			return nil, err
		}
		return client, nil
	}

	apiVersion := c.APIVersion
	if apiVersion == "" {
		apiVersion = DefaultAPIVersion
	}

	if c.TLSOptions.CAFile == "" {
		c.TLSOptions.CAFile = filepath.Join(dockerCertPath, defaultCaFile)
	}
	if c.TLSOptions.CertFile == "" {
		c.TLSOptions.CertFile = filepath.Join(dockerCertPath, defaultCertFile)
	}
	if c.TLSOptions.KeyFile == "" {
		c.TLSOptions.KeyFile = filepath.Join(dockerCertPath, defaultKeyFile)
	}
	if c.TrustKey == "" {
		c.TrustKey = filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
	}
	if c.TLSVerify {
		c.TLS = true
	}
	if c.TLS {
		c.TLSOptions.InsecureSkipVerify = !c.TLSVerify
	}

	var httpClient *http.Client
	if c.TLS {
		config, err := tlsconfig.Client(c.TLSOptions)
		if err != nil {
			return nil, err
		}
		tr := &http.Transport{
			TLSClientConfig: config,
		}
		proto, addr, _, err := client.ParseHost(c.Host)
		if err != nil {
			return nil, err
		}

		if err := sockets.ConfigureTransport(tr, proto, addr); err != nil {
			return nil, err
		}

		httpClient = &http.Client{
			Transport: tr,
		}
	}

	customHeaders := map[string]string{}
	customHeaders["User-Agent"] = fmt.Sprintf("Libcompose-Client/%s (%s)", version.VERSION, runtime.GOOS)

	client, err := client.NewClient(c.Host, apiVersion, httpClient, customHeaders)
	if err != nil {
		return nil, err
	}
	return client, nil
}
Esempio n. 6
0
func NewSupervisorDefault(host, version string, dedot bool) (*Supervisor, error) {
	cli, err := client.NewClient(host, version, nil, nil)
	return &Supervisor{cli: cli, dedot: dedot}, err
}