2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02: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 15:12:45 +02:00
|
|
|
package aci
|
2020-04-29 19:57:53 +02:00
|
|
|
|
|
|
|
import (
|
2020-05-04 16:38:02 +02:00
|
|
|
"strings"
|
2020-04-29 19:57:53 +02:00
|
|
|
|
2020-11-23 22:15:15 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
|
2020-06-30 12:23:22 +02:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-05-01 15:28:44 +02:00
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/aci/convert"
|
|
|
|
"github.com/docker/compose-cli/aci/login"
|
2021-01-15 15:44:42 +01:00
|
|
|
"github.com/docker/compose-cli/api/backend"
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/containers"
|
2020-10-12 10:18:45 +02:00
|
|
|
"github.com/docker/compose-cli/api/resources"
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/secrets"
|
2020-09-07 18:23:28 +02:00
|
|
|
"github.com/docker/compose-cli/api/volumes"
|
2021-06-14 16:26:14 +02:00
|
|
|
"github.com/docker/compose-cli/pkg/api"
|
2021-01-15 15:44:42 +01:00
|
|
|
|
2021-01-15 16:22:20 +01:00
|
|
|
"github.com/docker/compose-cli/api/cloud"
|
2021-01-15 16:31:59 +01:00
|
|
|
apicontext "github.com/docker/compose-cli/api/context"
|
|
|
|
"github.com/docker/compose-cli/api/context/store"
|
2020-04-29 19:57:53 +02:00
|
|
|
)
|
|
|
|
|
2020-07-03 10:53:50 +02:00
|
|
|
const (
|
2020-07-29 16:05:13 +02:00
|
|
|
backendType = store.AciContextType
|
2020-07-10 14:27:55 +02:00
|
|
|
singleContainerTag = "docker-single-container"
|
|
|
|
composeContainerTag = "docker-compose-application"
|
2020-09-08 15:27:56 +02:00
|
|
|
dockerVolumeTag = "docker-volume"
|
2020-07-03 10:53:50 +02:00
|
|
|
composeContainerSeparator = "_"
|
|
|
|
)
|
2020-05-06 17:14:53 +02:00
|
|
|
|
2020-07-13 10:38:29 +02:00
|
|
|
// LoginParams azure login options
|
|
|
|
type LoginParams struct {
|
2020-08-10 16:38:59 +02:00
|
|
|
TenantID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
2021-01-31 20:15:00 +01:00
|
|
|
CloudName string
|
2020-08-10 16:38:59 +02: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 10:38:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
func init() {
|
2020-07-29 15:12:45 +02:00
|
|
|
backend.Register(backendType, backendType, service, getCloudService)
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:43:40 +01:00
|
|
|
func service() (backend.Service, error) {
|
|
|
|
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 11:16:01 +02:00
|
|
|
var aciContext store.AciContext
|
2020-05-28 17:37:59 +02: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 11:16:01 +02:00
|
|
|
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
|
|
|
|
return nil, err
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:37:59 +02:00
|
|
|
return getAciAPIService(aciContext), nil
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:37:59 +02:00
|
|
|
func getCloudService() (cloud.Service, error) {
|
2020-05-13 23:33:16 +02:00
|
|
|
service, err := login.NewAzureLoginService()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 17:37:59 +02:00
|
|
|
return &aciCloudService{
|
|
|
|
loginService: service,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAciAPIService(aciCtx store.AciContext) *aciAPIService {
|
2020-09-22 11:45:02 +02:00
|
|
|
containerService := newContainerService(aciCtx)
|
|
|
|
composeService := newComposeService(aciCtx)
|
2020-05-05 16:27:22 +02:00
|
|
|
return &aciAPIService{
|
2020-09-22 11:45:02 +02:00
|
|
|
aciContainerService: &containerService,
|
|
|
|
aciComposeService: &composeService,
|
2020-09-07 18:23:28 +02:00
|
|
|
aciVolumeService: &aciVolumeService{
|
2020-09-08 10:11:02 +02:00
|
|
|
aciContext: aciCtx,
|
2020-09-07 18:23:28 +02:00
|
|
|
},
|
2020-10-14 16:06:45 +02:00
|
|
|
aciResourceService: &aciResourceService{
|
|
|
|
aciContext: aciCtx,
|
|
|
|
},
|
2020-05-28 17:37:59 +02:00
|
|
|
}
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
type aciAPIService struct {
|
2020-05-28 17:37:59 +02:00
|
|
|
*aciContainerService
|
|
|
|
*aciComposeService
|
2020-09-07 14:39:19 +02:00
|
|
|
*aciVolumeService
|
2020-10-12 10:18:45 +02:00
|
|
|
*aciResourceService
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
func (a *aciAPIService) ContainerService() containers.Service {
|
2020-05-28 17:37:59 +02:00
|
|
|
return a.aciContainerService
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (a *aciAPIService) ComposeService() api.Service {
|
2020-05-28 17:37:59 +02:00
|
|
|
return a.aciComposeService
|
2020-05-12 13:37:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
func (a *aciAPIService) SecretsService() secrets.Service {
|
2020-10-12 11:03:43 +02: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 09:13:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-07 14:39:19 +02:00
|
|
|
func (a *aciAPIService) VolumeService() volumes.Service {
|
|
|
|
return a.aciVolumeService
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:18:45 +02:00
|
|
|
func (a *aciAPIService) ResourceService() resources.Service {
|
|
|
|
return a.aciResourceService
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:38:51 +02: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 15:09:57 +02:00
|
|
|
return *container.Name == convert.ComposeDNSSidecarName || (!showAll && convert.GetStatus(container, group) != convert.StatusRunning)
|
2020-08-28 14:38:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:01:49 +02:00
|
|
|
func addTag(groupDefinition *containerinstance.ContainerGroup, tagName string) {
|
2020-07-08 23:31:01 +02:00
|
|
|
if groupDefinition.Tags == nil {
|
|
|
|
groupDefinition.Tags = make(map[string]*string, 1)
|
|
|
|
}
|
2020-07-16 10:01:49 +02:00
|
|
|
groupDefinition.Tags[tagName] = to.StringPtr(tagName)
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-07-08 23:31:01 +02:00
|
|
|
func getGroupAndContainerName(containerID string) (string, string) {
|
2020-07-03 10:53:50 +02:00
|
|
|
tokens := strings.Split(containerID, composeContainerSeparator)
|
2020-07-08 23:31:01 +02:00
|
|
|
groupName := tokens[0]
|
|
|
|
containerName := groupName
|
2020-05-06 17:14:53 +02:00
|
|
|
if len(tokens) > 1 {
|
|
|
|
containerName = tokens[len(tokens)-1]
|
|
|
|
groupName = containerID[:len(containerID)-(len(containerName)+1)]
|
|
|
|
}
|
|
|
|
return groupName, containerName
|
|
|
|
}
|