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