Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alombarte/794fb5db18cb27ebbfca88626f86d5ba to your computer and use it in GitHub Desktop.
Save alombarte/794fb5db18cb27ebbfca88626f86d5ba to your computer and use it in GitHub Desktop.
#!/bin/bash
# Entrypoint with password replacement based on ___PLACEHOLDERS___
# Only works in bash, does not work on sh (e.g.: Alpine)
FILE_TO_REPLACE="krakend.json"
passwords_to_replace="$(grep -o '___.*___' $FILE_TO_REPLACE | sed 's/___//g' | sort | uniq )"
for pass in $passwords_to_replace; do
if [ "x${!pass}" = 'x' ]; then
echo "[KO] Environment variable '$pass' is not declared and the configuration replacement failed!"
else
# Replace the ___PLACEHOLDER___ entry by its value
sed -i "s#___${pass}___#${!pass}#" $FILE_TO_REPLACE
echo "[OK] ${pass} written"
fi
done
# BONUS: Are there remaining placeholders?
PENDING_REPLACEMENTS="$(grep ___ $FILE_TO_REPLACE)"
if [ "x$PENDING_REPLACEMENTS" = 'x' ]; then
echo "[OK] All passwords have been set"
else
echo "[KO] The following passwords weren't retrieved:"
echo "$PENDING_REPLACEMENTS"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment