api.go 4.7 KB

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