api.go 7.2 KB

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