api.go 7.5 KB

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