"""DataDog Agent entrypoint script. This application provides a way to fill in DataDog Agent configuration files using Jinja templates and provided values in environment variables. After updating the configuration, the application will run DataDog Agent. """ import fnmatch import os import sys import jinja2 template_dir = os.getenv('TEMPLATE_DIR', '/opt/entrypoint/templates.d') config_dir = os.getenv('CONFIG_DIR', '/etc/datadog-agent/conf.d') environment = os.getenv('Environment') if not os.path.isdir(template_dir): print('Error: template directory "{}" not found'.format(template_dir)) sys.exit(1) tpl = jinja2.Environment( loader=jinja2.FileSystemLoader(template_dir), autoescape=jinja2.select_autoescape()) env = os.environ for template_file in os.listdir(template_dir): if not fnmatch.fnmatch(template_file, '*.yaml'): continue template_name, _ = os.path.splitext(template_file) config_path = os.path.join(config_dir, template_name + '.d', 'conf.yaml') print('Processing "{}" template to "{}"'.format( template_file, config_path)) try: template = tpl.get_template(template_file) template.stream(env).dump(config_path) except jinja2.TemplateNotFound as exc: print('Error: can\'t read the template file "{}": {}'.format( template_file, exc)) sys.exit(1) except FileNotFoundError as exc: print('Error: can\'t render the template file "{}": {}'.format( template_file, exc)) sys.exit(1) print('Launching DataDog Agent') sys.stdout.flush() try: os.execl('/init', '/init') except OSError as exc: print("Error: can't launch DataDog agent: {}".format(exc)) sys.exit(1)