api.go 9.9 KB

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