|
| 1 | +#!/opt/datadog-agent/embedded/bin/python3 |
| 2 | +# Populates a copy of datadog.yaml.example from DD_* environment variables. |
| 3 | +# Invoked by the "config" installp script; see that script for why this is a |
| 4 | +# separate file (AIX sed has no -i, and interpolating env values into an |
| 5 | +# eval'd script is a code-injection risk) and why edits are staged rather |
| 6 | +# than applied to the real datadog.yaml directly (permissions). |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +def env(*names, allow_empty=False): |
| 14 | + # allow_empty=True matches os.LookupEnv semantics (used by LoadProxyFromEnv |
| 15 | + # in pkg/config/setup/config.go): an explicit DD_PROXY_HTTP="" must |
| 16 | + # suppress a lower-priority HTTP_PROXY rather than fall through to it. |
| 17 | + for name in names: |
| 18 | + if allow_empty: |
| 19 | + if name in os.environ: |
| 20 | + return os.environ[name] |
| 21 | + elif os.environ.get(name): |
| 22 | + return os.environ[name] |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +def split_list(value): |
| 27 | + # DD_TAGS and DD_PROXY_NO_PROXY both accept space or comma as separators. |
| 28 | + return [v for v in value.replace(",", " ").split() if v] |
| 29 | + |
| 30 | + |
| 31 | +path = sys.argv[1] |
| 32 | +with open(path) as f: |
| 33 | + lines = f.readlines() |
| 34 | + |
| 35 | +# api_key is the only field with a real, uncommented line in the template; |
| 36 | +# the rest below ship commented out. Duplicate YAML keys resolve last-wins, |
| 37 | +# so it must be edited in place -- appending it instead would get |
| 38 | +# overridden by the template's own empty "api_key:" line. |
| 39 | +api_key = env("DD_API_KEY") |
| 40 | +for i, line in enumerate(lines): |
| 41 | + if line.startswith("api_key:"): |
| 42 | + value_yaml = yaml.safe_dump(api_key, default_flow_style=True).splitlines()[0] |
| 43 | + lines[i] = "api_key: " + value_yaml + "\n" |
| 44 | + break |
| 45 | + |
| 46 | +extra = {} |
| 47 | +if site := env("DD_SITE"): |
| 48 | + extra["site"] = site |
| 49 | +if hostname := env("DD_HOSTNAME"): |
| 50 | + extra["hostname"] = hostname |
| 51 | +if environment := env("DD_ENV"): |
| 52 | + extra["env"] = environment |
| 53 | +if infrastructure_mode := env("DD_INFRASTRUCTURE_MODE"): |
| 54 | + extra["infrastructure_mode"] = infrastructure_mode |
| 55 | +if tags := env("DD_TAGS"): |
| 56 | + extra["tags"] = split_list(tags) |
| 57 | + |
| 58 | +# DD_PROXY_* takes precedence over the generic HTTP_PROXY/HTTPS_PROXY/NO_PROXY |
| 59 | +# vars, matching the Agent's own env var resolution (LoadProxyFromEnv). |
| 60 | +proxy = {} |
| 61 | +if http_proxy := env("DD_PROXY_HTTP", "HTTP_PROXY", "http_proxy", allow_empty=True): |
| 62 | + proxy["http"] = http_proxy |
| 63 | +if https_proxy := env("DD_PROXY_HTTPS", "HTTPS_PROXY", "https_proxy", allow_empty=True): |
| 64 | + proxy["https"] = https_proxy |
| 65 | +if no_proxy := env("DD_PROXY_NO_PROXY", "NO_PROXY", "no_proxy", allow_empty=True): |
| 66 | + # DD_PROXY_NO_PROXY accepts space or comma; generic NO_PROXY is comma-only. |
| 67 | + if "DD_PROXY_NO_PROXY" in os.environ: |
| 68 | + proxy["no_proxy"] = split_list(no_proxy) |
| 69 | + else: |
| 70 | + proxy["no_proxy"] = [p for p in no_proxy.split(",") if p] |
| 71 | +if proxy: |
| 72 | + extra["proxy"] = proxy |
| 73 | + |
| 74 | +with open(path, "w") as f: |
| 75 | + if extra: |
| 76 | + f.write(yaml.safe_dump(extra, default_flow_style=False) + "\n") |
| 77 | + f.writelines(lines) |
0 commit comments