api.go 9.1 KB

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