|
|
@@ -21,11 +21,6 @@ import (
|
|
|
"github.com/docker/api/context/store"
|
|
|
)
|
|
|
|
|
|
-type containerService struct {
|
|
|
- containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
- ctx store.AciContext
|
|
|
-}
|
|
|
-
|
|
|
func init() {
|
|
|
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
|
|
|
return New(ctx)
|
|
|
@@ -36,8 +31,8 @@ func getter() interface{} {
|
|
|
return &store.AciContext{}
|
|
|
}
|
|
|
|
|
|
-// New creates a backend that can manage containers on ACI
|
|
|
-func New(ctx context.Context) (containers.ContainerService, error) {
|
|
|
+// New creates a backend that can manage containers
|
|
|
+func New(ctx context.Context) (backend.Service, error) {
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
|
|
contextStore, err := store.New()
|
|
|
if err != nil {
|
|
|
@@ -53,13 +48,47 @@ func New(ctx context.Context) (containers.ContainerService, error) {
|
|
|
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
|
|
|
containerGroupsClient.Authorizer = auth
|
|
|
|
|
|
- return &containerService{
|
|
|
- containerGroupsClient: containerGroupsClient,
|
|
|
- ctx: aciContext,
|
|
|
- }, nil
|
|
|
+ return getAciApiService(containerGroupsClient, aciContext), nil
|
|
|
+}
|
|
|
+
|
|
|
+func getAciApiService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciApiService {
|
|
|
+ return &aciApiService{
|
|
|
+ container: aciContainerService{
|
|
|
+ containerGroupsClient: cgc,
|
|
|
+ ctx: aciCtx,
|
|
|
+ },
|
|
|
+ compose: aciComposeService{
|
|
|
+ containerGroupsClient: cgc,
|
|
|
+ ctx: aciCtx,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type aciApiService struct {
|
|
|
+ container aciContainerService
|
|
|
+ compose aciComposeService
|
|
|
+}
|
|
|
+
|
|
|
+func (a *aciApiService) ContainerService() containers.Service {
|
|
|
+ return &aciContainerService{
|
|
|
+ containerGroupsClient: a.container.containerGroupsClient,
|
|
|
+ ctx: a.container.ctx,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (a *aciApiService) ComposeService() compose.Service {
|
|
|
+ return &aciComposeService{
|
|
|
+ containerGroupsClient: a.compose.containerGroupsClient,
|
|
|
+ ctx: a.compose.ctx,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type aciContainerService struct {
|
|
|
+ containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
+ ctx store.AciContext
|
|
|
}
|
|
|
|
|
|
-func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
|
|
|
+func (cs *aciContainerService) List(ctx context.Context) ([]containers.Container, error) {
|
|
|
var containerGroups []containerinstance.ContainerGroup
|
|
|
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
|
|
|
if err != nil {
|
|
|
@@ -73,7 +102,7 @@ func (cs *containerService) List(ctx context.Context) ([]containers.Container, e
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- res := []containers.Container{}
|
|
|
+ var res []containers.Container
|
|
|
for _, containerGroup := range containerGroups {
|
|
|
group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
|
|
|
if err != nil {
|
|
|
@@ -96,7 +125,7 @@ func (cs *containerService) List(ctx context.Context) ([]containers.Container, e
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
-func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
|
|
+func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
|
|
var ports []types.ServicePortConfig
|
|
|
for _, p := range r.Ports {
|
|
|
ports = append(ports, types.ServicePortConfig{
|
|
|
@@ -127,7 +156,7 @@ func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfi
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
|
|
+func (cs *aciContainerService) 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
|
|
|
@@ -142,7 +171,7 @@ func (cs *containerService) Exec(ctx context.Context, name string, command strin
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-func (cs *containerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
|
|
|
+func (cs *aciContainerService) Logs(ctx context.Context, containerName string, req containers.LogsRequest) error {
|
|
|
logs, err := getACIContainerLogs(ctx, cs.ctx, containerName, containerName)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -163,3 +192,32 @@ func (cs *containerService) Logs(ctx context.Context, containerName string, req
|
|
|
_, err = fmt.Fprint(req.Writer, logs)
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+type aciComposeService struct {
|
|
|
+ containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
+ ctx store.AciContext
|
|
|
+}
|
|
|
+
|
|
|
+func (cs *aciComposeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
|
|
|
+ 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)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ _, err = createACIContainers(ctx, cs.ctx, groupDefinition)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (cs *aciComposeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
|
|
|
+ project, err := compose.ProjectFromOptions(&opts)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ logrus.Debugf("Down on project with name %q\n", project.Name)
|
|
|
+ _, err = deleteACIContainerGroup(ctx, cs.ctx, project.Name)
|
|
|
+ return err
|
|
|
+}
|