소스 검색

Implement logs

Djordje Lukic 5 년 전
부모
커밋
1c7270b697
6개의 변경된 파일67개의 추가작업 그리고 0개의 파일을 삭제
  1. 10 0
      azure/aci.go
  2. 10 0
      azure/backend.go
  3. 39 0
      cli/cmd/logs.go
  4. 1 0
      cli/main.go
  5. 2 0
      containers/api.go
  6. 5 0
      example/backend.go

+ 10 - 0
azure/aci.go

@@ -224,6 +224,16 @@ func exec(ctx context.Context, address string, password string, reader io.Reader
 	}
 }
 
+func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string) (string, error) {
+	containerClient := getContainerClient(aciContext.SubscriptionID)
+
+	logs, err := containerClient.ListLogs(ctx, aciContext.ResourceGroup, containerGroupName, containerName, nil)
+	if err != nil {
+		return "", fmt.Errorf("cannot get container logs: %v", err)
+	}
+	return *logs.Content, err
+}
+
 func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
 	auth, _ := auth.NewAuthorizerFromCLI()
 	containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)

+ 10 - 0
azure/backend.go

@@ -2,6 +2,7 @@ package azure
 
 import (
 	"context"
+	"fmt"
 	"io"
 
 	"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
@@ -137,3 +138,12 @@ func (cs *containerService) Exec(ctx context.Context, name string, command strin
 		writer,
 	)
 }
+
+func (cs *containerService) Logs(ctx context.Context, name string, writer io.Writer, follow bool) error {
+	logs, err := getACIContainerLogs(ctx, cs.ctx, name, name)
+	if err != nil {
+		return err
+	}
+	_, err = fmt.Fprint(writer, logs)
+	return err
+}

+ 39 - 0
cli/cmd/logs.go

@@ -0,0 +1,39 @@
+package cmd
+
+import (
+	"context"
+	"os"
+
+	"github.com/docker/api/client"
+	"github.com/pkg/errors"
+	"github.com/spf13/cobra"
+)
+
+type logsOpts struct {
+	Follow bool
+}
+
+func LogsCommand() *cobra.Command {
+	var opts logsOpts
+	cmd := &cobra.Command{
+		Use:   "logs",
+		Short: "Fetch the logs of a container",
+		Args:  cobra.ExactArgs(1),
+		RunE: func(cmd *cobra.Command, args []string) error {
+			return runLogs(cmd.Context(), args[0], opts)
+		},
+	}
+
+	cmd.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "Follow log outut")
+
+	return cmd
+}
+
+func runLogs(ctx context.Context, name string, opts logsOpts) error {
+	c, err := client.New(ctx)
+	if err != nil {
+		return errors.Wrap(err, "cannot connect to backend")
+	}
+
+	return c.ContainerService().Logs(ctx, name, os.Stdout, opts.Follow)
+}

+ 1 - 0
cli/main.go

@@ -102,6 +102,7 @@ func main() {
 		&cmd.ExampleCommand,
 		run.Command(),
 		cmd.ExecCommand(),
+		cmd.LogsCommand(),
 	)
 
 	helpFunc := root.HelpFunc()

+ 2 - 0
containers/api.go

@@ -45,4 +45,6 @@ type ContainerService interface {
 	Run(ctx context.Context, config ContainerConfig) error
 	// Exec executes a command inside a running container
 	Exec(ctx context.Context, containerName string, command string, reader io.Reader, writer io.Writer) error
+	// Logs returns all the logs of a container
+	Logs(ctx context.Context, name string, writer io.Writer, follow bool) error
 }

+ 5 - 0
example/backend.go

@@ -43,3 +43,8 @@ func (cs *containerService) Exec(ctx context.Context, name string, command strin
 	fmt.Printf("Executing command %q on container %q", command, name)
 	return nil
 }
+
+func (cs *containerService) Logs(ctx context.Context, name string, writer io.Writer, follow bool) error {
+	fmt.Fprintf(writer, "Following logs for container %q", name)
+	return nil
+}