backend.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "strings"
  17. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
  18. "github.com/Azure/go-autorest/autorest/to"
  19. "github.com/pkg/errors"
  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/containers"
  24. "github.com/docker/compose-cli/api/secrets"
  25. "github.com/docker/compose-cli/api/volumes"
  26. "github.com/docker/compose-cli/backend"
  27. apicontext "github.com/docker/compose-cli/context"
  28. "github.com/docker/compose-cli/context/cloud"
  29. "github.com/docker/compose-cli/context/store"
  30. )
  31. const (
  32. backendType = store.AciContextType
  33. singleContainerTag = "docker-single-container"
  34. composeContainerTag = "docker-compose-application"
  35. dockerVolumeTag = "docker-volume"
  36. composeContainerSeparator = "_"
  37. )
  38. // LoginParams azure login options
  39. type LoginParams struct {
  40. TenantID string
  41. ClientID string
  42. ClientSecret string
  43. }
  44. // Validate returns an error if options are not used properly
  45. func (opts LoginParams) Validate() error {
  46. if opts.ClientID != "" || opts.ClientSecret != "" {
  47. if opts.ClientID == "" || opts.ClientSecret == "" || opts.TenantID == "" {
  48. return errors.New("for Service Principal login, 3 options must be specified: --client-id, --client-secret and --tenant-id")
  49. }
  50. }
  51. return nil
  52. }
  53. func init() {
  54. backend.Register(backendType, backendType, service, getCloudService)
  55. }
  56. func service(ctx context.Context) (backend.Service, error) {
  57. contextStore := store.ContextStore(ctx)
  58. currentContext := apicontext.CurrentContext(ctx)
  59. var aciContext store.AciContext
  60. if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
  61. return nil, err
  62. }
  63. return getAciAPIService(aciContext), nil
  64. }
  65. func getCloudService() (cloud.Service, error) {
  66. service, err := login.NewAzureLoginService()
  67. if err != nil {
  68. return nil, err
  69. }
  70. return &aciCloudService{
  71. loginService: service,
  72. }, nil
  73. }
  74. func getAciAPIService(aciCtx store.AciContext) *aciAPIService {
  75. containerService := newContainerService(aciCtx)
  76. composeService := newComposeService(aciCtx)
  77. return &aciAPIService{
  78. aciContainerService: &containerService,
  79. aciComposeService: &composeService,
  80. aciVolumeService: &aciVolumeService{
  81. aciContext: aciCtx,
  82. },
  83. }
  84. }
  85. type aciAPIService struct {
  86. *aciContainerService
  87. *aciComposeService
  88. *aciVolumeService
  89. }
  90. func (a *aciAPIService) ContainerService() containers.Service {
  91. return a.aciContainerService
  92. }
  93. func (a *aciAPIService) ComposeService() compose.Service {
  94. return a.aciComposeService
  95. }
  96. func (a *aciAPIService) SecretsService() secrets.Service {
  97. return nil
  98. }
  99. func (a *aciAPIService) VolumeService() volumes.Service {
  100. return a.aciVolumeService
  101. }
  102. func getContainerID(group containerinstance.ContainerGroup, container containerinstance.Container) string {
  103. containerID := *group.Name + composeContainerSeparator + *container.Name
  104. if _, ok := group.Tags[singleContainerTag]; ok {
  105. containerID = *group.Name
  106. }
  107. return containerID
  108. }
  109. func isContainerVisible(container containerinstance.Container, group containerinstance.ContainerGroup, showAll bool) bool {
  110. return *container.Name == convert.ComposeDNSSidecarName || (!showAll && convert.GetStatus(container, group) != convert.StatusRunning)
  111. }
  112. func addTag(groupDefinition *containerinstance.ContainerGroup, tagName string) {
  113. if groupDefinition.Tags == nil {
  114. groupDefinition.Tags = make(map[string]*string, 1)
  115. }
  116. groupDefinition.Tags[tagName] = to.StringPtr(tagName)
  117. }
  118. func getGroupAndContainerName(containerID string) (string, string) {
  119. tokens := strings.Split(containerID, composeContainerSeparator)
  120. groupName := tokens[0]
  121. containerName := groupName
  122. if len(tokens) > 1 {
  123. containerName = tokens[len(tokens)-1]
  124. groupName = containerID[:len(containerID)-(len(containerName)+1)]
  125. }
  126. return groupName, containerName
  127. }