From 3fcc1ebb9968e4d7680a2f5f5a9b908a42a549f7 Mon Sep 17 00:00:00 2001 From: Joseph Herlant Date: Mon, 31 Jul 2017 17:58:44 -0700 Subject: [PATCH] Add extra config capabilities via a map --- Dockerfile | 1 + alpine/Dockerfile | 1 + alpine/config_helper.py | 38 ++++++++++++++++++++++++++++++++++++++ alpine/entrypoint.sh | 4 ++++ entrypoint.sh | 4 ++++ 5 files changed, 48 insertions(+) create mode 100644 alpine/config_helper.py diff --git a/Dockerfile b/Dockerfile index fb2f0ca0..d1d9d72f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,7 @@ RUN mv /etc/dd-agent/datadog.conf.example /etc/dd-agent/datadog.conf \ # Add Docker check COPY conf.d/docker_daemon.yaml /etc/dd-agent/conf.d/docker_daemon.yaml +COPY alpine/config_helper.py /config_helper.py COPY entrypoint.sh /entrypoint.sh # Extra conf.d and checks.d diff --git a/alpine/Dockerfile b/alpine/Dockerfile index e29fa665..6026d1f7 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -14,6 +14,7 @@ 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 config_helper.py /config_helper.py COPY entrypoint.sh /entrypoint.sh # Expose supervisor and DogStatsD port diff --git a/alpine/config_helper.py b/alpine/config_helper.py new file mode 100644 index 00000000..5e68823e --- /dev/null +++ b/alpine/config_helper.py @@ -0,0 +1,38 @@ +#!/opt/datadog-agent/embedded/bin/python +''' +This script is a helper to build additionnal config file options +''' +from argparse import ArgumentParser +from os import getenv +from json import loads + +def add_extra_datadog_conf(conf_string, conf_file): + ''' + This function takes a json-formatted string as parameter and adds its + content at the end of the datadog.conf file given as parameter. + + The json string defining the additionnal config HAS to be formatted as + follows: + '{"key1": "value1", "key2": "value2"}' + or: '{"histogram_percentiles": "0.75, 0.95, 0.99"}' + ''' + try: + conf_data = loads(conf_string) + with open(conf_file, 'a') as f: + for data in conf_data: + f.write('{}: {}\n'.format(data, conf_data[data])) + except: + pass + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument('-c', '--config-file', + default='/etc/dd-agent/datadog.conf', + help='Full path of the config file to write the config to') + parser.add_argument('-j', '--json-env', + default='DD_EXTRA_CONF', + help='Name of the environment variable to retrieve the json string from') + args = parser.parse_args() + + dd_extra_conf = getenv(args.json_env, '{}') + add_extra_datadog_conf(dd_extra_conf, args.config_file) diff --git a/alpine/entrypoint.sh b/alpine/entrypoint.sh index 0b1f8b04..bccdd050 100755 --- a/alpine/entrypoint.sh +++ b/alpine/entrypoint.sh @@ -115,6 +115,10 @@ 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 +if [[ -n "$DD_EXTRA_CONF" ]]; then + PYTHONPATH=/opt/datadog-agent/agent /opt/datadog-agent/embedded/bin/python /config_helper.py -c /opt/datadog-agent/agent/datadog.conf +fi + ##### Integrations config ##### if [[ -n "${KUBERNETES}" || -n "${MESOS_MASTER}" || -n "${MESOS_SLAVE}" ]]; then diff --git a/entrypoint.sh b/entrypoint.sh index 056dd3ff..6807c392 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -129,6 +129,10 @@ if [[ $SD_BACKEND_PASSWORD ]]; then sed -i -e 's@^# sd_backend_password:.*$@sd_backend_password: '${SD_BACKEND_PASSWORD}'@' /etc/dd-agent/datadog.conf fi +if [[ -n "$DD_EXTRA_CONF" ]]; then + PYTHONPATH=/opt/datadog-agent/agent /opt/datadog-agent/embedded/bin/python /config_helper.py +fi + ##### Integrations config ##### if [[ $KUBERNETES || $MESOS_MASTER || $MESOS_SLAVE ]]; then