2020-06-18 16:13:24 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
|
|
|
|
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-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-06-30 12:23:22 +02:00
|
|
|
"strings"
|
2020-05-05 16:51:41 +02:00
|
|
|
"time"
|
2020-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
|
|
|
|
"github.com/Azure/go-autorest/autorest"
|
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2020-05-05 16:51:41 +02:00
|
|
|
tm "github.com/buger/goterm"
|
2020-05-04 20:38:10 +02:00
|
|
|
"github.com/gobwas/ws"
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2020-06-30 12:23:22 +02:00
|
|
|
"github.com/morikuni/aec"
|
2020-05-04 20:38:10 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/aci/convert"
|
|
|
|
"github.com/docker/compose-cli/aci/login"
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/containers"
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/context/store"
|
|
|
|
"github.com/docker/compose-cli/errdefs"
|
|
|
|
"github.com/docker/compose-cli/progress"
|
2020-05-01 15:28:44 +02:00
|
|
|
)
|
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerGroupsClient, err := login.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-05-04 23:50:00 +02:00
|
|
|
if err != nil {
|
2020-05-05 16:27:22 +02:00
|
|
|
return errors.Wrapf(err, "cannot get container group client")
|
2020-05-04 23:50:00 +02:00
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
// Check if the container group already exists
|
|
|
|
_, err = containerGroupsClient.Get(ctx, aciContext.ResourceGroup, *groupDefinition.Name)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(autorest.DetailedError); ok {
|
|
|
|
if err.StatusCode != http.StatusNotFound {
|
2020-05-05 16:27:22 +02:00
|
|
|
return err
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 16:27:22 +02:00
|
|
|
return err
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 16:27:22 +02:00
|
|
|
return fmt.Errorf("container group %q already exists", *groupDefinition.Name)
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:03:28 +02:00
|
|
|
return createOrUpdateACIContainers(ctx, aciContext, groupDefinition)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createOrUpdateACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error {
|
2020-06-17 22:19:08 +02:00
|
|
|
w := progress.ContextWriter(ctx)
|
2020-08-14 17:07:33 +02:00
|
|
|
containerGroupsClient, err := login.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-06-18 10:03:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot get container group client")
|
|
|
|
}
|
2020-07-10 14:41:13 +02:00
|
|
|
groupDisplay := "Group " + *groupDefinition.Name
|
2020-06-17 22:19:08 +02:00
|
|
|
w.Event(progress.Event{
|
2020-07-10 14:41:13 +02:00
|
|
|
ID: groupDisplay,
|
2020-06-17 22:19:08 +02:00
|
|
|
Status: progress.Working,
|
|
|
|
StatusText: "Waiting",
|
|
|
|
})
|
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
future, err := containerGroupsClient.CreateOrUpdate(
|
|
|
|
ctx,
|
|
|
|
aciContext.ResourceGroup,
|
|
|
|
*groupDefinition.Name,
|
|
|
|
groupDefinition,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-05-05 16:27:22 +02:00
|
|
|
return err
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 22:19:08 +02:00
|
|
|
w.Event(progress.Event{
|
2020-07-10 14:41:13 +02:00
|
|
|
ID: groupDisplay,
|
2020-06-17 22:19:08 +02:00
|
|
|
Status: progress.Done,
|
|
|
|
StatusText: "Created",
|
|
|
|
})
|
|
|
|
for _, c := range *groupDefinition.Containers {
|
2020-07-01 14:37:37 +02:00
|
|
|
if c.Name != nil && *c.Name != convert.ComposeDNSSidecarName {
|
|
|
|
w.Event(progress.Event{
|
|
|
|
ID: *c.Name,
|
|
|
|
Status: progress.Working,
|
|
|
|
StatusText: "Waiting",
|
|
|
|
})
|
|
|
|
}
|
2020-06-17 22:19:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
err = future.WaitForCompletionRef(ctx, containerGroupsClient.Client)
|
|
|
|
if err != nil {
|
2020-05-05 16:27:22 +02:00
|
|
|
return err
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
2020-06-17 22:19:08 +02:00
|
|
|
|
|
|
|
for _, c := range *groupDefinition.Containers {
|
2020-07-01 14:37:37 +02:00
|
|
|
if c.Name != nil && *c.Name != convert.ComposeDNSSidecarName {
|
|
|
|
w.Event(progress.Event{
|
|
|
|
ID: *c.Name,
|
|
|
|
Status: progress.Done,
|
|
|
|
StatusText: "Done",
|
|
|
|
})
|
|
|
|
}
|
2020-06-17 22:19:08 +02:00
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
|
2020-05-05 16:27:22 +02:00
|
|
|
return err
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 10:53:06 +02:00
|
|
|
func getACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) (containerinstance.ContainerGroup, error) {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerGroupsClient, err := login.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-06-12 10:53:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return containerinstance.ContainerGroup{}, fmt.Errorf("cannot get container group client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return containerGroupsClient.Get(ctx, aciContext.ResourceGroup, containerGroupName)
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
func getACIContainerGroups(ctx context.Context, subscriptionID string, resourceGroup string) ([]containerinstance.ContainerGroup, error) {
|
|
|
|
groupsClient, err := login.NewContainerGroupsClient(subscriptionID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var containerGroups []containerinstance.ContainerGroup
|
|
|
|
result, err := groupsClient.ListByResourceGroup(ctx, resourceGroup)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.NotDone() {
|
|
|
|
containerGroups = append(containerGroups, result.Values()...)
|
|
|
|
if err := result.NextWithContext(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var groups []containerinstance.ContainerGroup
|
|
|
|
for _, group := range containerGroups {
|
|
|
|
group, err := groupsClient.Get(ctx, resourceGroup, *group.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
groups = append(groups, group)
|
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:37:28 +02:00
|
|
|
func deleteACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) (containerinstance.ContainerGroup, error) {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerGroupsClient, err := login.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-05-05 10:58:24 +02:00
|
|
|
if err != nil {
|
2020-05-10 22:37:28 +02:00
|
|
|
return containerinstance.ContainerGroup{}, fmt.Errorf("cannot get container group client: %v", err)
|
2020-05-05 10:58:24 +02:00
|
|
|
}
|
2020-05-10 22:37:28 +02:00
|
|
|
|
2020-05-05 10:58:24 +02:00
|
|
|
return containerGroupsClient.Delete(ctx, aciContext.ResourceGroup, containerGroupName)
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:49:02 +02:00
|
|
|
func stopACIContainerGroup(ctx context.Context, aciContext store.AciContext, containerGroupName string) error {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerGroupsClient, err := login.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-08-11 17:49:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot get container group client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := containerGroupsClient.Stop(ctx, aciContext.ResourceGroup, containerGroupName)
|
|
|
|
if result.StatusCode == http.StatusNotFound {
|
|
|
|
return errdefs.ErrNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-03 13:35:25 +02:00
|
|
|
func execACIContainer(ctx context.Context, aciContext store.AciContext, command, containerGroup string, containerName string) (c containerinstance.ContainerExecResponse, err error) {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerClient, err := login.NewContainerClient(aciContext.SubscriptionID)
|
2020-05-04 23:50:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return c, errors.Wrapf(err, "cannot get container client")
|
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
rows, cols := getTermSize()
|
|
|
|
containerExecRequest := containerinstance.ContainerExecRequest{
|
|
|
|
Command: to.StringPtr(command),
|
|
|
|
TerminalSize: &containerinstance.ContainerExecRequestTerminalSize{
|
|
|
|
Rows: rows,
|
|
|
|
Cols: cols,
|
|
|
|
},
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
return containerClient.ExecuteCommand(
|
|
|
|
ctx,
|
|
|
|
aciContext.ResourceGroup,
|
|
|
|
containerGroup,
|
|
|
|
containerName,
|
|
|
|
containerExecRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTermSize() (*int32, *int32) {
|
|
|
|
rows := tm.Height()
|
|
|
|
cols := tm.Width()
|
|
|
|
return to.Int32Ptr(int32(rows)), to.Int32Ptr(int32(cols))
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:31:27 +02:00
|
|
|
func exec(ctx context.Context, address string, password string, request containers.ExecRequest) error {
|
2020-05-03 13:35:25 +02:00
|
|
|
conn, _, _, err := ws.DefaultDialer.Dial(ctx, address)
|
2020-05-01 15:28:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
err = wsutil.WriteClientMessage(conn, ws.OpText, []byte(password))
|
2020-05-01 15:28:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-04 20:38:10 +02:00
|
|
|
downstreamChannel := make(chan error, 10)
|
|
|
|
upstreamChannel := make(chan error, 10)
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, _, err := wsutil.ReadServerData(conn)
|
|
|
|
if err != nil {
|
2020-05-04 20:38:10 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
downstreamChannel <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
downstreamChannel <- err
|
2020-05-01 15:28:44 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-08 14:31:27 +02:00
|
|
|
fmt.Fprint(request.Stdout, string(msg))
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
}()
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-07-08 14:31:27 +02:00
|
|
|
if request.Interactive {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// We send each byte, byte-per-byte over the
|
|
|
|
// websocket because the console is in raw mode
|
|
|
|
buffer := make([]byte, 1)
|
|
|
|
n, err := request.Stdin.Read(buffer)
|
2020-05-04 20:38:10 +02:00
|
|
|
if err != nil {
|
2020-07-08 14:31:27 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
upstreamChannel <- nil
|
|
|
|
return
|
|
|
|
}
|
2020-05-04 20:38:10 +02:00
|
|
|
upstreamChannel <- err
|
2020-05-05 16:01:31 +02:00
|
|
|
return
|
2020-05-04 20:38:10 +02:00
|
|
|
}
|
2020-07-08 14:31:27 +02:00
|
|
|
|
|
|
|
if n > 0 {
|
|
|
|
err := wsutil.WriteClientMessage(conn, ws.OpText, buffer)
|
|
|
|
if err != nil {
|
|
|
|
upstreamChannel <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
}
|
2020-07-08 14:31:27 +02:00
|
|
|
}()
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
for {
|
|
|
|
select {
|
2020-05-04 20:38:10 +02:00
|
|
|
case err := <-downstreamChannel:
|
|
|
|
return errors.Wrap(err, "failed to read input from container")
|
|
|
|
case err := <-upstreamChannel:
|
|
|
|
return errors.Wrap(err, "failed to send input to container")
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:40:58 +02:00
|
|
|
func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string, tail *int32) (string, error) {
|
2020-08-14 17:07:33 +02:00
|
|
|
containerClient, err := login.NewContainerClient(aciContext.SubscriptionID)
|
2020-05-04 23:50:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "cannot get container client")
|
|
|
|
}
|
2020-05-03 13:41:45 +02:00
|
|
|
|
2020-06-29 15:40:58 +02:00
|
|
|
logs, err := containerClient.ListLogs(ctx, aciContext.ResourceGroup, containerGroupName, containerName, tail)
|
2020-05-03 13:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot get container logs: %v", err)
|
|
|
|
}
|
|
|
|
return *logs.Content, err
|
|
|
|
}
|
|
|
|
|
2020-07-09 10:50:48 +02:00
|
|
|
func streamLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string, req containers.LogsRequest) error {
|
2020-07-02 10:38:03 +02:00
|
|
|
numLines := 0
|
2020-09-04 15:09:57 +02:00
|
|
|
previousLogLines := ""
|
|
|
|
firstDisplay := true // optimization to exit sooner in cases like docker run hello-world, do not wait another 2 secs.
|
2020-06-30 12:23:22 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
logs, err := getACIContainerLogs(ctx, aciContext, containerGroupName, containerName, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logLines := strings.Split(logs, "\n")
|
|
|
|
currentOutput := len(logLines)
|
|
|
|
|
|
|
|
// Note: a backend should not do this normally, this breaks the log
|
|
|
|
// streaming over gRPC but this is the only thing we can do with
|
|
|
|
// the kind of logs ACI is giving us. Hopefully Azue will give us
|
|
|
|
// a real logs streaming api soon.
|
2020-06-30 16:33:36 +02:00
|
|
|
b := aec.EmptyBuilder
|
2020-07-02 10:38:03 +02:00
|
|
|
b = b.Up(uint(numLines))
|
2020-07-09 10:50:48 +02:00
|
|
|
fmt.Fprint(req.Writer, b.Column(0).ANSI)
|
2020-06-30 12:23:22 +02:00
|
|
|
|
2020-07-09 10:50:48 +02:00
|
|
|
numLines = getBacktrackLines(logLines, req.Width)
|
2020-06-30 12:23:22 +02:00
|
|
|
|
2020-07-02 10:38:03 +02:00
|
|
|
for i := 0; i < currentOutput-1; i++ {
|
2020-07-09 10:50:48 +02:00
|
|
|
fmt.Fprintln(req.Writer, logLines[i])
|
2020-07-02 10:38:03 +02:00
|
|
|
}
|
2020-06-30 12:23:22 +02:00
|
|
|
|
2020-09-04 15:09:57 +02:00
|
|
|
if (firstDisplay || previousLogLines == logs) && !isContainerRunning(ctx, aciContext, containerGroupName, containerName) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
firstDisplay = false
|
|
|
|
previousLogLines = logs
|
|
|
|
|
2020-07-03 10:49:57 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
}
|
2020-06-30 12:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:09:57 +02:00
|
|
|
func isContainerRunning(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string) bool {
|
|
|
|
group, err := getACIContainerGroup(ctx, aciContext, containerGroupName)
|
|
|
|
if err != nil {
|
|
|
|
return false // group has disappeared
|
|
|
|
}
|
|
|
|
for _, container := range *group.Containers {
|
|
|
|
if *container.Name == containerName {
|
|
|
|
if convert.GetStatus(container, group) == convert.StatusRunning {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:11:09 +02:00
|
|
|
func getBacktrackLines(lines []string, terminalWidth int) int {
|
2020-07-10 13:00:41 +02:00
|
|
|
if terminalWidth == 0 { // no terminal width has been set, do not divide by zero
|
|
|
|
return len(lines)
|
|
|
|
}
|
2020-07-02 11:11:09 +02:00
|
|
|
numLines := 0
|
|
|
|
for i := 0; i < len(lines)-1; i++ {
|
|
|
|
numLines++
|
|
|
|
if len(lines[i]) > terminalWidth {
|
|
|
|
numLines += len(lines[i]) / terminalWidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numLines
|
|
|
|
}
|