api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "github.com/compose-spec/compose-go/types"
  17. )
  18. // Service manages a compose project
  19. type Service interface {
  20. // Build executes the equivalent to a `compose build`
  21. Build(ctx context.Context, project *types.Project) error
  22. // Push executes the equivalent ot a `compose push`
  23. Push(ctx context.Context, project *types.Project) error
  24. // Pull executes the equivalent of a `compose pull`
  25. Pull(ctx context.Context, project *types.Project) error
  26. // Create executes the equivalent to a `compose create`
  27. Create(ctx context.Context, project *types.Project) error
  28. // Start executes the equivalent to a `compose start`
  29. Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
  30. // Up executes the equivalent to a `compose up`
  31. Up(ctx context.Context, project *types.Project, options UpOptions) error
  32. // Down executes the equivalent to a `compose down`
  33. Down(ctx context.Context, projectName string, options DownOptions) error
  34. // Logs executes the equivalent to a `compose logs`
  35. Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
  36. // Ps executes the equivalent to a `compose ps`
  37. Ps(ctx context.Context, projectName string) ([]ContainerSummary, error)
  38. // List executes the equivalent to a `docker stack ls`
  39. List(ctx context.Context, projectName string) ([]Stack, error)
  40. // Convert translate compose model into backend's native format
  41. Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
  42. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  43. RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
  44. }
  45. // UpOptions group options of the Up API
  46. type UpOptions struct {
  47. // Detach will create services and return immediately
  48. Detach bool
  49. }
  50. // DownOptions group options of the Down API
  51. type DownOptions struct {
  52. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  53. RemoveOrphans bool
  54. }
  55. // ConvertOptions group options of the Convert API
  56. type ConvertOptions struct {
  57. // Format define the output format used to dump converted application model (json|yaml)
  58. Format string
  59. }
  60. // RunOptions holds all flags for compose run
  61. type RunOptions struct {
  62. Name string
  63. Command []string
  64. Detach bool
  65. }
  66. // PortPublisher hold status about published port
  67. type PortPublisher struct {
  68. URL string
  69. TargetPort int
  70. PublishedPort int
  71. Protocol string
  72. }
  73. // ContainerSummary hold high-level description of a container
  74. type ContainerSummary struct {
  75. ID string
  76. Name string
  77. Project string
  78. Service string
  79. State string
  80. Publishers []PortPublisher
  81. }
  82. // ServiceStatus hold status about a service
  83. type ServiceStatus struct {
  84. ID string
  85. Name string
  86. Replicas int
  87. Desired int
  88. Ports []string
  89. Publishers []PortPublisher
  90. }
  91. // LogOptions defines optional parameters for the `Log` API
  92. type LogOptions struct {
  93. Services []string
  94. }
  95. const (
  96. // STARTING indicates that stack is being deployed
  97. STARTING string = "Starting"
  98. // RUNNING indicates that stack is deployed and services are running
  99. RUNNING string = "Running"
  100. // UPDATING indicates that some stack resources are being recreated
  101. UPDATING string = "Updating"
  102. // REMOVING indicates that stack is being deleted
  103. REMOVING string = "Removing"
  104. // UNKNOWN indicates unknown stack state
  105. UNKNOWN string = "Unknown"
  106. // FAILED indicates that stack deployment failed
  107. FAILED string = "Failed"
  108. )
  109. // Stack holds the name and state of a compose application/stack
  110. type Stack struct {
  111. ID string
  112. Name string
  113. Status string
  114. Reason string
  115. }
  116. // LogConsumer is a callback to process log messages from services
  117. type LogConsumer interface {
  118. Log(service, container, message string)
  119. }