api.go 9.5 KB

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