api.go 7.0 KB

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