api.go 7.9 KB

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