compose.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, options compose.BuildOptions) error {
  38. return errdefs.ErrNotImplemented
  39. }
  40. func (cs *aciComposeService) Push(ctx context.Context, project *types.Project, options compose.PushOptions) 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, options compose.StartOptions) error {
  50. return errdefs.ErrNotImplemented
  51. }
  52. func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
  53. return errdefs.ErrNotImplemented
  54. }
  55. func (cs *aciComposeService) Pause(ctx context.Context, project *types.Project) error {
  56. return errdefs.ErrNotImplemented
  57. }
  58. func (cs *aciComposeService) UnPause(ctx context.Context, project *types.Project) error {
  59. return errdefs.ErrNotImplemented
  60. }
  61. func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  62. logrus.Debugf("Up on project with name %q", project.Name)
  63. if err := autocreateFileshares(ctx, project); err != nil {
  64. return err
  65. }
  66. groupDefinition, err := convert.ToContainerGroup(ctx, cs.ctx, *project, cs.storageLogin)
  67. if err != nil {
  68. return err
  69. }
  70. addTag(&groupDefinition, composeContainerTag)
  71. return createOrUpdateACIContainers(ctx, cs.ctx, groupDefinition)
  72. }
  73. func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectName string) error {
  74. cgClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  75. if err != nil {
  76. return err
  77. }
  78. cg, err := cgClient.Get(ctx, cs.ctx.ResourceGroup, projectName)
  79. if err != nil {
  80. return err
  81. }
  82. if cg.Volumes == nil {
  83. return nil
  84. }
  85. for _, v := range *cg.Volumes {
  86. if v.AzureFile == nil || v.AzureFile.StorageAccountName == nil || v.AzureFile.ShareName == nil {
  87. continue
  88. }
  89. fmt.Printf("WARNING: fileshare \"%s/%s\" will NOT be deleted. Use 'docker volume rm' if you want to delete this volume\n",
  90. *v.AzureFile.StorageAccountName, *v.AzureFile.ShareName)
  91. }
  92. return nil
  93. }
  94. func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  95. logrus.Debugf("Down on projectName with name %q", projectName)
  96. if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
  97. return err
  98. }
  99. cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName)
  100. if err != nil {
  101. return err
  102. }
  103. if cg.IsHTTPStatus(http.StatusNoContent) {
  104. return errdefs.ErrNotFound
  105. }
  106. return err
  107. }
  108. func (cs *aciComposeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  109. groupsClient, err := login.NewContainerGroupsClient(cs.ctx.SubscriptionID)
  110. if err != nil {
  111. return nil, err
  112. }
  113. group, err := groupsClient.Get(ctx, cs.ctx.ResourceGroup, projectName)
  114. if err != nil {
  115. return nil, err
  116. }
  117. if group.Containers == nil || len(*group.Containers) == 0 {
  118. return nil, fmt.Errorf("no containers found in ACI container group %s", projectName)
  119. }
  120. res := []compose.ContainerSummary{}
  121. for _, container := range *group.Containers {
  122. if isContainerVisible(container, group, false) {
  123. continue
  124. }
  125. var publishers []compose.PortPublisher
  126. urls := formatter.PortsToStrings(convert.ToPorts(group.IPAddress, *container.Ports), convert.FQDN(group, cs.ctx.Location))
  127. for i, p := range *container.Ports {
  128. publishers = append(publishers, compose.PortPublisher{
  129. URL: urls[i],
  130. TargetPort: int(*p.Port),
  131. PublishedPort: int(*p.Port),
  132. Protocol: string(p.Protocol),
  133. })
  134. }
  135. id := getContainerID(group, container)
  136. res = append(res, compose.ContainerSummary{
  137. ID: id,
  138. Name: id,
  139. Project: projectName,
  140. Service: *container.Name,
  141. State: convert.GetStatus(container, group),
  142. Publishers: publishers,
  143. })
  144. }
  145. return res, nil
  146. }
  147. func (cs *aciComposeService) List(ctx context.Context, opts compose.ListOptions) ([]compose.Stack, error) {
  148. containerGroups, err := getACIContainerGroups(ctx, cs.ctx.SubscriptionID, cs.ctx.ResourceGroup)
  149. if err != nil {
  150. return nil, err
  151. }
  152. var stacks []compose.Stack
  153. for _, group := range containerGroups {
  154. if _, found := group.Tags[composeContainerTag]; !found {
  155. continue
  156. }
  157. state := compose.RUNNING
  158. for _, container := range *group.ContainerGroupProperties.Containers {
  159. containerState := convert.GetStatus(container, group)
  160. if containerState != compose.RUNNING {
  161. state = containerState
  162. break
  163. }
  164. }
  165. stacks = append(stacks, compose.Stack{
  166. ID: *group.ID,
  167. Name: *group.Name,
  168. Status: state,
  169. })
  170. }
  171. return stacks, nil
  172. }
  173. func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  174. return errdefs.ErrNotImplemented
  175. }
  176. func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  177. return nil, errdefs.ErrNotImplemented
  178. }
  179. func (cs *aciComposeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  180. return errdefs.ErrNotImplemented
  181. }
  182. func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  183. return 0, errdefs.ErrNotImplemented
  184. }
  185. func (cs *aciComposeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
  186. return nil, errdefs.ErrNotImplemented
  187. }
  188. func (cs *aciComposeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  189. return errdefs.ErrNotImplemented
  190. }