compose.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 aci
  14. import (
  15. "context"
  16. "fmt"
  17. "net/http"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/sirupsen/logrus"
  20. "github.com/docker/compose-cli/aci/convert"
  21. "github.com/docker/compose-cli/aci/login"
  22. "github.com/docker/compose-cli/api/compose"
  23. "github.com/docker/compose-cli/api/context/store"
  24. "github.com/docker/compose-cli/api/errdefs"
  25. "github.com/docker/compose-cli/utils/formatter"
  26. )
  27. type aciComposeService struct {
  28. ctx store.AciContext
  29. storageLogin login.StorageLoginImpl
  30. }
  31. func newComposeService(ctx store.AciContext) aciComposeService {
  32. return aciComposeService{
  33. ctx: ctx,
  34. storageLogin: login.StorageLoginImpl{AciContext: ctx},
  35. }
  36. }
  37. func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error {
  38. return errdefs.ErrNotImplemented
  39. }
  40. func (cs *aciComposeService) Push(ctx context.Context, project *types.Project) error {
  41. return errdefs.ErrNotImplemented
  42. }
  43. func (cs *aciComposeService) Pull(ctx context.Context, project *types.Project) error {
  44. return errdefs.ErrNotImplemented
  45. }
  46. func (cs *aciComposeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  47. return errdefs.ErrNotImplemented
  48. }
  49. func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
  50. return errdefs.ErrNotImplemented
  51. }
  52. func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  53. logrus.Debugf("Up on project with name %q", project.Name)
  54. if err := autocreateFileshares(ctx, project); err != nil {
  55. return err
  56. }
  57. groupDefinition, err := convert.ToContainerGroup(ctx, cs.ctx, *project, cs.storageLogin)
  58. if err != nil {
  59. return err
  60. }
  61. addTag(&groupDefinition, composeContainerTag)
  62. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  63. }
  64. func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectName string) error {
  65. cgClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  66. if err != nil {
  67. return err
  68. }
  69. cg, err := cgClient.Get(ctx, cs.ctx.ResourceGroup, projectName)
  70. if err != nil {
  71. return err
  72. }
  73. if cg.Volumes == nil {
  74. return nil
  75. }
  76. for _, v := range *cg.Volumes {
  77. if v.AzureFile == nil || v.AzureFile.StorageAccountName == nil || v.AzureFile.ShareName == nil {
  78. continue
  79. }
  80. fmt.Printf("WARNING: fileshare \"%s/%s\" will NOT be deleted. Use 'docker volume rm' if you want to delete this volume\n",
  81. *v.AzureFile.StorageAccountName, *v.AzureFile.ShareName)
  82. }
  83. return nil
  84. }
  85. func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  86. logrus.Debugf("Down on projectName with name %q", projectName)
  87. if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
  88. return err
  89. }
  90. cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName)
  91. if err != nil {
  92. return err
  93. }
  94. if cg.IsHTTPStatus(http.StatusNoContent) {
  95. return errdefs.ErrNotFound
  96. }
  97. return err
  98. }
  99. func (cs *aciComposeService) Ps(ctx context.Context, project string) ([]compose.ContainerSummary, error) {
  100. groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  101. if err != nil {
  102. return nil, err
  103. }
  104. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, project)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if group.Containers == nil || len(*group.Containers) == 0 {
  109. return nil, fmt.Errorf("no containers found in ACI container group %s", project)
  110. }
  111. res := []compose.ContainerSummary{}
  112. for _, container := range *group.Containers {
  113. if isContainerVisible(container, group, false) {
  114. continue
  115. }
  116. var publishers []compose.PortPublisher
  117. urls := formatter.PortsToStrings(convert.ToPorts(group.IPAddress, *container.Ports), convert.FQDN(group, cs.ctx.Location))
  118. for i, p := range *container.Ports {
  119. publishers = append(publishers, compose.PortPublisher{
  120. URL: urls[i],
  121. TargetPort: int(*p.Port),
  122. PublishedPort: int(*p.Port),
  123. Protocol: string(p.Protocol),
  124. })
  125. }
  126. id := getContainerID(group, container)
  127. res = append(res, compose.ContainerSummary{
  128. ID: id,
  129. Name: id,
  130. Project: project,
  131. Service: *container.Name,
  132. State: convert.GetStatus(container, group),
  133. Publishers: publishers,
  134. })
  135. }
  136. return res, nil
  137. }
  138. func (cs *aciComposeService) List(ctx context.Context) ([]compose.Stack, error) {
  139. containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
  140. if err != nil {
  141. return nil, err
  142. }
  143. var stacks []compose.Stack
  144. for _, group := range containerGroups {
  145. if _, found := group.Tags[composeContainerTag]; !found {
  146. continue
  147. }
  148. state := compose.RUNNING
  149. for _, container := range *group.ContainerGroupProperties.Containers {
  150. containerState := convert.GetStatus(container, group)
  151. if containerState != compose.RUNNING {
  152. state = containerState
  153. break
  154. }
  155. }
  156. stacks = append(stacks, compose.Stack{
  157. ID: *group.ID,
  158. Name: *group.Name,
  159. Status: state,
  160. })
  161. }
  162. return stacks, nil
  163. }
  164. func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  165. return errdefs.ErrNotImplemented
  166. }
  167. func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  168. return nil, errdefs.ErrNotImplemented
  169. }
  170. func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  171. return errdefs.ErrNotImplemented
  172. }