backend.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "github.com/compose-spec/compose-go/cli"
  20. "github.com/docker/api/context/cloud"
  21. "github.com/docker/api/errdefs"
  22. "github.com/docker/api/backend"
  23. "github.com/docker/api/compose"
  24. "github.com/docker/api/containers"
  25. ecstypes "github.com/docker/ecs-plugin/pkg/compose"
  26. )
  27. type apiService struct {
  28. containerService
  29. composeService
  30. }
  31. func (a *apiService) ContainerService() containers.Service {
  32. return &a.containerService
  33. }
  34. func (a *apiService) ComposeService() compose.Service {
  35. return &a.composeService
  36. }
  37. func init() {
  38. backend.Register("example", "example", service, cloud.NotImplementedCloudService)
  39. }
  40. func service(ctx context.Context) (backend.Service, error) {
  41. return &apiService{}, nil
  42. }
  43. type containerService struct{}
  44. func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
  45. return containers.Container{
  46. ID: "id",
  47. Image: "nginx",
  48. Platform: "Linux",
  49. RestartPolicyCondition: "none",
  50. }, nil
  51. }
  52. func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  53. result := []containers.Container{
  54. {
  55. ID: "id",
  56. Image: "nginx",
  57. },
  58. {
  59. ID: "1234",
  60. Image: "alpine",
  61. },
  62. }
  63. if all {
  64. result = append(result, containers.Container{
  65. ID: "stopped",
  66. Image: "nginx",
  67. })
  68. }
  69. return result, nil
  70. }
  71. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  72. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  73. return nil
  74. }
  75. func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  76. return errors.New("not implemented")
  77. }
  78. func (cs *containerService) Exec(ctx context.Context, name string, request containers.ExecRequest) error {
  79. fmt.Printf("Executing command %q on container %q", request.Command, name)
  80. return nil
  81. }
  82. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  83. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  84. return nil
  85. }
  86. func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
  87. fmt.Printf("Deleting container %q with force = %t\n", id, force)
  88. return nil
  89. }
  90. type composeService struct{}
  91. func (cs *composeService) Up(ctx context.Context, opts cli.ProjectOptions) error {
  92. prj, err := cli.ProjectFromOptions(&opts)
  93. if err != nil {
  94. return err
  95. }
  96. fmt.Printf("Up command on project %q", prj.Name)
  97. return nil
  98. }
  99. func (cs *composeService) Down(ctx context.Context, opts cli.ProjectOptions) error {
  100. prj, err := cli.ProjectFromOptions(&opts)
  101. if err != nil {
  102. return err
  103. }
  104. fmt.Printf("Down command on project %q", prj.Name)
  105. return nil
  106. }
  107. func (cs *composeService) Ps(ctx context.Context, opts cli.ProjectOptions) ([]ecstypes.ServiceStatus, error) {
  108. return nil, errdefs.ErrNotImplemented
  109. }
  110. func (cs *composeService) Logs(ctx context.Context, opts cli.ProjectOptions) error {
  111. return errdefs.ErrNotImplemented
  112. }