Skip to content

Commit

Permalink
Allow to change context-root (#507)
Browse files Browse the repository at this point in the history
* allow changing context-root using GEOSERVER_CONTEXT_ROOT variable
  • Loading branch information
tlvu authored Apr 5, 2023
1 parent a542668 commit ad324cc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ GEOWEBCACHE_CACHE_DIR=/opt/geoserver/data_dir/gwc
TOMCAT_EXTRAS=true
# Redirect to GeoServer web interface
ROOT_WEBAPP_REDIRECT=false
# Deploy to another context-root than https://host/geoserver, ex: https://host/my-geoserver
# GEOSERVER_CONTEXT_ROOT=my-geoserver
# For runtime only, do not use at build-time.
GEOSERVER_CONTEXT_ROOT=geoserver
# https://docs.geoserver.org/stable/en/user/production/container.html#optimize-your-jvm
INITIAL_MEMORY=2G
# https://docs.geoserver.org/stable/en/user/production/container.html#optimize-your-jvm
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* [Control flow properties](#control-flow-properties)
* [Changing GeoServer password and username](#changing-geoserver-password-and-username)
* [Docker secrets](#docker-secrets)
* [Changing GeoServer deployment context-root](#changing-geoserver-deployment-context-root)
* [Mounting Configs](#mounting-configs)
* [CORS Support](#cors-support)
* [Clustering using JMS Plugin](#clustering-using-jms-plugin)
Expand Down Expand Up @@ -525,6 +526,20 @@ Currently, the following environment variables
```
are supported.

### Changing GeoServer deployment context-root

You can pass the environment variable to change the context-root at runtime,
example:
```
GEOSERVER_CONTEXT_ROOT=my-geoserver
```

The example above will deploy Geoserver at https://host/my-geoserver instead of
the default location at https://host/geoserver.

This variable is meant for runtime only. At build-time, do not change this
value so at runtime it can perform the proper context-root rename.


## Mounting Configs

Expand Down
14 changes: 13 additions & 1 deletion scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ mkdir -p "${GEOSERVER_DATA_DIR}" "${CERT_DIR}" "${FOOTPRINTS_DATA_DIR}" "${FONT
source /scripts/functions.sh
source /scripts/env-data.sh

# Rename to match wanted context-root and so that we can unzip plugins to
# existing directory.
if [ x"${GEOSERVER_CONTEXT_ROOT}" != xgeoserver ]; then
echo "INFO: changing context-root to '${GEOSERVER_CONTEXT_ROOT}'."
GEOSERVER_INSTALL_DIR="$(detect_install_dir)"
if [ -e "${GEOSERVER_INSTALL_DIR}/webapps/geoserver" ]; then
mv "${GEOSERVER_INSTALL_DIR}/webapps/geoserver" "${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}"
else
echo "WARN: '${GEOSERVER_INSTALL_DIR}/webapps/geoserver' not found, probably already renamed as this is probably a container restart and not first run."
fi
fi

# Credits https://github.com/kartoza/docker-geoserver/pull/371
set_vars
export READONLY CLUSTER_DURABILITY BROKER_URL EMBEDDED_BROKER TOGGLE_MASTER TOGGLE_SLAVE BROKER_URL
Expand Down Expand Up @@ -102,4 +114,4 @@ if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
exec gosu ${USER_NAME} ${GEOSERVER_HOME}/bin/startup.sh
else
exec gosu ${USER_NAME} /usr/local/tomcat/bin/catalina.sh run
fi
fi
7 changes: 6 additions & 1 deletion scripts/env-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,9 @@ fi

if [ -z "${CHOWN_GWC_DATA_DIR}" ]; then
CHOWN_GWC_DATA_DIR=true
fi
fi

if [ -z "${GEOSERVER_CONTEXT_ROOT}" ]; then
# For runtime only, do not change at build-time.
GEOSERVER_CONTEXT_ROOT=geoserver
fi
25 changes: 15 additions & 10 deletions scripts/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ function validate_geo_install() {

}

function detect_install_dir() {
if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
echo "${GEOSERVER_HOME}"
else
echo "${CATALINA_HOME}"
fi
}

function unzip_geoserver() {
if [[ -f /tmp/geoserver/geoserver.war ]]; then
unzip /tmp/geoserver/geoserver.war -d "${CATALINA_HOME}"/webapps/geoserver &&
validate_geo_install "${CATALINA_HOME}"/webapps/geoserver && \
cp -r "${CATALINA_HOME}"/webapps/geoserver/data "${CATALINA_HOME}" &&
unzip /tmp/geoserver/geoserver.war -d "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT} &&
validate_geo_install "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT} && \
cp -r "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/data "${CATALINA_HOME}" &&
mv "${CATALINA_HOME}"/data/security "${CATALINA_HOME}" &&
rm -rf "${CATALINA_HOME}"/webapps/geoserver/data &&
mv "${CATALINA_HOME}"/webapps/geoserver/WEB-INF/lib/postgresql-* "${CATALINA_HOME}"/postgres_config/ &&
rm -rf "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/data &&
mv "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/postgresql-* "${CATALINA_HOME}"/postgres_config/ &&
rm -rf /tmp/geoserver
else
cp -r /tmp/geoserver/* "${GEOSERVER_HOME}"/ && \
Expand Down Expand Up @@ -246,11 +254,8 @@ function install_plugin() {
if [[ -f "${DATA_PATH}"/"${EXT}".zip ]];then
unzip "${DATA_PATH}"/"${EXT}".zip -d /tmp/gs_plugin
echo -e "\e[32m Enabling ${EXT} for GeoServer \033[0m"
if [[ -f /geoserver/start.jar ]]; then
cp -r -u -p /tmp/gs_plugin/*.jar /geoserver/webapps/geoserver/WEB-INF/lib/
else
cp -r -u -p /tmp/gs_plugin/*.jar "${CATALINA_HOME}"/webapps/geoserver/WEB-INF/lib/
fi
GEOSERVER_INSTALL_DIR="$(detect_install_dir)"
cp -r -u -p /tmp/gs_plugin/*.jar ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/
rm -rf /tmp/gs_plugin
else
echo -e "\e[32m ${EXT} extension will not be installed because it is not available \033[0m"
Expand Down
30 changes: 13 additions & 17 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ dpkg -i ${resources_dir}/libjpeg-turbo-official_2.1.4_${system_architecture}.deb
pushd "${CATALINA_HOME}" || exit

# Install GeoServer plugins in correct install dir
if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
GEOSERVER_INSTALL_DIR=${GEOSERVER_HOME}
else
GEOSERVER_INSTALL_DIR=${CATALINA_HOME}
fi
GEOSERVER_INSTALL_DIR="$(detect_install_dir)"

# Install any plugin zip files in resources/plugins
if ls /tmp/resources/plugins/*.zip >/dev/null 2>&1; then
for p in /tmp/resources/plugins/*.zip; do
unzip "$p" -d /tmp/gs_plugin &&
mv /tmp/gs_plugin/*.jar "${GEOSERVER_INSTALL_DIR}"/webapps/geoserver/WEB-INF/lib/ &&
mv /tmp/gs_plugin/*.jar "${GEOSERVER_INSTALL_DIR}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/ &&
rm -rf /tmp/gs_plugin
done
fi
Expand All @@ -115,39 +111,39 @@ fi
#if [[ ${GDAL_VERSION:0:3} == 3.2 ]];then
# echo "gdal versions are the same"
#else
# rm /usr/local/tomcat/webapps/geoserver/WEB-INF/lib/gdal-*
# rm /usr/local/tomcat/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/gdal-*
# validate_url https://repo1.maven.org/maven2/org/gdal/gdal/${GDAL_VERSION:0:3}.0/gdal-${GDAL_VERSION:0:3}.0.jar \
# '-O "${GEOSERVER_HOME}"/webapps/geoserver/WEB-INF/lib/gdal-${GDAL_VERSION:0:3}.0.jar'
# '-O "${GEOSERVER_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/gdal-${GDAL_VERSION:0:3}.0.jar'
#fi


# Install Marlin render https://www.geocat.net/docs/geoserver-enterprise/2020.5/install/production/marlin.html
JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ ${JAVA_VERSION} > 10 ]];then
if [[ -f $(find ${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib -regex ".*marlin-[0-9]\.[0-9]\.[0-9].*jar") ]]; then
mv ${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib/marlin-* ${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib/marlin-render.jar
if [[ -f $(find ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib -regex ".*marlin-[0-9]\.[0-9]\.[0-9].*jar") ]]; then
mv ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/marlin-* ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/marlin-render.jar
fi
else
if [[ -f $(find ${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib -regex ".*marlin-[0-9]\.[0-9]\.[0-9].*jar") ]]; then
rm "${GEOSERVER_INSTALL_DIR}"/webapps/geoserver/WEB-INF/lib/marlin-* \
if [[ -f $(find ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib -regex ".*marlin-[0-9]\.[0-9]\.[0-9].*jar") ]]; then
rm "${GEOSERVER_INSTALL_DIR}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/marlin-* \
validate_url https://github.com/bourgesl/marlin-renderer/releases/download/v0_9_4_2_jdk9/marlin-0.9.4.2-Unsafe-OpenJDK9.jar && \
mv marlin-0.9.4.2-Unsafe-OpenJDK9.jar ${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib/marlin-render.jar
mv marlin-0.9.4.2-Unsafe-OpenJDK9.jar ${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/marlin-render.jar
fi
fi

# Install jetty-servlets
if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
if [[ ! -f ${GEOSERVER_HOME}/webapps/geoserver/WEB-INF/lib/jetty-servlets.jar ]]; then
if [[ ! -f ${GEOSERVER_HOME}/webapps/${GEOSERVER_CONTEXT_ROOT/WEB-INF/lib/jetty-servlets.jar ]]; then
validate_url https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-servlets/11.0.9/jetty-servlets-11.0.9.jar \
'-O "${GEOSERVER_HOME}"/webapps/geoserver/WEB-INF/lib/jetty-servlets.jar'
'-O "${GEOSERVER_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/jetty-servlets.jar'
fi
fi
# Install jetty-util
if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
if [[ ! -f ${GEOSERVER_HOME}/webapps/geoserver/WEB-INF/lib/jetty-util.jar ]]; then
if [[ ! -f ${GEOSERVER_HOME}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/jetty-util.jar ]]; then
validate_url https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-util/11.0.9/jetty-util-11.0.9.jar \
'-O "${GEOSERVER_HOME}"/webapps/geoserver/WEB-INF/lib/jetty-util.jar'
'-O "${GEOSERVER_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/jetty-util.jar'
fi
fi
Expand Down
8 changes: 4 additions & 4 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ if [[ ${POSTGRES_JNDI} =~ [Tt][Rr][Uu][Ee] ]];then
POSTGRES_PORT=5432
export POSTGRES_PORT="${POSTGRES_PORT}"
fi
POSTGRES_JAR_COUNT=$(ls -1 ${CATALINA_HOME}/webapps/geoserver/WEB-INF/lib/postgresql-* 2>/dev/null | wc -l)
POSTGRES_JAR_COUNT=$(ls -1 ${CATALINA_HOME}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/postgresql-* 2>/dev/null | wc -l)
if [ "$POSTGRES_JAR_COUNT" != 0 ]; then
rm "${CATALINA_HOME}"/webapps/geoserver/WEB-INF/lib/postgresql-*
rm "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/postgresql-*
fi
cp "${CATALINA_HOME}"/postgres_config/postgresql-* "${CATALINA_HOME}"/lib/
if [[ -f ${EXTRA_CONFIG_DIR}/context.xml ]]; then
Expand All @@ -214,7 +214,7 @@ if [[ ${POSTGRES_JNDI} =~ [Tt][Rr][Uu][Ee] ]];then
fi

else
cp "${CATALINA_HOME}"/postgres_config/postgresql-* "${CATALINA_HOME}"/webapps/geoserver/WEB-INF/lib/
cp "${CATALINA_HOME}"/postgres_config/postgresql-* "${CATALINA_HOME}"/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/
fi


Expand Down Expand Up @@ -249,7 +249,7 @@ else

if [[ "${ROOT_WEBAPP_REDIRECT}" =~ [Tt][Rr][Uu][Ee] ]]; then
mkdir "${CATALINA_HOME}"/webapps/ROOT
cp /build_data/index.jsp "${CATALINA_HOME}"/webapps/ROOT/index.jsp
cat /build_data/index.jsp | sed "s@/geoserver/@/${GEOSERVER_CONTEXT_ROOT}/@g" > "${CATALINA_HOME}"/webapps/ROOT/index.jsp
fi
fi

Expand Down
8 changes: 2 additions & 6 deletions scripts/update_passwords.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ source /scripts/env-data.sh
source /scripts/functions.sh

# Setup install directory
if [[ -f ${GEOSERVER_HOME}/start.jar ]]; then
GEOSERVER_INSTALL_DIR=${GEOSERVER_HOME}
else
GEOSERVER_INSTALL_DIR=${CATALINA_HOME}
fi
GEOSERVER_INSTALL_DIR="$(detect_install_dir)"



Expand Down Expand Up @@ -47,7 +43,7 @@ if [[ "${USE_DEFAULT_CREDENTIALS}" =~ [Ff][Aa][Ll][Ss][Ee] ]]; then

USERS_XML=${USERS_XML:-${GEOSERVER_DATA_DIR}/security/usergroup/default/users.xml}
ROLES_XML=${ROLES_XML:-${GEOSERVER_DATA_DIR}/security/role/default/roles.xml}
CLASSPATH=${CLASSPATH:-${GEOSERVER_INSTALL_DIR}/webapps/geoserver/WEB-INF/lib/}
CLASSPATH=${CLASSPATH:-${GEOSERVER_INSTALL_DIR}/webapps/${GEOSERVER_CONTEXT_ROOT}/WEB-INF/lib/}

# users.xml setup
cp $USERS_XML $USERS_XML.orig
Expand Down

0 comments on commit ad324cc

Please sign in to comment.