improve template expansion
This commit is contained in:
parent
e23c5a61a3
commit
c1e2d8d319
@ -1,8 +1,21 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -ef
|
set -f
|
||||||
|
|
||||||
. /image-entry.d/00-common.envsh
|
. /image-entry.d/00-common.envsh
|
||||||
|
|
||||||
|
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
|
||||||
|
|
||||||
|
expand_error() {
|
||||||
|
[ "${expand_error_seen:-}" = 1 ] || log_always 'template expansion has failed'
|
||||||
|
expand_error_seen=1
|
||||||
|
if [ "${NGX_STRICT_LOAD}" = 1 ] ; then
|
||||||
|
t=10
|
||||||
|
log_always "injecting delay for $t seconds"
|
||||||
|
sleep $t
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
dirs='conf mod modules njs site snip'
|
dirs='conf mod modules njs site snip'
|
||||||
[ "${NGX_PROCESS_STATIC}" = 0 ] || dirs="${dirs} static"
|
[ "${NGX_PROCESS_STATIC}" = 0 ] || dirs="${dirs} static"
|
||||||
|
|
||||||
@ -10,8 +23,8 @@ for n in ${dirs} ; do
|
|||||||
merged_dir="${merged_root}/$n"
|
merged_dir="${merged_root}/$n"
|
||||||
[ -d "${merged_dir}" ] || continue
|
[ -d "${merged_dir}" ] || continue
|
||||||
|
|
||||||
expand_dir_envsubst "${merged_dir}/"
|
expand_dir_envsubst "${merged_dir}/" || expand_error
|
||||||
expand_dir_jinja "${merged_dir}/"
|
expand_dir_jinja "${merged_dir}/" || expand_error
|
||||||
done
|
done
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -40,7 +40,7 @@ J2_KWARGS = {}
|
|||||||
|
|
||||||
|
|
||||||
def merge_dict_from_file(filename):
|
def merge_dict_from_file(filename):
|
||||||
if (not filename) or (filename == ''):
|
if (not filename) or (str(filename) == ''):
|
||||||
return False
|
return False
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
return False
|
return False
|
||||||
@ -81,7 +81,7 @@ def render_error(msg, fail=True) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def render_file(file_in, file_out=None, fail=True):
|
def render_file(file_in, file_out=None, fail=True):
|
||||||
if (not file_in) or (file_in == ''):
|
if (not file_in) or (str(file_in) == ''):
|
||||||
return render_error(
|
return render_error(
|
||||||
'argument "file_in" is empty',
|
'argument "file_in" is empty',
|
||||||
fail)
|
fail)
|
||||||
@ -94,24 +94,14 @@ def render_file(file_in, file_out=None, fail=True):
|
|||||||
f'not a file: {file_in}',
|
f'not a file: {file_in}',
|
||||||
fail)
|
fail)
|
||||||
|
|
||||||
if file_out:
|
|
||||||
if str(file_in) == str(file_out):
|
|
||||||
return render_error(
|
|
||||||
f'unable to process template inplace: {file_in}',
|
|
||||||
fail)
|
|
||||||
f_out = file_out
|
f_out = file_out
|
||||||
else:
|
if not f_out:
|
||||||
if not file_in.endswith(J2_SUFFIX):
|
if not file_in.endswith(J2_SUFFIX):
|
||||||
return render_error(
|
return render_error(
|
||||||
f'input file name extension mismatch: {file_in}',
|
f'input file name extension mismatch: {file_in}',
|
||||||
fail)
|
fail)
|
||||||
f_out = os.path.splitext(file_in)[0]
|
f_out = os.path.splitext(file_in)[0]
|
||||||
|
|
||||||
if os.path.exists(f_out):
|
|
||||||
return render_error(
|
|
||||||
f'output file already exists: {f_out}',
|
|
||||||
fail)
|
|
||||||
|
|
||||||
dirs = J2_SEARCH_PATH.copy()
|
dirs = J2_SEARCH_PATH.copy()
|
||||||
for d in [os.path.dirname(file_in), os.getcwd()]:
|
for d in [os.path.dirname(file_in), os.getcwd()]:
|
||||||
if d not in dirs:
|
if d not in dirs:
|
||||||
@ -132,16 +122,29 @@ def render_file(file_in, file_out=None, fail=True):
|
|||||||
j2_template = j2_environ.get_template(file_in)
|
j2_template = j2_environ.get_template(file_in)
|
||||||
j2_stream = j2_template.stream(**J2_KWARGS)
|
j2_stream = j2_template.stream(**J2_KWARGS)
|
||||||
j2_stream.disable_buffering()
|
j2_stream.disable_buffering()
|
||||||
|
|
||||||
|
if os.path.lexists(f_out):
|
||||||
|
if os.path.islink(f_out) or (not os.path.isfile(f_out)):
|
||||||
|
return render_error(
|
||||||
|
f'output file is not safely writable: {f_out}',
|
||||||
|
fail)
|
||||||
|
if os.path.exists(f_out):
|
||||||
|
if os.path.samefile(file_in, f_out):
|
||||||
|
return render_error(
|
||||||
|
f'unable to process template inplace: {file_in}',
|
||||||
|
fail)
|
||||||
j2_stream.dump(f_out, encoding='utf-8')
|
j2_stream.dump(f_out, encoding='utf-8')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
global J2_KWARGS
|
kwa = {}
|
||||||
for m in J2_MODULES:
|
for m in J2_MODULES:
|
||||||
J2_KWARGS[m] = importlib.import_module(m)
|
kwa[m] = importlib.import_module(m)
|
||||||
J2_KWARGS['env'] = os.environ
|
kwa['env'] = os.environ
|
||||||
J2_KWARGS['cfg'] = {}
|
kwa['cfg'] = {}
|
||||||
|
global J2_KWARGS
|
||||||
|
J2_KWARGS = kwa
|
||||||
|
|
||||||
if J2_CONFIG != '':
|
if J2_CONFIG != '':
|
||||||
if os.path.isfile(J2_CONFIG):
|
if os.path.isfile(J2_CONFIG):
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
[ "${IEP_VERBOSE}" = 0 ] || {
|
[ "${IEP_VERBOSE}" = 0 ] || {
|
||||||
echo "Running ${0##*/}:" >&2
|
pfx=
|
||||||
|
[ "${IEP_TRACE}" = 0 ] || pfx="$(date +'%Y-%m-%d %H:%M:%S.%03N %z'): "
|
||||||
|
echo "# ${pfx}${0##*/}:" >&2
|
||||||
printf ' - %s\n' "$@" >&2
|
printf ' - %s\n' "$@" >&2
|
||||||
}
|
}
|
||||||
exec python3 "/usr/local/lib/jinja2/${0##*/}.py" "$@"
|
exec python3 "/usr/local/lib/jinja2/${0##*/}.py" "$@"
|
@ -1,5 +1,7 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
[ "${IEP_VERBOSE}" = 0 ] || {
|
[ "${IEP_VERBOSE}" = 0 ] || {
|
||||||
echo "Running ${0##*/}: $*" >&2
|
pfx=
|
||||||
|
[ "${IEP_TRACE}" = 0 ] || pfx="$(date +'%Y-%m-%d %H:%M:%S.%03N %z'): "
|
||||||
|
echo "# ${pfx}${0##*/}:${*:+ $*}" >&2
|
||||||
}
|
}
|
||||||
exec python3 "/usr/local/lib/jinja2/${0##*/}.py" "$@"
|
exec python3 "/usr/local/lib/jinja2/${0##*/}.py" "$@"
|
Loading…
Reference in New Issue
Block a user