api.go 9.0 KB

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