compose.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/context/store"
  24. "github.com/docker/compose-cli/errdefs"
  25. )
  26. type aciComposeService struct {
  27. ctx store.AciContext
  28. storageLogin login.StorageLoginImpl
  29. }
  30. func newComposeService(ctx store.AciContext) aciComposeService {
  31. return aciComposeService{
  32. ctx: ctx,
  33. storageLogin: login.StorageLoginImpl{AciContext: ctx},
  34. }
  35. }
  36. func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error {
  37. return errdefs.ErrNotImplemented
  38. }
  39. func (cs *aciComposeService) Push(ctx context.Context, project *types.Project) error {
  40. return errdefs.ErrNotImplemented
  41. }
  42. func (cs *aciComposeService) Pull(ctx context.Context, project *types.Project) error {
  43. return errdefs.ErrNotImplemented
  44. }
  45. func (cs *aciComposeService) Create(ctx context.Context, project *types.Project) error {
  46. return errdefs.ErrNotImplemented
  47. }
  48. func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
  49. return errdefs.ErrNotImplemented
  50. }
  51. func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, detach bool) error {
  52. logrus.Debugf("Up on project with name %q", project.Name)
  53. if err := autocreateFileshares(ctx, project); err != nil {
  54. return err
  55. }
  56. groupDefinition, err := convert.ToContainerGroup(ctx, cs.ctx, *project, cs.storageLogin)
  57. if err != nil {
  58. return err
  59. }
  60. addTag(&groupDefinition, composeContainerTag)
  61. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  62. }
  63. func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectName string) error {
  64. cgClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  65. if err != nil {
  66. return err
  67. }
  68. cg, err := cgClient.Get(ctx, cs.ctx.ResourceGroup, projectName)
  69. if err != nil {
  70. return err
  71. }
  72. if cg.Volumes == nil {
  73. return nil
  74. }
  75. for _, v := range *cg.Volumes {
  76. if v.AzureFile == nil || v.AzureFile.StorageAccountName == nil || v.AzureFile.ShareName == nil {
  77. continue
  78. }
  79. fmt.Printf("WARNING: fileshare \"%s/%s\" will NOT be deleted. Use 'docker volume rm' if you want to delete this volume\n",
  80. *v.AzureFile.StorageAccountName, *v.AzureFile.ShareName)
  81. }
  82. return nil
  83. }
  84. func (cs *aciComposeService) Down(ctx context.Context, project string) error {
  85. logrus.Debugf("Down on project with name %q", project)
  86. if err := cs.warnKeepVolumeOnDown(ctx, project); err != nil {
  87. return err
  88. }
  89. cg, err := deleteACIContainerGroup(ctx, cs.ctx, project)
  90. if err != nil {
  91. return err
  92. }
  93. if cg.IsHTTPStatus(http.StatusNoContent) {
  94. return errdefs.ErrNotFound
  95. }
  96. return err
  97. }
  98. func (cs *aciComposeService) Ps(ctx context.Context, project string) ([]compose.ServiceStatus, error) {
  99. groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  100. if err != nil {
  101. return nil, err
  102. }
  103. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, project)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if group.Containers == nil || len(*group.Containers) == 0 {
  108. return nil, fmt.Errorf("no containers found in ACI container group %s", project)
  109. }
  110. res := []compose.ServiceStatus{}
  111. for _, container := range *group.Containers {
  112. if isContainerVisible(container, group, false) {
  113. continue
  114. }
  115. res = append(res, convert.ContainerGroupToServiceStatus(getContainerID(group, container), group, container, cs.ctx.Location))
  116. }
  117. return res, nil
  118. }
  119. func (cs *aciComposeService) List(ctx context.Context, project string) ([]compose.Stack, error) {
  120. containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
  121. if err != nil {
  122. return nil, err
  123. }
  124. stacks := []compose.Stack{}
  125. for _, group := range containerGroups {
  126. if _, found := group.Tags[composeContainerTag]; !found {
  127. continue
  128. }
  129. if project != "" && *group.Name != project {
  130. continue
  131. }
  132. state := compose.RUNNING
  133. for _, container := range *group.ContainerGroupProperties.Containers {
  134. containerState := convert.GetStatus(container, group)
  135. if containerState != compose.RUNNING {
  136. state = containerState
  137. break
  138. }
  139. }
  140. stacks = append(stacks, compose.Stack{
  141. ID: *group.ID,
  142. Name: *group.Name,
  143. Status: state,
  144. })
  145. }
  146. return stacks, nil
  147. }
  148. func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer) error {
  149. return errdefs.ErrNotImplemented
  150. }
  151. func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) {
  152. return nil, errdefs.ErrNotImplemented
  153. }