backend.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package moby
  2. import (
  3. "context"
  4. "github.com/docker/api/context/cloud"
  5. "io"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/client"
  9. "github.com/pkg/errors"
  10. "github.com/docker/api/backend"
  11. "github.com/docker/api/compose"
  12. "github.com/docker/api/containers"
  13. "github.com/docker/api/errdefs"
  14. )
  15. type mobyService struct {
  16. apiClient *client.Client
  17. }
  18. func init() {
  19. backend.Register("moby", "moby", func(ctx context.Context) (backend.Service, error) {
  20. return New()
  21. })
  22. }
  23. // New returns a moby backend implementation
  24. func New() (backend.Service, error) {
  25. apiClient, err := client.NewClientWithOpts(client.FromEnv)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &mobyService{
  30. apiClient,
  31. }, nil
  32. }
  33. func (ms *mobyService) ContainerService() containers.Service {
  34. return ms
  35. }
  36. func (ms *mobyService) ComposeService() compose.Service {
  37. return nil
  38. }
  39. func (ms *mobyService) CloudService() cloud.Service {
  40. return nil
  41. }
  42. func (ms *mobyService) List(ctx context.Context) ([]containers.Container, error) {
  43. css, err := ms.apiClient.ContainerList(ctx, types.ContainerListOptions{
  44. All: false,
  45. })
  46. if err != nil {
  47. return []containers.Container{}, err
  48. }
  49. var result []containers.Container
  50. for _, container := range css {
  51. result = append(result, containers.Container{
  52. ID: container.ID,
  53. Image: container.Image,
  54. Status: container.Status,
  55. Command: container.Command,
  56. })
  57. }
  58. return result, nil
  59. }
  60. func (ms *mobyService) Run(ctx context.Context, r containers.ContainerConfig) error {
  61. create, err := ms.apiClient.ContainerCreate(ctx, &container.Config{
  62. Image: r.Image,
  63. }, nil, nil, r.ID)
  64. if err != nil {
  65. return err
  66. }
  67. return ms.apiClient.ContainerStart(ctx, create.ID, types.ContainerStartOptions{})
  68. }
  69. func (ms *mobyService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  70. cec, err := ms.apiClient.ContainerExecCreate(ctx, name, types.ExecConfig{
  71. Cmd: []string{command},
  72. Tty: true,
  73. AttachStderr: true,
  74. AttachStdin: true,
  75. AttachStdout: true,
  76. })
  77. if err != nil {
  78. return err
  79. }
  80. resp, err := ms.apiClient.ContainerExecAttach(ctx, cec.ID, types.ExecStartCheck{})
  81. if err != nil {
  82. return err
  83. }
  84. defer resp.Close()
  85. readChannel := make(chan error, 10)
  86. writeChannel := make(chan error, 10)
  87. go func() {
  88. _, err := io.Copy(writer, resp.Reader)
  89. readChannel <- err
  90. }()
  91. go func() {
  92. _, err := io.Copy(resp.Conn, reader)
  93. writeChannel <- err
  94. }()
  95. for {
  96. select {
  97. case err := <-readChannel:
  98. return err
  99. case err := <-writeChannel:
  100. return err
  101. }
  102. }
  103. }
  104. func (ms *mobyService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  105. r, err := ms.apiClient.ContainerLogs(ctx, containerName, types.ContainerLogsOptions{
  106. ShowStdout: true,
  107. ShowStderr: true,
  108. })
  109. if err != nil {
  110. return err
  111. }
  112. _, err = io.Copy(request.Writer, r)
  113. return err
  114. }
  115. func (ms *mobyService) Delete(ctx context.Context, containerID string, force bool) error {
  116. err := ms.apiClient.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{
  117. Force: force,
  118. })
  119. if client.IsErrNotFound(err) {
  120. return errors.Wrapf(errdefs.ErrNotFound, "container %q", containerID)
  121. }
  122. return err
  123. }