compose/azure/backend.go

140 lines
3.6 KiB
Go
Raw Normal View History

package azure
import (
"context"
2020-05-03 13:35:25 +02:00
"io"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/go-autorest/autorest/azure/auth"
2020-05-01 15:28:44 +02:00
"github.com/compose-spec/compose-go/types"
"github.com/pkg/errors"
2020-05-01 15:28:44 +02:00
"github.com/sirupsen/logrus"
"github.com/docker/api/azure/convert"
"github.com/docker/api/backend"
2020-05-01 15:28:44 +02:00
"github.com/docker/api/compose"
"github.com/docker/api/containers"
apicontext "github.com/docker/api/context"
"github.com/docker/api/context/store"
)
type containerService struct {
2020-05-04 09:57:52 +02:00
containerGroupsClient containerinstance.ContainerGroupsClient
ctx store.AciContext
}
func init() {
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
return New(ctx)
})
}
func getter() interface{} {
return &store.AciContext{}
}
func New(ctx context.Context) (containers.ContainerService, error) {
2020-05-04 09:57:52 +02:00
currentContext := apicontext.CurrentContext(ctx)
2020-04-29 23:44:01 +02:00
contextStore, err := store.New()
if err != nil {
return nil, err
}
2020-05-04 09:57:52 +02:00
metadata, err := contextStore.Get(currentContext, getter)
if err != nil {
return nil, errors.Wrap(err, "wrong context type")
}
2020-05-04 09:57:52 +02:00
aciContext, _ := metadata.Metadata.Data.(store.AciContext)
auth, _ := auth.NewAuthorizerFromCLI()
2020-05-04 09:57:52 +02:00
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
containerGroupsClient.Authorizer = auth
return &containerService{
2020-05-04 09:57:52 +02:00
containerGroupsClient: containerGroupsClient,
ctx: aciContext,
}, nil
}
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
2020-05-04 09:57:52 +02:00
var containerGroups []containerinstance.ContainerGroup
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
if err != nil {
return []containers.Container{}, err
}
for result.NotDone() {
2020-05-04 09:57:52 +02:00
containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil {
return []containers.Container{}, err
}
}
res := []containers.Container{}
2020-05-04 09:57:52 +02:00
for _, containerGroup := range containerGroups {
group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
2020-05-03 14:51:03 +02:00
if err != nil {
return []containers.Container{}, err
}
2020-05-04 09:57:52 +02:00
for _, container := range *group.Containers {
2020-05-03 14:51:03 +02:00
status := "Unknown"
2020-05-04 09:57:52 +02:00
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State
2020-05-03 14:51:03 +02:00
}
res = append(res, containers.Container{
2020-05-04 09:57:52 +02:00
ID: *container.Name,
Image: *container.Image,
2020-05-03 14:51:03 +02:00
Status: status,
})
}
}
return res, nil
}
2020-05-01 15:28:44 +02:00
func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
2020-05-01 16:03:33 +02:00
var ports []types.ServicePortConfig
for _, p := range r.Ports {
ports = append(ports, types.ServicePortConfig{
Target: p.Destination,
Published: p.Source,
})
}
project := compose.Project{
Name: r.ID,
Config: types.Config{
Services: []types.ServiceConfig{
{
Name: r.ID,
Image: r.Image,
Ports: ports,
},
},
2020-05-01 15:28:44 +02:00
},
}
logrus.Debugf("Running container %q with name %q\n", r.Image, r.ID)
groupDefinition, err := convert.ToContainerGroup(cs.ctx, project)
if err != nil {
return err
}
_, err = createACIContainers(ctx, cs.ctx, groupDefinition)
2020-05-01 15:28:44 +02:00
return err
}
2020-05-03 13:35:25 +02:00
func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, name, name)
if err != nil {
return err
}
return exec(
context.Background(),
*containerExecResponse.WebSocketURI,
*containerExecResponse.Password,
reader,
writer,
)
}