1
0

api.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // Images remove image used by services. 'all': Remove all images. 'local': Remove only images that don't have a tag
  86. Images string
  87. // Volumes remove volumes, both declared in the `volumes` section and anonymous ones
  88. Volumes bool
  89. }
  90. // ConvertOptions group options of the Convert API
  91. type ConvertOptions struct {
  92. // Format define the output format used to dump converted application model (json|yaml)
  93. Format string
  94. // Output defines the path to save the application model
  95. Output string
  96. }
  97. // KillOptions group options of the Kill API
  98. type KillOptions struct {
  99. // Signal to send to containers
  100. Signal string
  101. }
  102. // RemoveOptions group options of the Remove API
  103. type RemoveOptions struct {
  104. // DryRun just list removable resources
  105. DryRun bool
  106. // Volumes remove anonymous volumes
  107. Volumes bool
  108. // Force don't ask to confirm removal
  109. Force bool
  110. }
  111. // RunOptions options to execute compose run
  112. type RunOptions struct {
  113. Service string
  114. Command []string
  115. Detach bool
  116. AutoRemove bool
  117. Writer io.Writer
  118. Reader io.Reader
  119. // used by exec
  120. Tty bool
  121. WorkingDir string
  122. User string
  123. Environment []string
  124. Privileged bool
  125. Index int
  126. }
  127. // PsOptions group options of the Ps API
  128. type PsOptions struct {
  129. All bool
  130. }
  131. // PortPublisher hold status about published port
  132. type PortPublisher struct {
  133. URL string
  134. TargetPort int
  135. PublishedPort int
  136. Protocol string
  137. }
  138. // ContainerSummary hold high-level description of a container
  139. type ContainerSummary struct {
  140. ID string
  141. Name string
  142. Project string
  143. Service string
  144. State string
  145. Health string
  146. Publishers []PortPublisher
  147. }
  148. // ServiceStatus hold status about a service
  149. type ServiceStatus struct {
  150. ID string
  151. Name string
  152. Replicas int
  153. Desired int
  154. Ports []string
  155. Publishers []PortPublisher
  156. }
  157. // LogOptions defines optional parameters for the `Log` API
  158. type LogOptions struct {
  159. Services []string
  160. Tail string
  161. Follow bool
  162. }
  163. const (
  164. // STARTING indicates that stack is being deployed
  165. STARTING string = "Starting"
  166. // RUNNING indicates that stack is deployed and services are running
  167. RUNNING string = "Running"
  168. // UPDATING indicates that some stack resources are being recreated
  169. UPDATING string = "Updating"
  170. // REMOVING indicates that stack is being deleted
  171. REMOVING string = "Removing"
  172. // UNKNOWN indicates unknown stack state
  173. UNKNOWN string = "Unknown"
  174. // FAILED indicates that stack deployment failed
  175. FAILED string = "Failed"
  176. )
  177. const (
  178. // RecreateDiverged to recreate services which configuration diverges from compose model
  179. RecreateDiverged = "diverged"
  180. // RecreateForce to force service container being recreated
  181. RecreateForce = "force"
  182. // RecreateNever to never recreate existing service containers
  183. RecreateNever = "never"
  184. )
  185. // Stack holds the name and state of a compose application/stack
  186. type Stack struct {
  187. ID string
  188. Name string
  189. Status string
  190. Reason string
  191. }
  192. // LogConsumer is a callback to process log messages from services
  193. type LogConsumer interface {
  194. Log(name, container, message string)
  195. Status(name, container, msg string)
  196. Register(name string, source string)
  197. }
  198. // ContainerEventListener is a callback to process ContainerEvent from services
  199. type ContainerEventListener func(event ContainerEvent)
  200. // ContainerEvent notify an event has been collected on Source container implementing Service
  201. type ContainerEvent struct {
  202. Type int
  203. Source string
  204. Service string
  205. Name string
  206. Line string
  207. ExitCode int
  208. }
  209. const (
  210. // ContainerEventLog is a ContainerEvent of type log. Line is set
  211. ContainerEventLog = iota
  212. // ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
  213. ContainerEventAttach
  214. // ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
  215. ContainerEventExit
  216. )