# This Dockerfile will build an image that is configured to use Fluentd to
# collect container log files from the specified paths and send them to the
# Elasticsearch.
# The environment variable that controls which log files are collected is
# FILES_TO_COLLECT. Files specified in the environment variable should be
# separated by whitespace, as in "/var/log/syslog /var/log/nginx/access.log".
# This configuration assumes that the cluster this pod is running in has an
# Elasticsearch instance reachable via a service named elasticsearch-logging.

FROM ubuntu:14.04
MAINTAINER Alex Robinson "arob@google.com"

# Ensure there are enough file descriptors for running Fluentd.
RUN ulimit -n 65536

# Disable prompts from apt.
ENV DEBIAN_FRONTEND noninteractive

# Install the standard, official Fluentd agent (called td-agent, for the
# project's parent company, Treasure Data).
RUN apt-get -q update && \
    apt-get install -y curl && \
    apt-get install -y -q libcurl4-openssl-dev make && \
    apt-get clean
RUN /usr/bin/curl -L http://toolbelt.treasuredata.com/sh/install-ubuntu-trusty-td-agent2.sh | sh

# Change the default user and group to root. Needed to ensure Fluentd can access
# files anywhere in the filesystem that it's requested to.
RUN sed -i -e "s/USER=td-agent/USER=root/" -e "s/GROUP=td-agent/GROUP=root/" /etc/init.d/td-agent

# Install the Elasticsearch Fluentd plug-in.
RUN /usr/sbin/td-agent-gem install fluent-plugin-elasticsearch

# Copy the configuration file generator for creating input configurations for
# each file specified in the FILES_TO_COLLECT environment variable.
COPY config_generator.sh /usr/local/sbin/config_generator.sh

# Copy the Fluentd configuration file for collecting from all the inputs
# generated by the config generator and sending them to Elasticsearch.
COPY td-agent.conf /etc/td-agent/td-agent.conf

# Run the config generator to get the config files in place and start Fluentd.
# We have to run the config generator at runtime rather than now so that it can
# incorporate the files provided in the environment variable in its config.
CMD /usr/local/sbin/config_generator.sh && /usr/sbin/td-agent -qq --use-v1-config --suppress-repeated-stacktrace > /var/log/td-agent/td-agent.log
