api.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "io"
  17. "github.com/compose-spec/compose-go/types"
  18. )
  19. // Service manages a compose project
  20. type Service interface {
  21. // Build executes the equivalent to a `compose build`
  22. Build(ctx context.Context, project *types.Project) error
  23. // Push executes the equivalent ot a `compose push`
  24. Push(ctx context.Context, project *types.Project) error
  25. // Pull executes the equivalent of a `compose pull`
  26. Pull(ctx context.Context, project *types.Project) error
  27. // Create executes the equivalent to a `compose create`
  28. Create(ctx context.Context, project *types.Project) error
  29. // Start executes the equivalent to a `compose start`
  30. Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
  31. // Up executes the equivalent to a `compose up`
  32. Up(ctx context.Context, project *types.Project, options UpOptions) error
  33. // Down executes the equivalent to a `compose down`
  34. Down(ctx context.Context, projectName string, options DownOptions) error
  35. // Logs executes the equivalent to a `compose logs`
  36. Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
  37. // Ps executes the equivalent to a `compose ps`
  38. Ps(ctx context.Context, projectName string) ([]ContainerSummary, error)
  39. // List executes the equivalent to a `docker stack ls`
  40. List(ctx context.Context, projectName string) ([]Stack, error)
  41. // Convert translate compose model into backend's native format
  42. Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
  43. // CreateOneOffContainer creates a service oneoff container and starts its dependencies
  44. CreateOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
  45. // Run attaches to and starts a one-off container
  46. Run(ctx context.Context, container string, detach bool) error
  47. }
  48. // UpOptions group options of the Up API
  49. type UpOptions struct {
  50. // Detach will create services and return immediately
  51. Detach bool
  52. }
  53. // DownOptions group options of the Down API
  54. type DownOptions struct {
  55. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  56. RemoveOrphans bool
  57. }
  58. // ConvertOptions group options of the Convert API
  59. type ConvertOptions struct {
  60. // Format define the output format used to dump converted application model (json|yaml)
  61. Format string
  62. }
  63. // RunOptions holds all flags for compose run
  64. type RunOptions struct {
  65. Name string
  66. Command []string
  67. WorkingDir string
  68. Environment []string
  69. Publish []string
  70. Labels []string
  71. Volumes []string
  72. Remove bool
  73. NoDeps bool
  74. LogConsumer LogConsumer
  75. Detach bool
  76. Stdout io.ReadCloser
  77. Stdin io.WriteCloser
  78. }
  79. // PortPublisher hold status about published port
  80. type PortPublisher struct {
  81. URL string
  82. TargetPort int
  83. PublishedPort int
  84. Protocol string
  85. }
  86. // ContainerSummary hold high-level description of a container
  87. type ContainerSummary struct {
  88. ID string
  89. Name string
  90. Project string
  91. Service string
  92. State string
  93. Publishers []PortPublisher
  94. }
  95. // ServiceStatus hold status about a service
  96. type ServiceStatus struct {
  97. ID string
  98. Name string
  99. Replicas int
  100. Desired int
  101. Ports []string
  102. Publishers []PortPublisher
  103. }
  104. // LogOptions defines optional parameters for the `Log` API
  105. type LogOptions struct {
  106. Services []string
  107. }
  108. const (
  109. // STARTING indicates that stack is being deployed
  110. STARTING string = "Starting"
  111. // RUNNING indicates that stack is deployed and services are running
  112. RUNNING string = "Running"
  113. // UPDATING indicates that some stack resources are being recreated
  114. UPDATING string = "Updating"
  115. // REMOVING indicates that stack is being deleted
  116. REMOVING string = "Removing"
  117. // UNKNOWN indicates unknown stack state
  118. UNKNOWN string = "Unknown"
  119. // FAILED indicates that stack deployment failed
  120. FAILED string = "Failed"
  121. )
  122. // Stack holds the name and state of a compose application/stack
  123. type Stack struct {
  124. ID string
  125. Name string
  126. Status string
  127. Reason string
  128. }
  129. // LogConsumer is a callback to process log messages from services
  130. type LogConsumer interface {
  131. Log(service, container, message string)
  132. }