#!/bin/sh # # $Id$ echo "" echo "$0 helps you to create an init.d script" echo "for a Tomcat9 instance around the jpkg-tomcat9 package." echo "" echo "If you make a minor typo you might prefer fixing the resulting initscript" echo "rather than re-doing the script." echo "" echo "What is the name of your Tomcat9 instance?" echo "Please only use letters, digits, and -." echo "Example: myapp" echo "Example: myappweb" echo "" echo -n "CATALINA_INSTANCE = " read CATALINA_INSTANCE echo "" if [ -z "${CATALINA_INSTANCE}" ] ; then echo "ERROR: Empty input." exit 1 fi echo "You want to name your instance '"${CATALINA_INSTANCE}"'." ; echo "" INITSCRIPT="/etc/init.d/${CATALINA_INSTANCE}" if [ -e ${INITSCRIPT} ] then echo "ERROR: An according init script already exists: ${INSTITSCRIPT}" exit 2 fi echo "What is the CATALINA_BASE directory for ${INSTANCE}?" echo "Default: /var/www/java/${CATALINA_INSTANCE}" echo "Alternate example: /srv/java/${CATALINA_INSTANCE}" echo "" echo -n "CATALINA_BASE = [/var/www/java/${CATALINA_INSTANCE}] " read CATALINA_BASE echo "" if [ -z "${CATALINA_BASE}" ] ; then #echo "ERROR: Empty input." #exit 11 echo "Using default value: /var/www/java/${CATALINA_INSTANCE}" CATALINA_BASE="/var/www/java/${CATALINA_INSTANCE}" echo "" fi echo "DEBUG: CATALINA_BASE=$CATALINA_BASE" ; echo "" if [ ! -d ${CATALINA_BASE} ] then echo "WARNING: CATALINA_BASE does not point to a directory: ${CATALINA_BASE}" #exit 12 echo "Press Ctrl-C to abort or Enter to automatically create the instance there." read ENTER echo "Automatically creating a minimal instance in ${CATALINA_BASE}" mkdir -pv ${CATALINA_BASE}/conf ${CATALINA_BASE}/temp ${CATALINA_BASE}/webapps cp -uva /usr/share/jpkg-tomcat9/apache-tomcat-9/conf/server.xml ${CATALINA_BASE}/conf/ cp -uva /usr/share/jpkg-tomcat9/servlets/helloworldservlet.war ${CATALINA_BASE}/webapps/ROOT.war echo '' >>${CATALINA_BASE}/conf/tomcat_users.xml echo '' >>${CATALINA_BASE}/conf/tomcat_users.xml echo "" fi echo "What is the CATALINA_USER tomcat shall run as?" echo "Default: www-data" echo "Alternate example: myapp" echo "Alternate example: jdoe" echo "" echo -n "CATALINA_USER = [www-data] " read CATALINA_USER echo "" if [ -z "${CATALINA_USER}" ] ; then #echo "ERROR: Empty input." #exit 31 echo "Using default value: /var/www/java/${CATALINA_INSTANCE}" CATALINA_USER="www-data" echo "" fi echo "DEBUG: CATALINA_USER=$CATALINA_USER" ; echo "" if ! /usr/bin/getent passwd ${CATALINA_USER} >/dev/null then echo "ERROR: CATALINA_USER is no known user: ${CATALINA_USER}" exit 32 fi echo "What is the logging directory for ${CATALINA_INSTANCE}?" echo "Default: /var/log/${CATALINA_INSTANCE}" echo "Alternate example: ${CATALINA_BASE}/logs" echo "Alternate example: /var/www/log/${CATALINA_INSTANCE}" echo "Alternate example: /var/lib/myapp/${CATALINA_INSTANCE}" echo "" echo -n "LOGDIR = [/var/log/${CATALINA_INSTANCE}] " read LOGDIR echo "" if [ -z "${LOGDIR}" ] ; then #echo "ERROR: Empty input." #exit 41 echo "Using default value: /var/log/${CATALINA_INSTANCE}" LOGDIR="/var/log/${CATALINA_INSTANCE}" echo "" fi echo "DEBUG: LOGDIR=$LOGDIR" ; echo "" if [ ! -d ${LOGDIR} ] ; then echo "INFO: Logging directory will be created at first start: ${LOGDIR}" ; echo "" #exit 42 fi echo "Which additional directories shall be chown'd to ${CATALINA_USER} for startup?" echo "Example: /var/lib/${CATALINA_INSTANCE} /var/spool/${CATALINA_INSTANCE}" echo "" echo -n "CHOWNDIRS = " read CHOWNDIRS echo "" echo "DEBUG: CHOWNDIRS=$CHOWNDIRS" ; echo "" echo "Do you want a certain JAVA_HOME to be used?" echo "Example: /usr/lib/jvm/java-8-openjdk-amd64" echo "Example: /usr/lib/jvm/java-11-openjdk-amd64" echo "" echo -n "JAVA_HOME = " read JAVA_HOME echo "" echo "DEBUG: JAVA_HOME=$JAVA_HOME" ; echo "" if [ ! -z "$JAVA_HOME" ] ; then if [ ! -x "$JAVA_HOME/bin/java" ] ; then echo "Your JAVA_HOME does not point to a valid Java Installation: $JAVA_HOME" exit 52 else echo "Java version in $JAVA_HOME is:" $JAVA_HOME/bin/java -version echo "" fi fi echo "Do you want special JAVA_OPTS?" echo "Default: -Dfile.encoding=UTF-8 -Xms256m -Xmx1024m" echo "Example part: -Dfile.encoding=ISO-8859-1" echo "Example part: -Djava.awt.headless=true" echo "Enter a space to avoid the default setting." echo "" echo -n "JAVA_OPTS = [-Dfile.encoding=UTF-8 -Xms256m -Xmx1024m] " read JAVA_OPTS echo "" if [ -z "${JAVA_OPTS}" ] ; then echo "Using default value: -Dfile.encoding=UTF-8 -Xms256m -Xmx1024m" JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms256m -Xmx1024m" echo "" else if [ "${JAVA_OPTS}" = " " ] ; then JAVA_OPTS="" fi fi echo "DEBUG: JAVA_OPTS=$JAVA_OPTS" ; echo "" echo "We are done with questions ..." ; echo "" SEDFILE="/tmp/$$.sed" echo "Creating ${SEDFILE} ..." test -f "$SEDFILE" && /bin/rm $SEDFILE echo "s/_CATALINA_INSTANCE_/${CATALINA_INSTANCE}/g" >>$SEDFILE echo -n "s/_CATALINA_BASE_/" >>$SEDFILE echo -n "${CATALINA_BASE}" |sed -e 's/\//\\\//g' >>$SEDFILE echo "/g" >>$SEDFILE echo "s/_CATALINA_USER_/${CATALINA_USER}/g" >>$SEDFILE echo -n "s/_CHOWNDIRS_/" >>$SEDFILE echo -n "${CHOWNDIRS}" |sed -e 's/\//\\\//g' >>$SEDFILE echo "/g" >>$SEDFILE echo -n "s/_LOGDIR_/" >>$SEDFILE echo -n "${LOGDIR}" |sed -e 's/\//\\\//g' >>$SEDFILE echo "/g" >>$SEDFILE echo -n "s/_JAVA_HOME_/" >>$SEDFILE echo -n "${JAVA_HOME}" |sed -e 's/\//\\\//g' >>$SEDFILE echo "/g" >>$SEDFILE echo "s/_JAVA_OPTS_/${JAVA_OPTS}/g" >>$SEDFILE echo "" echo "cat-ing /usr/share/jpkg-tomcat9/init.d/instance.template" echo "through ${SEDFILE} into ${INITSCRIPT}" echo "" # make silent backup for debugging /bin/cp ${SEDFILE} /tmp/jpkg-tomcat9-create-instance.sed /bin/cat /usr/share/jpkg-tomcat9/init.d/instance.template |sed -f ${SEDFILE} >${INITSCRIPT} && /bin/rm ${SEDFILE} if [ -f ${SEDFILE} ] ; then echo "ERROR: The sed operation seems to have failed." exit 61 fi /bin/chmod 0700 ${INITSCRIPT} echo "Successfully created ${INITSCRIPT}." echo "" echo "Suggesting to continue with:" echo "vi ${INITSCRIPT}" if [ -x /bin/systemctl ] ; then echo "systemctl daemon-reload" echo "systemctl enable ${CATALINA_INSTANCE}" else echo "update-rc.d ${CATALINA_INSTANCE} defaults" echo "/etc/init.d/${CATALINA_INSTANCE} start" fi