api.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, consumer LogConsumer) 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) ([]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. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  46. RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) error
  47. }
  48. // CreateOptions group options of the Create API
  49. type CreateOptions struct {
  50. // Remove legacy containers for services that are not defined in the project
  51. RemoveOrphans bool
  52. // Recreate define the strategy to apply on existing containers
  53. Recreate string
  54. }
  55. // UpOptions group options of the Up API
  56. type UpOptions struct {
  57. // Detach will create services and return immediately
  58. Detach bool
  59. }
  60. // DownOptions group options of the Down API
  61. type DownOptions struct {
  62. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  63. RemoveOrphans bool
  64. // Project is the compose project used to define this app. Might be nil if user ran `down` just with project name
  65. Project *types.Project
  66. }
  67. // ConvertOptions group options of the Convert API
  68. type ConvertOptions struct {
  69. // Format define the output format used to dump converted application model (json|yaml)
  70. Format string
  71. }
  72. // RunOptions options to execute compose run
  73. type RunOptions struct {
  74. Service string
  75. Command []string
  76. Detach bool
  77. AutoRemove bool
  78. Writer io.Writer
  79. Reader io.Reader
  80. }
  81. // PortPublisher hold status about published port
  82. type PortPublisher struct {
  83. URL string
  84. TargetPort int
  85. PublishedPort int
  86. Protocol string
  87. }
  88. // ContainerSummary hold high-level description of a container
  89. type ContainerSummary struct {
  90. ID string
  91. Name string
  92. Project string
  93. Service string
  94. State string
  95. Health string
  96. Publishers []PortPublisher
  97. }
  98. // ServiceStatus hold status about a service
  99. type ServiceStatus struct {
  100. ID string
  101. Name string
  102. Replicas int
  103. Desired int
  104. Ports []string
  105. Publishers []PortPublisher
  106. }
  107. // LogOptions defines optional parameters for the `Log` API
  108. type LogOptions struct {
  109. Services []string
  110. Tail string
  111. Follow bool
  112. }
  113. const (
  114. // STARTING indicates that stack is being deployed
  115. STARTING string = "Starting"
  116. // RUNNING indicates that stack is deployed and services are running
  117. RUNNING string = "Running"
  118. // UPDATING indicates that some stack resources are being recreated
  119. UPDATING string = "Updating"
  120. // REMOVING indicates that stack is being deleted
  121. REMOVING string = "Removing"
  122. // UNKNOWN indicates unknown stack state
  123. UNKNOWN string = "Unknown"
  124. // FAILED indicates that stack deployment failed
  125. FAILED string = "Failed"
  126. )
  127. const (
  128. // RecreateDiverged to recreate services which configuration diverges from compose model
  129. RecreateDiverged = "diverged"
  130. // RecreateForce to force service container being recreated
  131. RecreateForce = "force"
  132. // RecreateNever to never recreate existing service containers
  133. RecreateNever = "never"
  134. )
  135. // Stack holds the name and state of a compose application/stack
  136. type Stack struct {
  137. ID string
  138. Name string
  139. Status string
  140. Reason string
  141. }
  142. // LogConsumer is a callback to process log messages from services
  143. type LogConsumer interface {
  144. Log(service, container, message string)
  145. }