api.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. "strings"
  18. "time"
  19. "github.com/compose-spec/compose-go/types"
  20. )
  21. // Service manages a compose project
  22. type Service interface {
  23. // Build executes the equivalent to a `compose build`
  24. Build(ctx context.Context, project *types.Project, options BuildOptions) error
  25. // Push executes the equivalent ot a `compose push`
  26. Push(ctx context.Context, project *types.Project) error
  27. // Pull executes the equivalent of a `compose pull`
  28. Pull(ctx context.Context, project *types.Project) error
  29. // Create executes the equivalent to a `compose create`
  30. Create(ctx context.Context, project *types.Project, opts CreateOptions) error
  31. // Start executes the equivalent to a `compose start`
  32. Start(ctx context.Context, project *types.Project, options StartOptions) error
  33. // Stop executes the equivalent to a `compose stop`
  34. Stop(ctx context.Context, project *types.Project, options StopOptions) error
  35. // Up executes the equivalent to a `compose up`
  36. Up(ctx context.Context, project *types.Project, options UpOptions) error
  37. // Down executes the equivalent to a `compose down`
  38. Down(ctx context.Context, projectName string, options DownOptions) error
  39. // Logs executes the equivalent to a `compose logs`
  40. Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
  41. // Ps executes the equivalent to a `compose ps`
  42. Ps(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
  43. // List executes the equivalent to a `docker stack ls`
  44. List(ctx context.Context, options ListOptions) ([]Stack, error)
  45. // Convert translate compose model into backend's native format
  46. Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
  47. // Kill executes the equivalent to a `compose kill`
  48. Kill(ctx context.Context, project *types.Project, options KillOptions) error
  49. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  50. RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
  51. // Remove executes the equivalent to a `compose rm`
  52. Remove(ctx context.Context, project *types.Project, options RemoveOptions) ([]string, error)
  53. // Exec executes a command in a running service container
  54. Exec(ctx context.Context, project *types.Project, opts RunOptions) error
  55. // Pause executes the equivalent to a `compose pause`
  56. Pause(ctx context.Context, project *types.Project) error
  57. // UnPause executes the equivalent to a `compose unpause`
  58. UnPause(ctx context.Context, project *types.Project) error
  59. }
  60. // BuildOptions group options of the Build API
  61. type BuildOptions struct {
  62. // Pull always attempt to pull a newer version of the image
  63. Pull bool
  64. }
  65. // CreateOptions group options of the Create API
  66. type CreateOptions struct {
  67. // Remove legacy containers for services that are not defined in the project
  68. RemoveOrphans bool
  69. // Recreate define the strategy to apply on existing containers
  70. Recreate string
  71. }
  72. // StartOptions group options of the Start API
  73. type StartOptions struct {
  74. // Attach will attach to service containers and send container logs and events
  75. Attach ContainerEventListener
  76. }
  77. // StopOptions group options of the Stop API
  78. type StopOptions struct {
  79. // Timeout override container stop timeout
  80. Timeout *time.Duration
  81. }
  82. // UpOptions group options of the Up API
  83. type UpOptions struct {
  84. // Detach will create services and return immediately
  85. Detach bool
  86. }
  87. // DownOptions group options of the Down API
  88. type DownOptions struct {
  89. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  90. RemoveOrphans bool
  91. // Project is the compose project used to define this app. Might be nil if user ran `down` just with project name
  92. Project *types.Project
  93. // Timeout override container stop timeout
  94. Timeout *time.Duration
  95. // Images remove image used by services. 'all': Remove all images. 'local': Remove only images that don't have a tag
  96. Images string
  97. // Volumes remove volumes, both declared in the `volumes` section and anonymous ones
  98. Volumes bool
  99. }
  100. // ConvertOptions group options of the Convert API
  101. type ConvertOptions struct {
  102. // Format define the output format used to dump converted application model (json|yaml)
  103. Format string
  104. // Output defines the path to save the application model
  105. Output string
  106. }
  107. // KillOptions group options of the Kill API
  108. type KillOptions struct {
  109. // Signal to send to containers
  110. Signal string
  111. }
  112. // RemoveOptions group options of the Remove API
  113. type RemoveOptions struct {
  114. // DryRun just list removable resources
  115. DryRun bool
  116. // Volumes remove anonymous volumes
  117. Volumes bool
  118. // Force don't ask to confirm removal
  119. Force bool
  120. }
  121. // RunOptions options to execute compose run
  122. type RunOptions struct {
  123. Name string
  124. Service string
  125. Command []string
  126. Entrypoint []string
  127. Detach bool
  128. AutoRemove bool
  129. Writer io.Writer
  130. Reader io.Reader
  131. Tty bool
  132. WorkingDir string
  133. User string
  134. Environment []string
  135. Labels types.Labels
  136. Privileged bool
  137. // used by exec
  138. Index int
  139. }
  140. // EnvironmentMap return RunOptions.Environment as a MappingWithEquals
  141. func (opts *RunOptions) EnvironmentMap() types.MappingWithEquals {
  142. environment := types.MappingWithEquals{}
  143. for _, s := range opts.Environment {
  144. parts := strings.SplitN(s, "=", 2)
  145. key := parts[0]
  146. switch {
  147. case len(parts) == 1:
  148. environment[key] = nil
  149. default:
  150. environment[key] = &parts[1]
  151. }
  152. }
  153. return environment
  154. }
  155. // ListOptions group options of the ls API
  156. type ListOptions struct {
  157. All bool
  158. }
  159. // PsOptions group options of the Ps API
  160. type PsOptions struct {
  161. All bool
  162. }
  163. // PortPublisher hold status about published port
  164. type PortPublisher struct {
  165. URL string
  166. TargetPort int
  167. PublishedPort int
  168. Protocol string
  169. }
  170. // ContainerSummary hold high-level description of a container
  171. type ContainerSummary struct {
  172. ID string
  173. Name string
  174. Project string
  175. Service string
  176. State string
  177. Health string
  178. Publishers []PortPublisher
  179. }
  180. // ServiceStatus hold status about a service
  181. type ServiceStatus struct {
  182. ID string
  183. Name string
  184. Replicas int
  185. Desired int
  186. Ports []string
  187. Publishers []PortPublisher
  188. }
  189. // LogOptions defines optional parameters for the `Log` API
  190. type LogOptions struct {
  191. Services []string
  192. Tail string
  193. Follow bool
  194. Timestamps bool
  195. }
  196. const (
  197. // STARTING indicates that stack is being deployed
  198. STARTING string = "Starting"
  199. // RUNNING indicates that stack is deployed and services are running
  200. RUNNING string = "Running"
  201. // UPDATING indicates that some stack resources are being recreated
  202. UPDATING string = "Updating"
  203. // REMOVING indicates that stack is being deleted
  204. REMOVING string = "Removing"
  205. // UNKNOWN indicates unknown stack state
  206. UNKNOWN string = "Unknown"
  207. // FAILED indicates that stack deployment failed
  208. FAILED string = "Failed"
  209. )
  210. const (
  211. // RecreateDiverged to recreate services which configuration diverges from compose model
  212. RecreateDiverged = "diverged"
  213. // RecreateForce to force service container being recreated
  214. RecreateForce = "force"
  215. // RecreateNever to never recreate existing service containers
  216. RecreateNever = "never"
  217. )
  218. // Stack holds the name and state of a compose application/stack
  219. type Stack struct {
  220. ID string
  221. Name string
  222. Status string
  223. Reason string
  224. }
  225. // LogConsumer is a callback to process log messages from services
  226. type LogConsumer interface {
  227. Log(name, service, container, message string)
  228. Status(name, container, msg string)
  229. Register(name string, source string)
  230. }
  231. // ContainerEventListener is a callback to process ContainerEvent from services
  232. type ContainerEventListener func(event ContainerEvent)
  233. // ContainerEvent notify an event has been collected on Source container implementing Service
  234. type ContainerEvent struct {
  235. Type int
  236. Source string
  237. Service string
  238. Name string
  239. Line string
  240. ExitCode int
  241. }
  242. const (
  243. // ContainerEventLog is a ContainerEvent of type log. Line is set
  244. ContainerEventLog = iota
  245. // ContainerEventAttach is a ContainerEvent of type attach. First event sent about a container
  246. ContainerEventAttach
  247. // ContainerEventExit is a ContainerEvent of type exit. ExitCode is set
  248. ContainerEventExit
  249. // UserCancel user cancelled compose up, we are stopping containers
  250. UserCancel
  251. )