Skip to content

Commit

Permalink
Refactor the entrypoint for easier maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
aerostitch committed Aug 9, 2017
1 parent 795c013 commit 1f1eb0c
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 648 deletions.
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ FROM debian:jessie
MAINTAINER Datadog <[email protected]>

ENV DOCKER_DD_AGENT=yes \
AGENT_VERSION=1:5.16.0-1
AGENT_VERSION=1:5.16.0-1 \
PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:${PATH}" \
PYTHONPATH=/opt/datadog-agent/agent \
DD_CONF_LOG_TO_SYSLOG=no \
NON_LOCAL_TRAFFIC=yes \
DD_SUPERVISOR_DELETE_USER=yes

# Install the Agent
RUN echo "deb http://apt.datadoghq.com/ stable main" > /etc/apt/sources.list.d/datadog.list \
Expand All @@ -15,15 +20,9 @@ RUN echo "deb http://apt.datadoghq.com/ stable main" > /etc/apt/sources.list.d/d
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Configure the Agent
# 1. Listen to statsd (8125) and traces (8126) from other containers
# 2. Turn syslog off
# 3. Remove dd-agent user from supervisor configuration
# 4. Remove dd-agent user from init.d configuration
# 5. Fix permission on /etc/init.d/datadog-agent
# 1. Remove dd-agent user from init.d configuration
# 2. Fix permission on /etc/init.d/datadog-agent
RUN mv /etc/dd-agent/datadog.conf.example /etc/dd-agent/datadog.conf \
&& sed -i -e"s/^.*non_local_traffic:.*$/non_local_traffic: yes/" /etc/dd-agent/datadog.conf \
&& sed -i -e"s/^.*log_to_syslog:.*$/log_to_syslog: no/" /etc/dd-agent/datadog.conf \
&& sed -i "/user=dd-agent/d" /etc/dd-agent/supervisor.conf \
&& sed -i 's/AGENTUSER="dd-agent"/AGENTUSER="root"/g' /etc/init.d/datadog-agent \
&& rm /etc/dd-agent/conf.d/network.yaml.default \
|| chmod +x /etc/init.d/datadog-agent
Expand All @@ -33,6 +32,8 @@ COPY conf.d/docker_daemon.yaml /etc/dd-agent/conf.d/docker_daemon.yaml

COPY entrypoint.sh /entrypoint.sh

COPY config_builder.py /config_builder.py

# Extra conf.d and checks.d
VOLUME ["/conf.d", "/checks.d"]

Expand Down
15 changes: 7 additions & 8 deletions alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ ENV DD_HOME=/opt/datadog-agent \
DD_START_AGENT=0 \
DOCKER_DD_AGENT=yes \
PYCURL_SSL_LIBRARY=openssl \
AGENT_VERSION=5.16.0
AGENT_VERSION=5.16.0 \
PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:$PATH" \
PYTHONPATH=/opt/datadog-agent/agent \
DD_CONF_LOG_TO_SYSLOG=no \
NON_LOCAL_TRAFFIC=yes \
DD_SUPERVISOR_DELETE_USER=yes

# Add Docker check
COPY conf.d/docker_daemon.yaml "$DD_HOME/agent/conf.d/docker_daemon.yaml"

# Add install and config files
ADD https://raw.githubusercontent.com/DataDog/dd-agent/master/packaging/datadog-agent/source/setup_agent.sh /tmp/setup_agent.sh
COPY entrypoint.sh /entrypoint.sh
ADD https://raw.githubusercontent.com/DataDog/docker-dd-agent/master/config_builder.py /config_builder.py

# Expose supervisor and DogStatsD port
EXPOSE 9001/tcp 8125/udp
Expand All @@ -30,14 +36,7 @@ RUN apk add -qU --no-cache -t .build-deps gcc musl-dev pgcluster-dev linux-heade
&& apk del -q .build-deps

# Configure the Agent
# 1. Listen to statsd from other containers
# 2. Turn syslog off
# 3. Remove dd-agent user from supervisor configuration
# 4. Remove setup script
RUN cp "$DD_HOME/agent/datadog.conf.example" "$DD_HOME/agent/datadog.conf" \
&& sed -i -e"s/^.*non_local_traffic:.*$/non_local_traffic: yes/" "$DD_HOME/agent/datadog.conf" \
&& sed -i -e"s/^.*log_to_syslog:.*$/log_to_syslog: no/" "$DD_HOME/agent/datadog.conf" \
&& sed -i "/user=dd-agent/d" "$DD_HOME/agent/supervisor.conf" \
&& rm "$DD_HOME/agent/conf.d/network.yaml.default" \
|| rm /tmp/setup_agent.sh

Expand Down
176 changes: 1 addition & 175 deletions alpine/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,179 +3,7 @@
set -x

##### Core config #####

if [[ $DD_API_KEY ]]; then
export API_KEY=${DD_API_KEY}
fi

if [[ $DD_API_KEY_FILE ]]; then
export API_KEY=$(cat $DD_API_KEY_FILE)
fi

if [[ $API_KEY ]]; then
sed -i -e "s/^.*api_key:.*$/api_key: ${API_KEY}/" /opt/datadog-agent/agent/datadog.conf
else
echo "You must set API_KEY environment variable to run the Datadog Agent container"
exit 1
fi

if [[ $DD_HOSTNAME ]]; then
sed -i -r -e "s/^# ?hostname.*$/hostname: ${DD_HOSTNAME}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $DD_TAGS ]]; then
export TAGS=${DD_TAGS}
fi

if [[ $EC2_TAGS ]]; then
export EC2_TAGS=${EC2_TAGS//\//\\/} # escape forward slashes from tags before invoking sed
sed -i -e "s/^# collect_ec2_tags.*$/collect_ec2_tags: ${EC2_TAGS}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $TAGS ]]; then
export TAGS=${TAGS//\//\\/} # escape forward slashes from tags before invoking sed
sed -i -r -e "s/^# ?tags:.*$/tags: ${TAGS}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $DD_LOG_LEVEL ]]; then
export LOG_LEVEL=$DD_LOG_LEVEL
fi

if [[ $LOG_LEVEL ]]; then
sed -i -e"s/^.*log_level:.*$/log_level: ${LOG_LEVEL}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $DD_LOGS_STDOUT ]]; then
export LOGS_STDOUT=$DD_LOGS_STDOUT
fi

if [[ $LOGS_STDOUT == "yes" ]]; then
sed -i -e "/^.*_logfile.*$/d" /opt/datadog-agent/agent/supervisor.conf
sed -i -e "/^.*\[program:.*\].*$/a stdout_logfile=\/dev\/stdout" /opt/datadog-agent/agent/supervisor.conf
sed -i -e "/^.*\[program:.*\].*$/a stdout_logfile_maxbytes=0" /opt/datadog-agent/agent/supervisor.conf
sed -i -e "/^.*\[program:.*\].*$/a stderr_logfile=\/dev\/stderr" /opt/datadog-agent/agent/supervisor.conf
sed -i -e "/^.*\[program:.*\].*$/a stderr_logfile_maxbytes=0" /opt/datadog-agent/agent/supervisor.conf
fi

if [[ $DD_URL ]]; then
sed -i -e 's@^.*dd_url:.*$@dd_url: '${DD_URL}'@' /opt/datadog-agent/agent/datadog.conf
fi

if [[ $STATSD_METRIC_NAMESPACE ]]; then
sed -i -e "s/^# statsd_metric_namespace:.*$/statsd_metric_namespace: ${STATSD_METRIC_NAMESPACE}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $USE_DOGSTATSD ]]; then
sed -i -e "s/^.*use_dogstatsd:.*$/use_dogstatsd: ${USE_DOGSTATSD}/" /opt/datadog-agent/agent/datadog.conf
fi


##### Proxy config #####

if [[ $PROXY_HOST ]]; then
sed -i -e "s/^# proxy_host:.*$/proxy_host: ${PROXY_HOST}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $PROXY_PORT ]]; then
sed -i -e "s/^# proxy_port:.*$/proxy_port: ${PROXY_PORT}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $PROXY_USER ]]; then
sed -i -e "s/^# proxy_user:.*$/proxy_user: ${PROXY_USER}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $PROXY_PASSWORD ]]; then
sed -i -e "s/^# proxy_password:.*$/proxy_password: ${PROXY_PASSWORD}/" /opt/datadog-agent/agent/datadog.conf
fi

##### Service discovery #####
EC2_HOST_IP=`curl --silent http://169.254.169.254/latest/meta-data/local-ipv4 --max-time 1`

if [[ $SD_BACKEND ]]; then
sed -i -e "s/^# service_discovery_backend:.*$/service_discovery_backend: ${SD_BACKEND}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_CONFIG_BACKEND ]]; then
sed -i -e "s/^# sd_config_backend:.*$/sd_config_backend: ${SD_CONFIG_BACKEND}/" /opt/datadog-agent/agent/datadog.conf
# If no SD_BACKEND_HOST value is defined AND running in EC2 and host ip is available
if [[ -z $SD_BACKEND_HOST && -n $EC2_HOST_IP ]]; then
export SD_BACKEND_HOST="$EC2_HOST_IP"
fi
fi

if [[ $SD_BACKEND_HOST ]]; then
sed -i -e "s/^# sd_backend_host:.*$/sd_backend_host: ${SD_BACKEND_HOST}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_BACKEND_PORT ]]; then
sed -i -e "s/^# sd_backend_port:.*$/sd_backend_port: ${SD_BACKEND_PORT}/" /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_TEMPLATE_DIR ]]; then
sed -i -e 's@^# sd_template_dir:.*$@sd_template_dir: '${SD_TEMPLATE_DIR}'@' /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_CONSUL_TOKEN ]]; then
sed -i -e 's@^# consul_token:.*$@consul_token: '${SD_CONSUL_TOKEN}'@' /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_BACKEND_USER ]]; then
sed -i -e 's@^# sd_backend_username:.*$@sd_backend_username: '${SD_BACKEND_USER}'@' /opt/datadog-agent/agent/datadog.conf
fi

if [[ $SD_BACKEND_PASSWORD ]]; then
sed -i -e 's@^# sd_backend_password:.*$@sd_backend_password: '${SD_BACKEND_PASSWORD}'@' /opt/datadog-agent/agent/datadog.conf
fi

##### Integrations config #####

if [[ -n "${KUBERNETES}" || -n "${MESOS_MASTER}" || -n "${MESOS_SLAVE}" ]]; then
# expose supervisord as a health check
echo "
[inet_http_server]
port = 0.0.0.0:9001
" >> /opt/datadog-agent/agent/supervisor.conf
fi

if [[ $KUBERNETES ]]; then
# enable kubernetes check
cp /opt/datadog-agent/agent/conf.d/kubernetes.yaml.example /opt/datadog-agent/agent/conf.d/kubernetes.yaml

# allows to disable kube_service tagging if needed (big clusters)
if [[ $KUBERNETES_COLLECT_SERVICE_TAGS ]]; then
sed -i -e 's@# collect_service_tags:.*$@ collect_service_tags: '${KUBERNETES_COLLECT_SERVICE_TAGS}'@' /opt/datadog-agent/agent/conf.d/kubernetes.yaml
fi

# enable event collector
# WARNING: to avoid duplicates, only one agent at a time across the entire cluster should have this feature enabled.
if [[ $KUBERNETES_COLLECT_EVENTS ]]; then
sed -i -e "s@# collect_events: false@ collect_events: true@" /opt/datadog-agent/agent/conf.d/kubernetes.yaml

# enable the namespace regex
if [[ $KUBERNETES_NAMESPACE_NAME_REGEX ]]; then
sed -i -e "s@# namespace_name_regexp:@ namespace_name_regexp: ${KUBERNETES_NAMESPACE_NAME_REGEX}@" /opt/datadog-agent/agent/conf.d/kubernetes.yaml
fi
fi
fi

if [[ $MESOS_MASTER ]]; then
cp /opt/datadog-agent/agent/conf.d/mesos_master.yaml.example /opt/datadog-agent/agent/conf.d/mesos_master.yaml
cp /opt/datadog-agent/agent/conf.d/zk.yaml.example /opt/datadog-agent/agent/conf.d/zk.yaml

sed -i -e "s/localhost/leader.mesos/" /opt/datadog-agent/agent/conf.d/mesos_master.yaml
sed -i -e "s/localhost/leader.mesos/" /opt/datadog-agent/agent/conf.d/zk.yaml
fi

if [[ $MESOS_SLAVE ]]; then
cp /opt/datadog-agent/agent/conf.d/mesos_slave.yaml.example /opt/datadog-agent/agent/conf.d/mesos_slave.yaml

sed -i -e "s/localhost/$HOST/" /opt/datadog-agent/agent/conf.d/mesos_slave.yaml
fi

if [[ $MARATHON_URL ]]; then
cp /opt/datadog-agent/agent/conf.d/marathon.yaml.example /opt/datadog-agent/agent/conf.d/marathon.yaml
sed -i -e "s@# - url: \"https://server:port\"@- url: ${MARATHON_URL}@" /opt/datadog-agent/agent/conf.d/marathon.yaml
fi
/opt/datadog-agent/venv/bin/activate && python /config_builder.py

find /conf.d -name '*.yaml' -exec cp --parents {} /opt/datadog-agent/agent \;

Expand All @@ -184,8 +12,6 @@ find /checks.d -name '*.py' -exec cp {} /opt/datadog-agent/agent/checks.d \;

##### Starting up #####

export PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:$PATH"

if [[ $DOGSTATSD_ONLY ]]; then
source /opt/datadog-agent/venv/bin/activate && python /opt/datadog-agent/agent/dogstatsd.py
else
Expand Down
Loading

0 comments on commit 1f1eb0c

Please sign in to comment.