2020-04-14 20:22:34 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-05-23 15:50:31 +00:00
|
|
|
#####################################################################
|
|
|
|
# Substitutes the given environment variables in the given files.
|
|
|
|
# Parameters:
|
|
|
|
# $1: Comma-delimited list of file paths
|
|
|
|
# $2: Comma-delimited list of environment variables
|
2020-05-24 15:26:20 +00:00
|
|
|
# $3: Absolute path to nginx html directory (optional)
|
2020-05-26 12:28:42 +00:00
|
|
|
# $4: Should restart nginx (optional)
|
2020-05-23 15:50:31 +00:00
|
|
|
#####################################################################
|
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Substituting environment variables...'
|
2020-05-26 12:28:42 +00:00
|
|
|
num_args=0
|
2020-05-17 13:50:00 +00:00
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# The first parameter is a comma-delimited list of paths to files which should be substituted
|
2020-04-14 20:22:34 +00:00
|
|
|
if [[ -z $1 ]]; then
|
2020-05-17 02:59:05 +00:00
|
|
|
echo 'ERROR: No target files given.'
|
2020-04-14 20:22:34 +00:00
|
|
|
exit 1
|
2020-05-26 12:28:42 +00:00
|
|
|
else
|
|
|
|
num_args=1
|
2020-04-14 20:22:34 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-20 02:20:03 +00:00
|
|
|
# The second parameter is a comma-delimited list of environment variable names
|
|
|
|
if [[ -z $2 ]]; then
|
|
|
|
echo 'ERROR: No environment variables given.'
|
|
|
|
exit 1
|
2020-05-26 12:28:42 +00:00
|
|
|
else
|
|
|
|
num_args=2
|
2020-05-20 02:20:03 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-24 15:26:20 +00:00
|
|
|
# The third parameter is the absolute path to the nginx html directory
|
2020-05-26 12:28:42 +00:00
|
|
|
if [[ -z $3 ]]; then
|
|
|
|
echo '' # It's optional. Don't print anything.
|
|
|
|
else
|
|
|
|
num_args=3
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The fourth parameter, if 'true', is whether we should reload nginx
|
|
|
|
if [[ -z $4 ]]; then
|
|
|
|
echo '' # It's optional. Don't print anything.
|
|
|
|
else
|
|
|
|
num_args=4
|
2020-05-24 15:26:20 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-26 14:34:50 +00:00
|
|
|
# Find & replace BASE_HREF in all files in the nginx html directory
|
2020-05-26 18:48:31 +00:00
|
|
|
if [[ "$2" == *"BASE_HREF"* ]] && [[ "$2" == *"DEPLOY_URL"* ]]; then
|
2020-05-26 13:33:49 +00:00
|
|
|
# Add trailing slash to $BASE_HREF if needed
|
2020-05-24 02:54:10 +00:00
|
|
|
length=${#BASE_HREF}
|
|
|
|
last_char=${BASE_HREF:length-1:1}
|
2020-05-25 13:29:21 +00:00
|
|
|
[[ $last_char != "/" ]] && BASE_HREF="$BASE_HREF/"; :
|
2020-05-24 15:26:20 +00:00
|
|
|
|
2020-05-26 18:48:31 +00:00
|
|
|
# Add trailing slash to $DEPLOY_URL if needed
|
|
|
|
length=${#DEPLOY_URL}
|
|
|
|
last_char=${DEPLOY_URL:length-1:1}
|
|
|
|
[[ $last_char != "/" ]] && DEPLOY_URL="$DEPLOY_URL/"; :
|
|
|
|
|
2020-05-24 15:26:20 +00:00
|
|
|
# The third parameter is the absolute path to the nginx html directory
|
2020-05-26 12:28:42 +00:00
|
|
|
if [[ $num_args -ge 3 ]]; then
|
2020-05-24 15:26:20 +00:00
|
|
|
# Replace all instances of __REPLACE_ME_WITH_BASE_HREF__ with $BASE_HREF
|
|
|
|
find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \
|
|
|
|
xargs -0 sed -i 's@__REPLACE_ME_WITH_BASE_HREF__@'"$BASE_HREF"'@g'
|
|
|
|
|
|
|
|
echo 'Replacing base href...'
|
|
|
|
# Wait a few seconds in case find | sed needs more time
|
|
|
|
sleep 3
|
2020-05-26 18:48:31 +00:00
|
|
|
|
|
|
|
# Replace all instances of __REPLACE_ME_WITH_DEPLOY_URL__ with $DEPLOY_URL
|
|
|
|
find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \
|
|
|
|
xargs -0 sed -i 's@__REPLACE_ME_WITH_DEPLOY_URL__@'"$DEPLOY_URL"'@g'
|
|
|
|
|
|
|
|
echo 'Replacing deploy url...'
|
|
|
|
# Wait a few seconds in case find | sed needs more time
|
|
|
|
sleep 3
|
2020-05-24 15:26:20 +00:00
|
|
|
fi
|
2020-05-24 02:54:10 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-20 02:20:03 +00:00
|
|
|
# Convert "VAR1,VAR2,VAR3,..." to "\$VAR1 \$VAR2 \$VAR3 ..."
|
|
|
|
env_list="\\\$${2//,/ \\\$}" # "\" and "$" are escaped as "\\" and "\$"
|
|
|
|
for file_path in ${1//,/ }
|
2020-05-17 02:59:05 +00:00
|
|
|
do
|
2020-05-25 13:29:21 +00:00
|
|
|
echo "replacing environment variables in $file_path"
|
2020-05-17 13:50:00 +00:00
|
|
|
|
2020-05-17 02:59:05 +00:00
|
|
|
# Replace strings in the given file(s) in env_list
|
2020-05-17 13:50:00 +00:00
|
|
|
envsubst "$env_list" < "$file_path" > "$file_path".tmp && mv "$file_path".tmp "$file_path"
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo '...'
|
2020-05-24 03:55:43 +00:00
|
|
|
# Wait a second in case envsubst needs more time
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
# If this is the nginx default.conf file, replace double slashes with single slashes
|
|
|
|
if [[ $file_path == *"/default.conf"* ]]; then
|
|
|
|
sed -i -e 's@//@/@g' "$file_path"
|
|
|
|
fi
|
2020-05-17 02:59:05 +00:00
|
|
|
done
|
2020-04-14 20:22:34 +00:00
|
|
|
|
2020-05-17 13:50:00 +00:00
|
|
|
echo 'Finished substituting environment variables.'
|
2020-05-20 02:20:03 +00:00
|
|
|
for env_var in ${2//,/ }
|
|
|
|
do
|
|
|
|
echo "$env_var = ${!env_var}"
|
|
|
|
done
|
2020-05-24 18:27:06 +00:00
|
|
|
|
2020-05-26 12:28:42 +00:00
|
|
|
# Reload nginx
|
|
|
|
if [ $num_args -ge 4 ] && [ "$4" == "true" ]; then
|
|
|
|
# Check to see if nginx command is available
|
|
|
|
if hash nginx 2> /dev/null; then
|
2020-05-26 13:02:37 +00:00
|
|
|
# Check to see if nginx is already running
|
|
|
|
if [ -e /var/run/nginx.pid ]; then
|
|
|
|
echo "nginx is currently running. Reloading nginx..."
|
|
|
|
exec nginx -s reload
|
|
|
|
echo "nginx reloaded."
|
|
|
|
else
|
|
|
|
echo "nginx is not yet running. Starting nginx..."
|
|
|
|
exec nginx -g 'daemon off;'
|
|
|
|
echo "nginx started."
|
|
|
|
fi
|
2020-05-26 12:28:42 +00:00
|
|
|
else
|
|
|
|
echo "nginx command not found on this system."
|
2020-05-24 18:27:06 +00:00
|
|
|
fi
|
|
|
|
fi
|
2020-05-26 12:28:42 +00:00
|
|
|
|
|
|
|
# Execute all other commands with parameters
|
|
|
|
num_args=$((num_args + 1))
|
|
|
|
exec "${@:num_args}"
|