compose.go 4.7 KB

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