From a747fbe08074e18227c78b03615cdcf1fc318a3f Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Tue, 16 Jul 2024 15:23:17 +0300 Subject: [PATCH] improve jinja2 template for env retention --- angie/conf.dist/core-preserve-env.conf.j2 | 35 +++++++++++++++++-- image-entry.d/01-defaults.envsh | 2 -- image-entry.d/12-core-env.envsh | 34 ------------------ ...ts.envsh => 12-core-worker-defaults.envsh} | 0 4 files changed, 33 insertions(+), 38 deletions(-) delete mode 100755 image-entry.d/12-core-env.envsh rename image-entry.d/{13-core-worker-defaults.envsh => 12-core-worker-defaults.envsh} (100%) diff --git a/angie/conf.dist/core-preserve-env.conf.j2 b/angie/conf.dist/core-preserve-env.conf.j2 index 2f31b0f..f4ff5e5 100644 --- a/angie/conf.dist/core-preserve-env.conf.j2 +++ b/angie/conf.dist/core-preserve-env.conf.j2 @@ -1,3 +1,34 @@ -{% for v in os.getenv('NGX_CORE_ENV', '').split(sep=' ') -%} +{#- prologue -#} +{%- set penv = [] -%} +{%- if cfg.preserve_env -%} + {%- set penv = cfg.preserve_env -%} + {%- if penv is string -%} + {%- set penv = [penv] -%} + {%- elif penv is iterable -%} + {#- {%- set penv = penv -%} -#} + {%- else -%} + {%- set penv = [penv|string()] -%} + {%- endif -%} +{%- endif -%} +{%- set have = namespace() -%} +{%- set have.tz = false -%} +{%- set have.malloc_arena = false -%} +{#- scan -#} +{%- for v in penv -%} + {%- set have.tz = have.tz or re.match('TZ(=|$)', v|string()) -%} + {%- set have.malloc_arena = have.malloc_arena or re.match('MALLOC_ARENA_MAX(=|$)', v|string()) -%} +{%- endfor -%} +{#- main part -#} +{%- if not have.tz -%} +env TZ; +{% endif %} +{%- if not have.malloc_arena -%} +env MALLOC_ARENA_MAX; +{% endif %} +{%- for v in penv -%} + {%- if re.search("(\"|'|\\s)", v|string()) %} +env {{ (v|string()).__repr__() }}; + {%- else %} env {{ v }}; -{% endfor -%} + {%- endif %} +{%- endfor -%} diff --git a/image-entry.d/01-defaults.envsh b/image-entry.d/01-defaults.envsh index cabece1..900a129 100755 --- a/image-entry.d/01-defaults.envsh +++ b/image-entry.d/01-defaults.envsh @@ -14,8 +14,6 @@ NGX_CORE_MODULES="${NGX_CORE_MODULES:-}" NGX_CORE_EVENTS_SNIPPETS="${NGX_CORE_EVENTS_SNIPPETS:-}" NGX_CORE_SNIPPETS="${NGX_CORE_SNIPPETS:-}" -NGX_CORE_ENV="${NGX_CORE_ENV:-}" - NGX_PROCESS_STATIC=$(gobool_to_int "${NGX_PROCESS_STATIC:-0}" 0) set +a diff --git a/image-entry.d/12-core-env.envsh b/image-entry.d/12-core-env.envsh deleted file mode 100755 index 3fa283f..0000000 --- a/image-entry.d/12-core-env.envsh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -if [ -z "${NGX_CORE_ENV:-}" ] ; then - NGX_CORE_ENV='TZ MALLOC_ARENA_MAX' -else - unset __set_f - __set_f= - case "${-}" in - *f* ) __set_f=1 ;; - esac - [ -n "${__set_f}" ] || set -f - - unset __env __have_tz __have_malloc - for __env in ${NGX_CORE_ENV} ; do - case "${__env}" in - TZ | TZ=* ) - __have_tz=1 - ;; - MALLOC_ARENA_MAX | MALLOC_ARENA_MAX=* ) - __have_malloc=1 - ;; - esac - done - unset __env - - [ -n "${__have_malloc}" ] || NGX_CORE_ENV="MALLOC_ARENA_MAX ${NGX_CORE_ENV}" - [ -n "${__have_tz}" ] || NGX_CORE_ENV="TZ ${NGX_CORE_ENV}" - unset __have_tz __have_malloc - - [ -n "${__set_f}" ] || set +f - unset __set_f -fi - -export NGX_CORE_ENV diff --git a/image-entry.d/13-core-worker-defaults.envsh b/image-entry.d/12-core-worker-defaults.envsh similarity index 100% rename from image-entry.d/13-core-worker-defaults.envsh rename to image-entry.d/12-core-worker-defaults.envsh