backend.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // +build example
  2. /*
  3. Copyright 2020 Docker, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package example
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "github.com/docker/api/context/cloud"
  21. "github.com/docker/api/backend"
  22. "github.com/docker/api/compose"
  23. "github.com/docker/api/containers"
  24. )
  25. type apiService struct {
  26. containerService
  27. composeService
  28. }
  29. func (a *apiService) ContainerService() containers.Service {
  30. return &a.containerService
  31. }
  32. func (a *apiService) ComposeService() compose.Service {
  33. return &a.composeService
  34. }
  35. func init() {
  36. backend.Register("example", "example", service, cloud.NotImplementedCloudService)
  37. }
  38. func service(ctx context.Context) (backend.Service, error) {
  39. return &apiService{}, nil
  40. }
  41. type containerService struct{}
  42. func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
  43. return containers.Container{
  44. ID: "id",
  45. Image: "nginx",
  46. }, nil
  47. }
  48. func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  49. result := []containers.Container{
  50. {
  51. ID: "id",
  52. Image: "nginx",
  53. },
  54. {
  55. ID: "1234",
  56. Image: "alpine",
  57. },
  58. }
  59. if all {
  60. result = append(result, containers.Container{
  61. ID: "stopped",
  62. Image: "nginx",
  63. })
  64. }
  65. return result, nil
  66. }
  67. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  68. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  69. return nil
  70. }
  71. func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  72. return errors.New("not implemented")
  73. }
  74. func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  75. fmt.Printf("Executing command %q on container %q", command, name)
  76. return nil
  77. }
  78. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  79. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  80. return nil
  81. }
  82. func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
  83. fmt.Printf("Deleting container %q with force = %t\n", id, force)
  84. return nil
  85. }
  86. type composeService struct{}
  87. func (cs *composeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  88. prj, err := compose.ProjectFromOptions(&opts)
  89. if err != nil {
  90. return err
  91. }
  92. fmt.Printf("Up command on project %q", prj.Name)
  93. return nil
  94. }
  95. func (cs *composeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  96. prj, err := compose.ProjectFromOptions(&opts)
  97. if err != nil {
  98. return err
  99. }
  100. fmt.Printf("Down command on project %q", prj.Name)
  101. return nil
  102. }