compose.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. }
  30. func (cs *aciComposeService) Up(ctx context.Context, project *types.Project) error {
  31. logrus.Debugf("Up on project with name %q", project.Name)
  32. groupDefinition, err := convert.ToContainerGroup(ctx, cs.ctx, *project)
  33. addTag(&groupDefinition, composeContainerTag)
  34. if err != nil {
  35. return err
  36. }
  37. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  38. }
  39. func (cs *aciComposeService) Down(ctx context.Context, project string) error {
  40. logrus.Debugf("Down on project with name %q", project)
  41. cg, err := deleteACIContainerGroup(ctx, cs.ctx, project)
  42. if err != nil {
  43. return err
  44. }
  45. if cg.StatusCode == http.StatusNoContent {
  46. return errdefs.ErrNotFound
  47. }
  48. return err
  49. }
  50. func (cs *aciComposeService) Ps(ctx context.Context, project string) ([]compose.ServiceStatus, error) {
  51. groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  52. if err != nil {
  53. return nil, err
  54. }
  55. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, project)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if group.Containers == nil || len(*group.Containers) == 0 {
  60. return nil, fmt.Errorf("no containers found in ACI container group %s", project)
  61. }
  62. res := []compose.ServiceStatus{}
  63. for _, container := range *group.Containers {
  64. if isContainerVisible(container, group, false) {
  65. continue
  66. }
  67. res = append(res, convert.ContainerGroupToServiceStatus(getContainerID(group, container), group, container))
  68. }
  69. return res, nil
  70. }
  71. func (cs *aciComposeService) List(ctx context.Context, project string) ([]compose.Stack, error) {
  72. containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
  73. if err != nil {
  74. return nil, err
  75. }
  76. stacks := []compose.Stack{}
  77. for _, group := range containerGroups {
  78. if _, found := group.Tags[composeContainerTag]; !found {
  79. continue
  80. }
  81. if project != "" && *group.Name != project {
  82. continue
  83. }
  84. state := compose.RUNNING
  85. for _, container := range *group.ContainerGroupProperties.Containers {
  86. containerState := convert.GetStatus(container, group)
  87. if containerState != compose.RUNNING {
  88. state = containerState
  89. break
  90. }
  91. }
  92. stacks = append(stacks, compose.Stack{
  93. ID: *group.ID,
  94. Name: *group.Name,
  95. Status: state,
  96. })
  97. }
  98. return stacks, nil
  99. }
  100. func (cs *aciComposeService) Logs(ctx context.Context, project string, w io.Writer) error {
  101. return errdefs.ErrNotImplemented
  102. }
  103. func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project) ([]byte, error) {
  104. return nil, errdefs.ErrNotImplemented
  105. }