2020-04-29 19:57:53 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-03 13:41:45 +02:00
|
|
|
"fmt"
|
2020-05-03 13:35:25 +02:00
|
|
|
"io"
|
2020-05-10 22:37:28 +02:00
|
|
|
"net/http"
|
2020-05-04 16:38:02 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-04-29 19:57:53 +02:00
|
|
|
|
|
|
|
"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"
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-05-01 15:28:44 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-05-01 15:48:20 +02:00
|
|
|
"github.com/docker/api/azure/convert"
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/backend"
|
2020-05-01 15:28:44 +02:00
|
|
|
"github.com/docker/api/compose"
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/containers"
|
|
|
|
apicontext "github.com/docker/api/context"
|
2020-05-01 15:48:20 +02:00
|
|
|
"github.com/docker/api/context/store"
|
2020-04-29 19:57:53 +02:00
|
|
|
)
|
|
|
|
|
2020-05-06 17:14:53 +02:00
|
|
|
const singleContainerName = "single--container--aci"
|
|
|
|
|
2020-05-10 22:37:28 +02:00
|
|
|
// ErrNoSuchContainer is returned when the mentioned container does not exist
|
|
|
|
var ErrNoSuchContainer = errors.New("no such container")
|
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
func init() {
|
2020-05-05 17:14:26 +02:00
|
|
|
backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) {
|
2020-04-29 19:57:53 +02:00
|
|
|
return New(ctx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getter() interface{} {
|
|
|
|
return &store.AciContext{}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
// New creates a backend that can manage containers
|
|
|
|
func New(ctx context.Context) (backend.Service, 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()
|
2020-04-29 19:57:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 09:57:52 +02:00
|
|
|
metadata, err := contextStore.Get(currentContext, getter)
|
2020-04-29 19:57:53 +02:00
|
|
|
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)
|
2020-04-29 19:57:53 +02:00
|
|
|
|
|
|
|
auth, _ := auth.NewAuthorizerFromCLI()
|
2020-05-04 09:57:52 +02:00
|
|
|
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-04-29 19:57:53 +02:00
|
|
|
containerGroupsClient.Authorizer = auth
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
return getAciAPIService(containerGroupsClient, aciContext), nil
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciAPIService {
|
|
|
|
return &aciAPIService{
|
2020-05-05 17:14:26 +02:00
|
|
|
aciContainerService: aciContainerService{
|
2020-05-05 15:37:12 +02:00
|
|
|
containerGroupsClient: cgc,
|
|
|
|
ctx: aciCtx,
|
|
|
|
},
|
2020-05-05 17:14:26 +02:00
|
|
|
aciComposeService: aciComposeService{
|
2020-05-05 15:37:12 +02:00
|
|
|
containerGroupsClient: cgc,
|
|
|
|
ctx: aciCtx,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
type aciAPIService struct {
|
2020-05-05 17:14:26 +02:00
|
|
|
aciContainerService
|
|
|
|
aciComposeService
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
func (a *aciAPIService) ContainerService() containers.Service {
|
2020-05-05 15:37:12 +02:00
|
|
|
return &aciContainerService{
|
2020-05-05 17:14:26 +02:00
|
|
|
containerGroupsClient: a.aciContainerService.containerGroupsClient,
|
|
|
|
ctx: a.aciContainerService.ctx,
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
func (a *aciAPIService) ComposeService() compose.Service {
|
2020-05-05 15:37:12 +02:00
|
|
|
return &aciComposeService{
|
2020-05-05 17:14:26 +02:00
|
|
|
containerGroupsClient: a.aciComposeService.containerGroupsClient,
|
|
|
|
ctx: a.aciComposeService.ctx,
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type aciContainerService struct {
|
|
|
|
containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
|
ctx store.AciContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *aciContainerService) 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)
|
2020-04-29 19:57:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.NotDone() {
|
2020-05-04 09:57:52 +02:00
|
|
|
containerGroups = append(containerGroups, result.Values()...)
|
2020-04-29 19:57:53 +02:00
|
|
|
if err := result.NextWithContext(ctx); err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 10:58:24 +02:00
|
|
|
var 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-06 17:14:53 +02:00
|
|
|
var containerID string
|
|
|
|
if *container.Name == singleContainerName {
|
|
|
|
containerID = *containerGroup.Name
|
|
|
|
} else {
|
|
|
|
containerID = *containerGroup.Name + "_" + *container.Name
|
|
|
|
}
|
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
|
|
|
}
|
2020-04-29 19:57:53 +02:00
|
|
|
res = append(res, containers.Container{
|
2020-05-06 17:14:53 +02:00
|
|
|
ID: containerID,
|
2020-05-04 09:57:52 +02:00
|
|
|
Image: *container.Image,
|
2020-05-03 14:51:03 +02:00
|
|
|
Status: status,
|
2020-04-29 19:57:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
func (cs *aciContainerService) 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,
|
|
|
|
})
|
|
|
|
}
|
2020-05-04 11:51:40 +02:00
|
|
|
project := compose.Project{
|
|
|
|
Name: r.ID,
|
|
|
|
Config: types.Config{
|
|
|
|
Services: []types.ServiceConfig{
|
|
|
|
{
|
2020-05-06 17:14:53 +02:00
|
|
|
Name: singleContainerName,
|
2020-05-04 11:51:40 +02:00
|
|
|
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)
|
2020-05-01 15:48:20 +02:00
|
|
|
groupDefinition, err := convert.ToContainerGroup(cs.ctx, project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
return createACIContainers(ctx, cs.ctx, groupDefinition)
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-06 17:14:53 +02:00
|
|
|
func getGrouNameContainername(containerID string) (groupName string, containerName string) {
|
|
|
|
tokens := strings.Split(containerID, "_")
|
|
|
|
groupName = tokens[0]
|
|
|
|
if len(tokens) > 1 {
|
|
|
|
containerName = tokens[len(tokens)-1]
|
|
|
|
groupName = containerID[:len(containerID)-(len(containerName)+1)]
|
|
|
|
} else {
|
|
|
|
containerName = singleContainerName
|
|
|
|
}
|
|
|
|
return groupName, containerName
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
func (cs *aciContainerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
2020-05-06 17:14:53 +02:00
|
|
|
groupName, containerAciName := getGrouNameContainername(name)
|
|
|
|
containerExecResponse, err := execACIContainer(ctx, cs.ctx, command, groupName, containerAciName)
|
2020-05-03 13:35:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec(
|
|
|
|
context.Background(),
|
|
|
|
*containerExecResponse.WebSocketURI,
|
|
|
|
*containerExecResponse.Password,
|
|
|
|
reader,
|
|
|
|
writer,
|
|
|
|
)
|
|
|
|
}
|
2020-05-03 13:41:45 +02:00
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
|
2020-05-06 17:14:53 +02:00
|
|
|
groupName, containerAciName := getGrouNameContainername(containerName)
|
|
|
|
logs, err := getACIContainerLogs(ctx, cs.ctx, groupName, containerAciName)
|
2020-05-03 13:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-04 16:38:02 +02:00
|
|
|
if req.Tail != "all" {
|
|
|
|
tail, err := strconv.Atoi(req.Tail)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lines := strings.Split(logs, "\n")
|
|
|
|
|
|
|
|
// If asked for less lines than exist, take only those lines
|
|
|
|
if tail <= len(lines) {
|
|
|
|
logs = strings.Join(lines[len(lines)-tail:], "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprint(req.Writer, logs)
|
2020-05-03 13:41:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 10:58:24 +02:00
|
|
|
|
2020-05-10 22:37:28 +02:00
|
|
|
func (cs *aciContainerService) Delete(ctx context.Context, containerID string, _ bool) error {
|
|
|
|
cg, err := deleteACIContainerGroup(ctx, cs.ctx, containerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cg.StatusCode == http.StatusNoContent {
|
|
|
|
return ErrNoSuchContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
type aciComposeService struct {
|
|
|
|
containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
|
ctx store.AciContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
|
2020-05-05 10:58:24 +02:00
|
|
|
project, err := compose.ProjectFromOptions(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Up on project with name %q\n", project.Name)
|
|
|
|
groupDefinition, err := convert.ToContainerGroup(cs.ctx, *project)
|
2020-05-06 17:14:53 +02:00
|
|
|
|
2020-05-05 10:58:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 16:27:22 +02:00
|
|
|
return createACIContainers(ctx, cs.ctx, groupDefinition)
|
2020-05-05 10:58:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:37:12 +02:00
|
|
|
func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
|
2020-05-05 10:58:24 +02:00
|
|
|
project, err := compose.ProjectFromOptions(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Down on project with name %q\n", project.Name)
|
2020-05-10 22:37:28 +02:00
|
|
|
|
|
|
|
cg, err := deleteACIContainerGroup(ctx, cs.ctx, project.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cg.StatusCode == http.StatusNoContent {
|
|
|
|
return ErrNoSuchContainer
|
|
|
|
}
|
|
|
|
|
2020-05-05 10:58:24 +02:00
|
|
|
return err
|
|
|
|
}
|