2020-06-18 17:13:24 +03:00
|
|
|
/*
|
2020-09-22 13:13:00 +03:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 17:13:24 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-29 16:12:45 +03:00
|
|
|
package aci
|
2020-04-29 20:57:53 +03:00
|
|
|
|
|
|
|
import (
|
2023-05-12 17:45:24 +03:00
|
|
|
"os"
|
2020-05-04 17:38:02 +03:00
|
|
|
"strings"
|
2020-04-29 20:57:53 +03:00
|
|
|
|
2023-05-18 18:32:17 +03:00
|
|
|
"github.com/docker/compose-cli/utils"
|
|
|
|
|
2020-11-24 00:15:15 +03:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
|
2020-06-30 13:23:22 +03:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2021-08-31 20:08:07 +03:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2020-04-29 20:57:53 +03:00
|
|
|
"github.com/pkg/errors"
|
2020-05-01 16:28:44 +03:00
|
|
|
|
2020-08-21 18:24:53 +03:00
|
|
|
"github.com/docker/compose-cli/aci/convert"
|
|
|
|
"github.com/docker/compose-cli/aci/login"
|
2021-01-15 17:44:42 +03:00
|
|
|
"github.com/docker/compose-cli/api/backend"
|
2021-08-31 20:08:07 +03:00
|
|
|
"github.com/docker/compose-cli/api/cloud"
|
2020-09-07 14:22:08 +03:00
|
|
|
"github.com/docker/compose-cli/api/containers"
|
2021-08-31 20:08:07 +03:00
|
|
|
apicontext "github.com/docker/compose-cli/api/context"
|
|
|
|
"github.com/docker/compose-cli/api/context/store"
|
2020-10-12 11:18:45 +03:00
|
|
|
"github.com/docker/compose-cli/api/resources"
|
2020-09-07 14:22:08 +03:00
|
|
|
"github.com/docker/compose-cli/api/secrets"
|
2020-09-07 19:23:28 +03:00
|
|
|
"github.com/docker/compose-cli/api/volumes"
|
2020-04-29 20:57:53 +03:00
|
|
|
)
|
|
|
|
|
2020-07-03 11:53:50 +03:00
|
|
|
const (
|
2020-07-29 17:05:13 +03:00
|
|
|
backendType = store.AciContextType
|
2020-07-10 15:27:55 +03:00
|
|
|
singleContainerTag = "docker-single-container"
|
|
|
|
composeContainerTag = "docker-compose-application"
|
2020-09-08 16:27:56 +03:00
|
|
|
dockerVolumeTag = "docker-volume"
|
2020-07-03 11:53:50 +03:00
|
|
|
composeContainerSeparator = "_"
|
|
|
|
)
|
2020-05-06 18:14:53 +03:00
|
|
|
|
2020-07-13 11:38:29 +03:00
|
|
|
// LoginParams azure login options
|
|
|
|
type LoginParams struct {
|
2020-08-10 17:38:59 +03:00
|
|
|
TenantID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
2021-01-31 22:15:00 +03:00
|
|
|
CloudName string
|
2020-08-10 17:38:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if options are not used properly
|
|
|
|
func (opts LoginParams) Validate() error {
|
|
|
|
if opts.ClientID != "" || opts.ClientSecret != "" {
|
|
|
|
if opts.ClientID == "" || opts.ClientSecret == "" || opts.TenantID == "" {
|
|
|
|
return errors.New("for Service Principal login, 3 options must be specified: --client-id, --client-secret and --tenant-id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-13 11:38:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:57:53 +03:00
|
|
|
func init() {
|
2020-07-29 16:12:45 +03:00
|
|
|
backend.Register(backendType, backendType, service, getCloudService)
|
2020-04-29 20:57:53 +03:00
|
|
|
}
|
|
|
|
|
2021-03-11 13:43:40 +03:00
|
|
|
func service() (backend.Service, error) {
|
2023-05-18 18:32:17 +03:00
|
|
|
utils.ShowDeprecationWarning(os.Stderr)
|
2021-03-11 13:43:40 +03:00
|
|
|
contextStore := store.Instance()
|
|
|
|
currentContext := apicontext.Current()
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 12:16:01 +03:00
|
|
|
var aciContext store.AciContext
|
2020-05-28 18:37:59 +03:00
|
|
|
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 12:16:01 +03:00
|
|
|
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
|
|
|
|
return nil, err
|
2020-04-29 20:57:53 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 18:37:59 +03:00
|
|
|
return getAciAPIService(aciContext), nil
|
2020-05-05 16:37:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 18:37:59 +03:00
|
|
|
func getCloudService() (cloud.Service, error) {
|
2020-05-14 00:33:16 +03:00
|
|
|
service, err := login.NewAzureLoginService()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 18:37:59 +03:00
|
|
|
return &aciCloudService{
|
|
|
|
loginService: service,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAciAPIService(aciCtx store.AciContext) *aciAPIService {
|
2020-09-22 12:45:02 +03:00
|
|
|
containerService := newContainerService(aciCtx)
|
|
|
|
composeService := newComposeService(aciCtx)
|
2020-05-05 17:27:22 +03:00
|
|
|
return &aciAPIService{
|
2020-09-22 12:45:02 +03:00
|
|
|
aciContainerService: &containerService,
|
|
|
|
aciComposeService: &composeService,
|
2020-09-07 19:23:28 +03:00
|
|
|
aciVolumeService: &aciVolumeService{
|
2020-09-08 11:11:02 +03:00
|
|
|
aciContext: aciCtx,
|
2020-09-07 19:23:28 +03:00
|
|
|
},
|
2020-10-14 17:06:45 +03:00
|
|
|
aciResourceService: &aciResourceService{
|
|
|
|
aciContext: aciCtx,
|
|
|
|
},
|
2020-05-28 18:37:59 +03:00
|
|
|
}
|
2020-05-05 16:37:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:27:22 +03:00
|
|
|
type aciAPIService struct {
|
2020-05-28 18:37:59 +03:00
|
|
|
*aciContainerService
|
|
|
|
*aciComposeService
|
2020-09-07 15:39:19 +03:00
|
|
|
*aciVolumeService
|
2020-10-12 11:18:45 +03:00
|
|
|
*aciResourceService
|
2020-04-29 20:57:53 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:27:22 +03:00
|
|
|
func (a *aciAPIService) ContainerService() containers.Service {
|
2020-05-28 18:37:59 +03:00
|
|
|
return a.aciContainerService
|
2020-05-05 16:37:12 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 17:26:14 +03:00
|
|
|
func (a *aciAPIService) ComposeService() api.Service {
|
2020-05-28 18:37:59 +03:00
|
|
|
return a.aciComposeService
|
2020-05-12 14:37:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:13:58 +03:00
|
|
|
func (a *aciAPIService) SecretsService() secrets.Service {
|
2020-10-12 12:03:43 +03:00
|
|
|
// Not implemented on ACI
|
|
|
|
// Secrets are created and mounted in the container at it's creation and not stored on ACI
|
2020-08-18 10:13:58 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:39:19 +03:00
|
|
|
func (a *aciAPIService) VolumeService() volumes.Service {
|
|
|
|
return a.aciVolumeService
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:18:45 +03:00
|
|
|
func (a *aciAPIService) ResourceService() resources.Service {
|
|
|
|
return a.aciResourceService
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:38:51 +03:00
|
|
|
func getContainerID(group containerinstance.ContainerGroup, container containerinstance.Container) string {
|
|
|
|
containerID := *group.Name + composeContainerSeparator + *container.Name
|
|
|
|
if _, ok := group.Tags[singleContainerTag]; ok {
|
|
|
|
containerID = *group.Name
|
|
|
|
}
|
|
|
|
return containerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func isContainerVisible(container containerinstance.Container, group containerinstance.ContainerGroup, showAll bool) bool {
|
2020-09-04 16:09:57 +03:00
|
|
|
return *container.Name == convert.ComposeDNSSidecarName || (!showAll && convert.GetStatus(container, group) != convert.StatusRunning)
|
2020-08-28 15:38:51 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 11:01:49 +03:00
|
|
|
func addTag(groupDefinition *containerinstance.ContainerGroup, tagName string) {
|
2020-07-09 00:31:01 +03:00
|
|
|
if groupDefinition.Tags == nil {
|
|
|
|
groupDefinition.Tags = make(map[string]*string, 1)
|
|
|
|
}
|
2020-07-16 11:01:49 +03:00
|
|
|
groupDefinition.Tags[tagName] = to.StringPtr(tagName)
|
2020-05-01 16:28:44 +03:00
|
|
|
}
|
2020-05-03 14:35:25 +03:00
|
|
|
|
2020-07-09 00:31:01 +03:00
|
|
|
func getGroupAndContainerName(containerID string) (string, string) {
|
2020-07-03 11:53:50 +03:00
|
|
|
tokens := strings.Split(containerID, composeContainerSeparator)
|
2020-07-09 00:31:01 +03:00
|
|
|
groupName := tokens[0]
|
|
|
|
containerName := groupName
|
2020-05-06 18:14:53 +03:00
|
|
|
if len(tokens) > 1 {
|
|
|
|
containerName = tokens[len(tokens)-1]
|
|
|
|
groupName = containerID[:len(containerID)-(len(containerName)+1)]
|
|
|
|
}
|
|
|
|
return groupName, containerName
|
|
|
|
}
|