Skip to content

Commit 1f1eb0c

Browse files
committed
Refactor the entrypoint for easier maintenance
1 parent 795c013 commit 1f1eb0c

File tree

12 files changed

+314
-648
lines changed

12 files changed

+314
-648
lines changed

Dockerfile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ FROM debian:jessie
33
MAINTAINER Datadog <[email protected]>
44

55
ENV DOCKER_DD_AGENT=yes \
6-
AGENT_VERSION=1:5.16.0-1
6+
AGENT_VERSION=1:5.16.0-1 \
7+
PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:${PATH}" \
8+
PYTHONPATH=/opt/datadog-agent/agent \
9+
DD_CONF_LOG_TO_SYSLOG=no \
10+
NON_LOCAL_TRAFFIC=yes \
11+
DD_SUPERVISOR_DELETE_USER=yes
712

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

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

3433
COPY entrypoint.sh /entrypoint.sh
3534

35+
COPY config_builder.py /config_builder.py
36+
3637
# Extra conf.d and checks.d
3738
VOLUME ["/conf.d", "/checks.d"]
3839

alpine/Dockerfile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ ENV DD_HOME=/opt/datadog-agent \
77
DD_START_AGENT=0 \
88
DOCKER_DD_AGENT=yes \
99
PYCURL_SSL_LIBRARY=openssl \
10-
AGENT_VERSION=5.16.0
10+
AGENT_VERSION=5.16.0 \
11+
PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:$PATH" \
12+
PYTHONPATH=/opt/datadog-agent/agent \
13+
DD_CONF_LOG_TO_SYSLOG=no \
14+
NON_LOCAL_TRAFFIC=yes \
15+
DD_SUPERVISOR_DELETE_USER=yes
1116

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

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

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

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

alpine/entrypoint.sh

Lines changed: 1 addition & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -3,179 +3,7 @@
33
set -x
44

55
##### Core config #####
6-
7-
if [[ $DD_API_KEY ]]; then
8-
export API_KEY=${DD_API_KEY}
9-
fi
10-
11-
if [[ $DD_API_KEY_FILE ]]; then
12-
export API_KEY=$(cat $DD_API_KEY_FILE)
13-
fi
14-
15-
if [[ $API_KEY ]]; then
16-
sed -i -e "s/^.*api_key:.*$/api_key: ${API_KEY}/" /opt/datadog-agent/agent/datadog.conf
17-
else
18-
echo "You must set API_KEY environment variable to run the Datadog Agent container"
19-
exit 1
20-
fi
21-
22-
if [[ $DD_HOSTNAME ]]; then
23-
sed -i -r -e "s/^# ?hostname.*$/hostname: ${DD_HOSTNAME}/" /opt/datadog-agent/agent/datadog.conf
24-
fi
25-
26-
if [[ $DD_TAGS ]]; then
27-
export TAGS=${DD_TAGS}
28-
fi
29-
30-
if [[ $EC2_TAGS ]]; then
31-
export EC2_TAGS=${EC2_TAGS//\//\\/} # escape forward slashes from tags before invoking sed
32-
sed -i -e "s/^# collect_ec2_tags.*$/collect_ec2_tags: ${EC2_TAGS}/" /opt/datadog-agent/agent/datadog.conf
33-
fi
34-
35-
if [[ $TAGS ]]; then
36-
export TAGS=${TAGS//\//\\/} # escape forward slashes from tags before invoking sed
37-
sed -i -r -e "s/^# ?tags:.*$/tags: ${TAGS}/" /opt/datadog-agent/agent/datadog.conf
38-
fi
39-
40-
if [[ $DD_LOG_LEVEL ]]; then
41-
export LOG_LEVEL=$DD_LOG_LEVEL
42-
fi
43-
44-
if [[ $LOG_LEVEL ]]; then
45-
sed -i -e"s/^.*log_level:.*$/log_level: ${LOG_LEVEL}/" /opt/datadog-agent/agent/datadog.conf
46-
fi
47-
48-
if [[ $DD_LOGS_STDOUT ]]; then
49-
export LOGS_STDOUT=$DD_LOGS_STDOUT
50-
fi
51-
52-
if [[ $LOGS_STDOUT == "yes" ]]; then
53-
sed -i -e "/^.*_logfile.*$/d" /opt/datadog-agent/agent/supervisor.conf
54-
sed -i -e "/^.*\[program:.*\].*$/a stdout_logfile=\/dev\/stdout" /opt/datadog-agent/agent/supervisor.conf
55-
sed -i -e "/^.*\[program:.*\].*$/a stdout_logfile_maxbytes=0" /opt/datadog-agent/agent/supervisor.conf
56-
sed -i -e "/^.*\[program:.*\].*$/a stderr_logfile=\/dev\/stderr" /opt/datadog-agent/agent/supervisor.conf
57-
sed -i -e "/^.*\[program:.*\].*$/a stderr_logfile_maxbytes=0" /opt/datadog-agent/agent/supervisor.conf
58-
fi
59-
60-
if [[ $DD_URL ]]; then
61-
sed -i -e 's@^.*dd_url:.*$@dd_url: '${DD_URL}'@' /opt/datadog-agent/agent/datadog.conf
62-
fi
63-
64-
if [[ $STATSD_METRIC_NAMESPACE ]]; then
65-
sed -i -e "s/^# statsd_metric_namespace:.*$/statsd_metric_namespace: ${STATSD_METRIC_NAMESPACE}/" /opt/datadog-agent/agent/datadog.conf
66-
fi
67-
68-
if [[ $USE_DOGSTATSD ]]; then
69-
sed -i -e "s/^.*use_dogstatsd:.*$/use_dogstatsd: ${USE_DOGSTATSD}/" /opt/datadog-agent/agent/datadog.conf
70-
fi
71-
72-
73-
##### Proxy config #####
74-
75-
if [[ $PROXY_HOST ]]; then
76-
sed -i -e "s/^# proxy_host:.*$/proxy_host: ${PROXY_HOST}/" /opt/datadog-agent/agent/datadog.conf
77-
fi
78-
79-
if [[ $PROXY_PORT ]]; then
80-
sed -i -e "s/^# proxy_port:.*$/proxy_port: ${PROXY_PORT}/" /opt/datadog-agent/agent/datadog.conf
81-
fi
82-
83-
if [[ $PROXY_USER ]]; then
84-
sed -i -e "s/^# proxy_user:.*$/proxy_user: ${PROXY_USER}/" /opt/datadog-agent/agent/datadog.conf
85-
fi
86-
87-
if [[ $PROXY_PASSWORD ]]; then
88-
sed -i -e "s/^# proxy_password:.*$/proxy_password: ${PROXY_PASSWORD}/" /opt/datadog-agent/agent/datadog.conf
89-
fi
90-
91-
##### Service discovery #####
92-
EC2_HOST_IP=`curl --silent http://169.254.169.254/latest/meta-data/local-ipv4 --max-time 1`
93-
94-
if [[ $SD_BACKEND ]]; then
95-
sed -i -e "s/^# service_discovery_backend:.*$/service_discovery_backend: ${SD_BACKEND}/" /opt/datadog-agent/agent/datadog.conf
96-
fi
97-
98-
if [[ $SD_CONFIG_BACKEND ]]; then
99-
sed -i -e "s/^# sd_config_backend:.*$/sd_config_backend: ${SD_CONFIG_BACKEND}/" /opt/datadog-agent/agent/datadog.conf
100-
# If no SD_BACKEND_HOST value is defined AND running in EC2 and host ip is available
101-
if [[ -z $SD_BACKEND_HOST && -n $EC2_HOST_IP ]]; then
102-
export SD_BACKEND_HOST="$EC2_HOST_IP"
103-
fi
104-
fi
105-
106-
if [[ $SD_BACKEND_HOST ]]; then
107-
sed -i -e "s/^# sd_backend_host:.*$/sd_backend_host: ${SD_BACKEND_HOST}/" /opt/datadog-agent/agent/datadog.conf
108-
fi
109-
110-
if [[ $SD_BACKEND_PORT ]]; then
111-
sed -i -e "s/^# sd_backend_port:.*$/sd_backend_port: ${SD_BACKEND_PORT}/" /opt/datadog-agent/agent/datadog.conf
112-
fi
113-
114-
if [[ $SD_TEMPLATE_DIR ]]; then
115-
sed -i -e 's@^# sd_template_dir:.*$@sd_template_dir: '${SD_TEMPLATE_DIR}'@' /opt/datadog-agent/agent/datadog.conf
116-
fi
117-
118-
if [[ $SD_CONSUL_TOKEN ]]; then
119-
sed -i -e 's@^# consul_token:.*$@consul_token: '${SD_CONSUL_TOKEN}'@' /opt/datadog-agent/agent/datadog.conf
120-
fi
121-
122-
if [[ $SD_BACKEND_USER ]]; then
123-
sed -i -e 's@^# sd_backend_username:.*$@sd_backend_username: '${SD_BACKEND_USER}'@' /opt/datadog-agent/agent/datadog.conf
124-
fi
125-
126-
if [[ $SD_BACKEND_PASSWORD ]]; then
127-
sed -i -e 's@^# sd_backend_password:.*$@sd_backend_password: '${SD_BACKEND_PASSWORD}'@' /opt/datadog-agent/agent/datadog.conf
128-
fi
129-
130-
##### Integrations config #####
131-
132-
if [[ -n "${KUBERNETES}" || -n "${MESOS_MASTER}" || -n "${MESOS_SLAVE}" ]]; then
133-
# expose supervisord as a health check
134-
echo "
135-
[inet_http_server]
136-
port = 0.0.0.0:9001
137-
" >> /opt/datadog-agent/agent/supervisor.conf
138-
fi
139-
140-
if [[ $KUBERNETES ]]; then
141-
# enable kubernetes check
142-
cp /opt/datadog-agent/agent/conf.d/kubernetes.yaml.example /opt/datadog-agent/agent/conf.d/kubernetes.yaml
143-
144-
# allows to disable kube_service tagging if needed (big clusters)
145-
if [[ $KUBERNETES_COLLECT_SERVICE_TAGS ]]; then
146-
sed -i -e 's@# collect_service_tags:.*$@ collect_service_tags: '${KUBERNETES_COLLECT_SERVICE_TAGS}'@' /opt/datadog-agent/agent/conf.d/kubernetes.yaml
147-
fi
148-
149-
# enable event collector
150-
# WARNING: to avoid duplicates, only one agent at a time across the entire cluster should have this feature enabled.
151-
if [[ $KUBERNETES_COLLECT_EVENTS ]]; then
152-
sed -i -e "s@# collect_events: false@ collect_events: true@" /opt/datadog-agent/agent/conf.d/kubernetes.yaml
153-
154-
# enable the namespace regex
155-
if [[ $KUBERNETES_NAMESPACE_NAME_REGEX ]]; then
156-
sed -i -e "s@# namespace_name_regexp:@ namespace_name_regexp: ${KUBERNETES_NAMESPACE_NAME_REGEX}@" /opt/datadog-agent/agent/conf.d/kubernetes.yaml
157-
fi
158-
fi
159-
fi
160-
161-
if [[ $MESOS_MASTER ]]; then
162-
cp /opt/datadog-agent/agent/conf.d/mesos_master.yaml.example /opt/datadog-agent/agent/conf.d/mesos_master.yaml
163-
cp /opt/datadog-agent/agent/conf.d/zk.yaml.example /opt/datadog-agent/agent/conf.d/zk.yaml
164-
165-
sed -i -e "s/localhost/leader.mesos/" /opt/datadog-agent/agent/conf.d/mesos_master.yaml
166-
sed -i -e "s/localhost/leader.mesos/" /opt/datadog-agent/agent/conf.d/zk.yaml
167-
fi
168-
169-
if [[ $MESOS_SLAVE ]]; then
170-
cp /opt/datadog-agent/agent/conf.d/mesos_slave.yaml.example /opt/datadog-agent/agent/conf.d/mesos_slave.yaml
171-
172-
sed -i -e "s/localhost/$HOST/" /opt/datadog-agent/agent/conf.d/mesos_slave.yaml
173-
fi
174-
175-
if [[ $MARATHON_URL ]]; then
176-
cp /opt/datadog-agent/agent/conf.d/marathon.yaml.example /opt/datadog-agent/agent/conf.d/marathon.yaml
177-
sed -i -e "s@# - url: \"https://server:port\"@- url: ${MARATHON_URL}@" /opt/datadog-agent/agent/conf.d/marathon.yaml
178-
fi
6+
/opt/datadog-agent/venv/bin/activate && python /config_builder.py
1797

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

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

18513
##### Starting up #####
18614

187-
export PATH="/opt/datadog-agent/embedded/bin:/opt/datadog-agent/bin:$PATH"
188-
18915
if [[ $DOGSTATSD_ONLY ]]; then
19016
source /opt/datadog-agent/venv/bin/activate && python /opt/datadog-agent/agent/dogstatsd.py
19117
else

0 commit comments

Comments
 (0)