compose.go 8.1 KB

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