1
0

api.go 8.5 KB

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