forked from cpuguy83/docker-log-driver-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
116 lines (96 loc) · 2.52 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/daemon/logger"
"github.com/docker/docker/daemon/logger/loggerutils"
)
const (
driverName = "s3logdriver"
)
// S3Logger is the logger struct that implements the Docker logger interface.
type S3Logger struct {
s3Client *s3.S3
bucket string
}
// LogOption represents options for configuring the S3 logger.
type LogOption struct {
S3Bucket string
}
func main() {
var opts LogOption
flag.StringVar(&opts.S3Bucket, "s3-bucket", "", "S3 bucket name")
flag.Parse()
if opts.S3Bucket == "" {
fmt.Println("Please provide an S3 bucket name")
os.Exit(1)
}
// Initialize AWS session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create an S3 client
s3Client := s3.New(sess)
// Create S3Logger instance
s3Logger := &S3Logger{
s3Client: s3Client,
bucket: opts.S3Bucket,
}
// Register the logger with Docker
h := logdriver.NewHandler(s3Logger)
err := h.ServeUnix(driverName, 0)
if err != nil {
fmt.Printf("Error starting the S3 logger: %s\n", err)
os.Exit(1)
}
}
// Log is the method called by Docker daemon to stream container logs.
func (l *S3Logger) Log(ctx context.Context, config logger.Message) error {
if config.Source != "" {
return nil // skip logs not coming from a container
}
containerID := config.ContainerID
reader, err := l.s3Client.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(l.bucket),
Key: aws.String(containerID),
})
if err != nil {
return fmt.Errorf("failed to get object from S3: %v", err)
}
defer reader.Body.Close()
scanner := bufio.NewScanner(reader.Body)
for scanner.Scan() {
logLine := scanner.Text()
// Send log line to Docker daemon
configLine := logger.LogLine{
Line: logLine,
Source: containerID,
Partial: false,
Timestamp: time.Now(),
}
if err := configLine.MarshalJSON(); err != nil {
return fmt.Errorf("error marshalling log line: %v", err)
}
select {
case <-ctx.Done():
return nil
default:
config.Logs <- &configLine
}
}
return nil
}
// Capabilities returns the capabilities of the logger.
func (l *S3Logger) Capabilities() *logger.Capabilities {
return &logger.Capabilities{ReadLogs: false, ReadConfig: false}
}