api.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, opts CreateOptions) error
  29. // Start executes the equivalent to a `compose start`
  30. Start(ctx context.Context, project *types.Project, options StartOptions) error
  31. // Stop executes the equivalent to a `compose stop`
  32. Stop(ctx context.Context, project *types.Project) error
  33. // Up executes the equivalent to a `compose up`
  34. Up(ctx context.Context, project *types.Project, options UpOptions) error
  35. // Down executes the equivalent to a `compose down`
  36. Down(ctx context.Context, projectName string, options DownOptions) error
  37. // Logs executes the equivalent to a `compose logs`
  38. Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
  39. // Ps executes the equivalent to a `compose ps`
  40. Ps(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
  41. // List executes the equivalent to a `docker stack ls`
  42. List(ctx context.Context) ([]Stack, error)
  43. // Convert translate compose model into backend's native format
  44. Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
  45. // Kill executes the equivalent to a `compose kill`
  46. Kill(ctx context.Context, project *types.Project, options KillOptions) error
  47. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  48. RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
  49. }
  50. // CreateOptions group options of the Create API
  51. type CreateOptions struct {
  52. // Remove legacy containers for services that are not defined in the project
  53. RemoveOrphans bool
  54. // Recreate define the strategy to apply on existing containers
  55. Recreate string
  56. }
  57. // StartOptions group options of the Start API
  58. type StartOptions struct {
  59. // Attach will attach to service containers and send container logs and events
  60. Attach ContainerEventListener
  61. }
  62. // UpOptions group options of the Up API
  63. type UpOptions struct {
  64. // Detach will create services and return immediately
  65. Detach bool
  66. }
  67. // DownOptions group options of the Down API
  68. type DownOptions struct {
  69. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  70. RemoveOrphans bool
  71. // Project is the compose project used to define this app. Might be nil if user ran `down` just with project name
  72. Project *types.Project
  73. }
  74. // ConvertOptions group options of the Convert API
  75. type ConvertOptions struct {
  76. // Format define the output format used to dump converted application model (json|yaml)
  77. Format string
  78. // Output defines the path to save the application model
  79. Output string
  80. }
  81. // KillOptions group options of the Kill API
  82. type KillOptions struct {
  83. // Signal to send to containers
  84. Signal string
  85. }
  86. // RunOptions options to execute compose run
  87. type RunOptions struct {
  88. Service string
  89. Command []string
  90. Detach bool
  91. AutoRemove bool
  92. Writer io.Writer
  93. Reader io.Reader
  94. }
  95. // PsOptions group options of the Ps API
  96. type PsOptions struct {
  97. All bool
  98. }
  99. // PortPublisher hold status about published port
  100. type PortPublisher struct {
  101. URL string
  102. TargetPort int
  103. PublishedPort int
  104. Protocol string
  105. }
  106. // ContainerSummary hold high-level description of a container
  107. type ContainerSummary struct {
  108. ID string
  109. Name string
  110. Project string
  111. Service string
  112. State string
  113. Health string
  114. Publishers []PortPublisher
  115. }
  116. // ServiceStatus hold status about a service
  117. type ServiceStatus struct {
  118. ID string
  119. Name string
  120. Replicas int
  121. Desired int
  122. Ports []string
  123. Publishers []PortPublisher
  124. }
  125. // LogOptions defines optional parameters for the `Log` API
  126. type LogOptions struct {
  127. Services []string
  128. Tail string
  129. Follow bool
  130. }
  131. const (
  132. // STARTING indicates that stack is being deployed
  133. STARTING string = "Starting"
  134. // RUNNING indicates that stack is deployed and services are running
  135. RUNNING string = "Running"
  136. // UPDATING indicates that some stack resources are being recreated
  137. UPDATING string = "Updating"
  138. // REMOVING indicates that stack is being deleted
  139. REMOVING string = "Removing"
  140. // UNKNOWN indicates unknown stack state
  141. UNKNOWN string = "Unknown"
  142. // FAILED indicates that stack deployment failed
  143. FAILED string = "Failed"
  144. )
  145. const (
  146. // RecreateDiverged to recreate services which configuration diverges from compose model
  147. RecreateDiverged = "diverged"
  148. // RecreateForce to force service container being recreated
  149. RecreateForce = "force"
  150. // RecreateNever to never recreate existing service containers
  151. RecreateNever = "never"
  152. )
  153. // Stack holds the name and state of a compose application/stack
  154. type Stack struct {
  155. ID string
  156. Name string
  157. Status string
  158. Reason string
  159. }
  160. // LogConsumer is a callback to process log messages from services
  161. type LogConsumer interface {
  162. Log(service, container, message string)
  163. Status(service, container, msg string)
  164. }
  165. // ContainerEventListener is a callback to process ContainerEvent from services
  166. type ContainerEventListener func(event ContainerEvent)
  167. // ContainerEvent notify an event has been collected on Source container implementing Service
  168. type ContainerEvent struct {
  169. Type int
  170. Source string
  171. Service string
  172. Line string
  173. ExitCode int
  174. }
  175. const (
  176. // ContainerEventLog is a ContainerEvent of type log. Line is set
  177. ContainerEventLog = iota
  178. // ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
  179. ContainerEventExit
  180. )