1
0

api.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // CreateOneOffContainer creates a service oneoff container and starts its dependencies
  43. CreateOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
  44. // Run attaches to and starts a one-off container
  45. Run(ctx context.Context, container string, detach bool) error
  46. }
  47. // UpOptions group options of the Up API
  48. type UpOptions struct {
  49. // Detach will create services and return immediately
  50. Detach bool
  51. }
  52. // DownOptions group options of the Down API
  53. type DownOptions struct {
  54. // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
  55. RemoveOrphans bool
  56. }
  57. // ConvertOptions group options of the Convert API
  58. type ConvertOptions struct {
  59. // Format define the output format used to dump converted application model (json|yaml)
  60. Format string
  61. }
  62. // RunOptions holds all flags for compose run
  63. type RunOptions struct {
  64. Name string
  65. Command []string
  66. }
  67. // PortPublisher hold status about published port
  68. type PortPublisher struct {
  69. URL string
  70. TargetPort int
  71. PublishedPort int
  72. Protocol string
  73. }
  74. // ContainerSummary hold high-level description of a container
  75. type ContainerSummary struct {
  76. ID string
  77. Name string
  78. Project string
  79. Service string
  80. State string
  81. Publishers []PortPublisher
  82. }
  83. // ServiceStatus hold status about a service
  84. type ServiceStatus struct {
  85. ID string
  86. Name string
  87. Replicas int
  88. Desired int
  89. Ports []string
  90. Publishers []PortPublisher
  91. }
  92. // LogOptions defines optional parameters for the `Log` API
  93. type LogOptions struct {
  94. Services []string
  95. }
  96. const (
  97. // STARTING indicates that stack is being deployed
  98. STARTING string = "Starting"
  99. // RUNNING indicates that stack is deployed and services are running
  100. RUNNING string = "Running"
  101. // UPDATING indicates that some stack resources are being recreated
  102. UPDATING string = "Updating"
  103. // REMOVING indicates that stack is being deleted
  104. REMOVING string = "Removing"
  105. // UNKNOWN indicates unknown stack state
  106. UNKNOWN string = "Unknown"
  107. // FAILED indicates that stack deployment failed
  108. FAILED string = "Failed"
  109. )
  110. // Stack holds the name and state of a compose application/stack
  111. type Stack struct {
  112. ID string
  113. Name string
  114. Status string
  115. Reason string
  116. }
  117. // LogConsumer is a callback to process log messages from services
  118. type LogConsumer interface {
  119. Log(service, container, message string)
  120. }