|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "path" |
| 8 | + "syscall" |
| 9 | + |
| 10 | + log "github.com/Sirupsen/logrus" |
| 11 | + "github.com/docker/docker/api/types/plugins/logdriver" |
| 12 | + "github.com/docker/docker/daemon/logger" |
| 13 | + "github.com/rchicoli/docker-log-elasticsearch/pkg/elasticsearch" |
| 14 | + "github.com/robfig/cron" |
| 15 | + "github.com/tonistiigi/fifo" |
| 16 | + "golang.org/x/sync/errgroup" |
| 17 | +) |
| 18 | + |
| 19 | +type container struct { |
| 20 | + cron *cron.Cron |
| 21 | + esClient elasticsearch.Client |
| 22 | + indexName string |
| 23 | + info logger.Info |
| 24 | + logger *log.Entry |
| 25 | + pipeline pipeline |
| 26 | + stream io.ReadCloser |
| 27 | +} |
| 28 | + |
| 29 | +type pipeline struct { |
| 30 | + group *errgroup.Group |
| 31 | + inputCh chan logdriver.LogEntry |
| 32 | + outputCh chan LogMessage |
| 33 | +} |
| 34 | + |
| 35 | +// newContainer stores the container's configuration in memory |
| 36 | +// and returns a pointer to the container |
| 37 | +func (d *Driver) newContainer(ctx context.Context, file string) (*container, error) { |
| 38 | + |
| 39 | + filename := path.Base(file) |
| 40 | + log.WithField("fifo", file).Debug("created fifo file") |
| 41 | + |
| 42 | + d.mu.Lock() |
| 43 | + if _, exists := d.logs[filename]; exists { |
| 44 | + return nil, fmt.Errorf("error: a logger for this container already exists: %s", filename) |
| 45 | + } |
| 46 | + d.mu.Unlock() |
| 47 | + |
| 48 | + f, err := fifo.OpenFifo(ctx, file, syscall.O_RDONLY, 0700) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("could not open fifo: %q", err) |
| 51 | + } |
| 52 | + |
| 53 | + d.mu.Lock() |
| 54 | + c := &container{stream: f} |
| 55 | + d.logs[filename] = c |
| 56 | + d.mu.Unlock() |
| 57 | + |
| 58 | + return c, nil |
| 59 | +} |
| 60 | + |
| 61 | +// getContainer retrieves the container's configuration from memory |
| 62 | +func (d *Driver) getContainer(file string) (*container, error) { |
| 63 | + |
| 64 | + filename := path.Base(file) |
| 65 | + |
| 66 | + d.mu.Lock() |
| 67 | + defer d.mu.Unlock() |
| 68 | + |
| 69 | + c, exists := d.logs[filename] |
| 70 | + if !exists { |
| 71 | + return nil, fmt.Errorf("error: logger not found for socket ID: %v", file) |
| 72 | + } |
| 73 | + |
| 74 | + return c, nil |
| 75 | +} |
0 commit comments