docker-nginx-certbot icon indicating copy to clipboard operation
docker-nginx-certbot copied to clipboard

Consider adding Jinja templates

Open dtcooper opened this issue 1 year ago • 0 comments

Would you consider adding Jinja templates to the image? I find them super useful in my projects.

I've created dtcooper/docker-nginx-certbot-jinja which does this (see project's README). I was thinking, since adding the jinjanate command is really trivial (we already have Python + pip in the container images), I'm wondering if we might add my Jinja template processing code here.

They could be applied pretty easily, ie with a script at /docker-entrypoint.d/25-jinja-on-templates.sh as follows:

#!/bin/bash

set -e

ME=$(basename "$0")

entrypoint_log() {
    if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
        echo "$@"
    fi
}

# Adapted from 20-envsubst-on-templates.sh
auto_jinja() {
  local template_dir="${NGINX_JINJA_TEMPLATE_DIR:-/etc/nginx/templates}"
  local suffix="${NGINX_JINJA_TEMPLATE_SUFFIX:-.jinja}"
  local output_dir="${NGINX_JINJA_OUTPUT_DIR:-/etc/nginx/conf.d}"

  local template relative_path output_path subdir
  [ -d "$template_dir" ] || return 0
  if [ ! -w "$output_dir" ]; then
    entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
    return 0
  fi
  find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
    relative_path="${template#$template_dir/}"
    output_path="$output_dir/${relative_path%$suffix}"
    subdir=$(dirname "$relative_path")
    mkdir -p "$output_dir/$subdir"
    entrypoint_log "$ME: Running jinjanate (jinja template) on $template to $output_path"
    jinjanate --quiet --undefined --output-file "$output_path" "$template"
  done
}

auto_jinja

If you like the idea, I can prepare a merge request with tests and can add information about it to the documentation.

Cheers,

David

dtcooper avatar Oct 18 '24 17:10 dtcooper