backend.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package moby
  2. import (
  3. "context"
  4. "io"
  5. "github.com/docker/api/context/cloud"
  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. Ports: getPorts(container.Ports),
  57. })
  58. }
  59. return result, nil
  60. }
  61. func (ms *mobyService) Run(ctx context.Context, r containers.ContainerConfig) error {
  62. create, err := ms.apiClient.ContainerCreate(ctx, &container.Config{
  63. Image: r.Image,
  64. }, nil, nil, r.ID)
  65. if err != nil {
  66. return err
  67. }
  68. return ms.apiClient.ContainerStart(ctx, create.ID, types.ContainerStartOptions{})
  69. }
  70. func (ms *mobyService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  71. cec, err := ms.apiClient.ContainerExecCreate(ctx, name, types.ExecConfig{
  72. Cmd: []string{command},
  73. Tty: true,
  74. AttachStderr: true,
  75. AttachStdin: true,
  76. AttachStdout: true,
  77. })
  78. if err != nil {
  79. return err
  80. }
  81. resp, err := ms.apiClient.ContainerExecAttach(ctx, cec.ID, types.ExecStartCheck{})
  82. if err != nil {
  83. return err
  84. }
  85. defer resp.Close()
  86. readChannel := make(chan error, 10)
  87. writeChannel := make(chan error, 10)
  88. go func() {
  89. _, err := io.Copy(writer, resp.Reader)
  90. readChannel <- err
  91. }()
  92. go func() {
  93. _, err := io.Copy(resp.Conn, reader)
  94. writeChannel <- err
  95. }()
  96. for {
  97. select {
  98. case err := <-readChannel:
  99. return err
  100. case err := <-writeChannel:
  101. return err
  102. }
  103. }
  104. }
  105. func (ms *mobyService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  106. r, err := ms.apiClient.ContainerLogs(ctx, containerName, types.ContainerLogsOptions{
  107. ShowStdout: true,
  108. ShowStderr: true,
  109. })
  110. if err != nil {
  111. return err
  112. }
  113. _, err = io.Copy(request.Writer, r)
  114. return err
  115. }
  116. func (ms *mobyService) Delete(ctx context.Context, containerID string, force bool) error {
  117. err := ms.apiClient.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{
  118. Force: force,
  119. })
  120. if client.IsErrNotFound(err) {
  121. return errors.Wrapf(errdefs.ErrNotFound, "container %q", containerID)
  122. }
  123. return err
  124. }
  125. func getPorts(ports []types.Port) []containers.Port {
  126. result := []containers.Port{}
  127. for _, port := range ports {
  128. result = append(result, containers.Port{
  129. ContainerPort: uint32(port.PrivatePort),
  130. HostPort: uint32(port.PublicPort),
  131. HostIP: port.IP,
  132. Protocol: port.Type,
  133. })
  134. }
  135. return result
  136. }