-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
134 lines (108 loc) · 3.15 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/daemon/logger"
"github.com/docker/go-plugins-helpers/sdk"
"github.com/rchicoli/docker-log-elasticsearch/pkg/docker"
)
var logLevels = map[string]log.Level{
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
}
// StartLoggingRequest payload
type StartLoggingRequest struct {
File string `json:"file,omitempty"`
Info logger.Info `json:"info,omitempty"`
}
// StopLoggingRequest payload
type StopLoggingRequest struct {
File string `json:"file,omitempty"`
}
// CapabilitiesResponse payload
type CapabilitiesResponse struct {
Cap logger.Capability `json:"capabilities,omitempty"`
Err string `json:"err,omitempty"`
}
func main() {
levelVal := os.Getenv("LOG_LEVEL")
if levelVal == "" {
levelVal = "info"
}
if level, exists := logLevels[levelVal]; exists {
log.SetLevel(level)
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
} else {
fmt.Fprintln(os.Stderr, "invalid log level: ", levelVal)
os.Exit(1)
}
h := sdk.NewHandler(`{"Implements": ["LoggingDriver"]}`)
d := docker.NewDriver()
h.HandleFunc("/LogDriver.StartLogging", func(w http.ResponseWriter, r *http.Request) {
var req StartLoggingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
respond(fmt.Errorf("error: could not decode payload: %v", err), w)
return
}
if req.Info.ContainerID == "" {
respond(errors.New("error: could not find containerID in request payload"), w)
return
}
err := d.StartLogging(req.File, req.Info)
// reader returns EOF if NoBody is sent
respond(err, w)
})
h.HandleFunc("/LogDriver.StopLogging", func(w http.ResponseWriter, r *http.Request) {
var req StopLoggingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := d.StopLogging(req.File)
// reader returns EOF if NoBody is sent
respond(err, w)
})
h.HandleFunc("/LogDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(&CapabilitiesResponse{
Cap: logger.Capability{ReadLogs: false},
})
})
// TODO: implement read logs from elasticsearch
// h.HandleFunc("/LogDriver.ReadLogs", func(w http.ResponseWriter, r *http.Request) {
// var req ReadLogsRequest
// if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
// stream, err := d.ReadLogs(req.Info, req.Config)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// defer stream.Close()
// w.Header().Set("Content-Type", "application/x-json-stream")
// wf := ioutils.NewWriteFlusher(w)
// io.Copy(wf, stream)
// })
if err := h.ServeUnix(d.Name(), 0); err != nil {
log.WithError(err).Fatal("error: serving unix")
}
}
type response struct {
Err string `json:"err,omitempty"`
}
func respond(err error, w http.ResponseWriter) {
var res response
if err != nil {
res.Err = err.Error()
}
json.NewEncoder(w).Encode(&res)
}