api.go 7.2 KB

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