codepot/docker/httpd-fg.sh

187 lines
5.6 KiB
Bash
Raw Normal View History

2021-08-31 16:13:29 +00:00
#!/bin/bash
set -e
CODEPOT_CONFIG_FILE="/var/lib/codepot/codepot.ini"
HTTPD_CONFIG_FILE="/etc/httpd/conf/httpd.conf"
SERVICE_PORT=""
HIDE_INDEX_PAGE=""
HTTPS_REDIRECTED=""
while getopts ":hp:-:" oc
do
case "${oc}" in
-)
case "${OPTARG}" in
port)
opt=${OPTARG}
SERVICE_PORT="${!OPTIND}"
OPTIND=$(($OPTIND + 1))
;;
port=*)
SERVICE_PORT=${OPTARG#*=}
2022-08-17 01:02:04 +00:00
opt=${OPTARG%=$val}
;;
hide-index-page)
opt=${OPTARG}
HIDE_INDEX_PAGE="${!OPTIND}"
OPTIND=$(($OPTIND + 1))
;;
hide-index-page=*)
HIDE_INDEX_PAGE=${OPTARG#*=}
2022-08-17 01:02:04 +00:00
opt=${OPTARG%=$val}
;;
2021-09-08 14:20:47 +00:00
https-redirected)
opt=${OPTARG}
HTTPS_REDIRECTED="${!OPTIND}"
OPTIND=$(($OPTIND + 1))
;;
https-redirected=*)
HTTPS_REDIRECTED=${OPTARG#*=}
2022-08-17 01:02:04 +00:00
opt=${OPTARG%=$val}
2021-09-08 14:20:47 +00:00
;;
*)
2022-08-17 01:02:04 +00:00
echo "Warning: unknown option - $OPTARG"
;;
esac
;;
h)
2021-09-08 14:20:47 +00:00
echo "-------------------------------------------------------------------------"
echo "This container runs a http service on port 80."
echo "Use an external reverse proxy to enable https as it doesn't"
echo "enable the HTTP service."
echo "Extra options allowed when running the container: "
2021-09-08 14:20:47 +00:00
echo " -h print this help message"
echo " -p number specify the port number"
echo " -port number specify the port number"
echo " -hide-index-page yes/no hide/show the index page script from the URL"
2021-09-08 14:20:47 +00:00
echo " -https-redirected yes/no indicate if the requets are HTTPS redirected"
echo "-------------------------------------------------------------------------"
2022-08-17 01:02:04 +00:00
exit 0
;;
p)
SERVICE_PORT=${OPTARG#*=}
opt=${OPTARG%=$val}
;;
*)
2022-08-17 01:02:04 +00:00
echo "Warning: unknown option - $OPTARG"
;;
esac
done
2021-09-08 14:20:47 +00:00
## fall back to default values if the given values are not proper
echo "${SERVICE_PORT}" | grep -q -E '^[[:digit:]]+$' || SERVICE_PORT="80"
[[ "${HIDE_INDEX_PAGE}" == "" ]] && HIDE_INDEX_PAGE="no"
2021-09-08 14:20:47 +00:00
[[ "${HTTPS_REDIRECTED}" == "" ]] && HTTPS_REDIRECTED="no"
2021-08-31 16:13:29 +00:00
# Note: we don't just use "apache2ctl" here because it itself is just a shell-script wrapper around apache2 which provides extra functionality like "apache2ctl start" for launching apache2 in the background.
# (also, when run as "apache2ctl <apache args>", it does not use "exec", which leaves an undesirable resident shell process)
: "${APACHE_CONFDIR:=/etc/httpd}"
: "${APACHE_ENVVARS:=$APACHE_CONFDIR/envvars}"
if test -f "$APACHE_ENVVARS"; then
. "$APACHE_ENVVARS"
fi
# Apache gets grumpy about PID files pre-existing
: "${APACHE_RUN_DIR:=/var/run/httpd}"
: "${APACHE_PID_FILE:=$APACHE_RUN_DIR/httpd.pid}"
rm -f "$APACHE_PID_FILE"
# create missing directories
# (especially APACHE_RUN_DIR, APACHE_LOCK_DIR, and APACHE_LOG_DIR)
for e in "${!APACHE_@}"; do
if [[ "$e" == *_DIR ]] && [[ "${!e}" == /* ]]; then
# handle "/var/lock" being a symlink to "/run/lock", but "/run/lock" not existing beforehand, so "/var/lock/something" fails to mkdir
# mkdir: cannot create directory '/var/lock': File exists
dir="${!e}"
while [ "$dir" != "$(dirname "$dir")" ]; do
dir="$(dirname "$dir")"
if [ -d "$dir" ]; then
break
fi
absDir="$(readlink -f "$dir" 2>/dev/null || :)"
if [ -n "$absDir" ]; then
mkdir -p "$absDir"
fi
done
mkdir -p "${!e}"
fi
done
chown apache:apache /var/lib/codepot
for i in /var/cache/codepot /var/log/codepot \
/var/lib/codepot/attachments \
/var/lib/codepot/files \
/var/lib/codepot/issuefiles \
/var/lib/codepot/svnrepo \
/var/lib/codepot/usericons
do
[ ! -d "$i" ] && {
mkdir -p "$i"
chown apache:apache "$i"
}
done
2021-09-07 09:36:56 +00:00
[ ! -f /var/lib/codepot/codepot.db ] && {
sqlite3 -init /etc/codepot/codepot.sqlite /var/lib/codepot/codepot.db ""
chown apache:apache /var/lib/codepot/codepot.db
}
2021-09-07 09:36:56 +00:00
[ ! -f "${CODEPOT_CONFIG_FILE}" ] && {
cp -pf /etc/codepot/codepot.ini "${CODEPOT_CONFIG_FILE}"
chown apache:apache "${CODEPOT_CONFIG_FILE}"
}
2021-09-07 09:36:56 +00:00
grep -F -q 'env[CODEPOT_CONFIG_FILE]' /etc/php-fpm.d/www.conf || {
echo "env[CODEPOT_CONFIG_FILE] = ${CODEPOT_CONFIG_FILE}" >> /etc/php-fpm.d/www.conf
}
2021-09-07 09:36:56 +00:00
sed -r -i "s|PerlSetEnv CODEPOT_CONFIG_FILE .*\$|PerlSetEnv CODEPOT_CONFIG_FILE ${CODEPOT_CONFIG_FILE}|g" /etc/httpd/conf.d/codepot.conf
## change the port number as specified on the command line
echo "Configuring to listen on the port[$SERVICE_PORT] hide-index-page[$HIDE_INDEX_PAGE] https-redirected[$HTTPS_REDIRECTED]"
sed -r -i "s|^Listen[[:space:]]+.*|Listen ${SERVICE_PORT}|g" "${HTTPD_CONFIG_FILE}"
2021-09-08 14:20:47 +00:00
if [[ "${HTTPS_REDIRECTED}" =~ [Yy][Ee][Ss] ]]
then
## The DAV COPY request contains the header 'Destination: https://' if the origin request
## is HTTPS. This container is configured to server on HTTP only. If HTTPS is redirected
## to HTTP, we must translate https:// to http:// in the Destination header.
## Otherwise, the response is 502 Bad Gateway.
echo "RequestHeader edit Destination ^https: http: early" > /etc/httpd/conf.d/codepot-dav-https-redirected.conf
else
rm -f /etc/httpd/conf.d/codepot-dav-https-redirected.conf
fi
if [[ "${HIDE_INDEX_PAGE}" =~ [Yy][Ee][Ss] ]]
then
sed -r -i 's|^index_page[[:space:]]*=.*$|index_page=""|g' "${CODEPOT_CONFIG_FILE}"
2021-09-08 16:15:07 +00:00
echo 'RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
2021-09-08 16:15:07 +00:00
RewriteRule ^(.*)$ index.php/$1 [L]' > /var/www/html/.htaccess
sed -r -i '/<Directory "\/var\/www\/html">/,/<\/Directory>/s|^[[:space:]]*AllowOverride[[:space:]]+.*$| AllowOverride All|g' "${HTTPD_CONFIG_FILE}"
else
sed -r -i 's|^index_page[[:space:]]*=.*$|index_page="index.php"|g' "${CODEPOT_CONFIG_FILE}"
rm -rf /var/www/html/.htaccess
sed -r -i '/<Directory "\/var\/www\/html">/,/<\/Directory>/s|^[[:space:]]*AllowOverride[[:space:]]+.*$| AllowOverride None|g' "${HTTPD_CONFIG_FILE}"
fi
2021-08-31 16:13:29 +00:00
php-fpm
exec httpd -DFOREGROUND