api.go 8.7 KB

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