#!/bin/bash
# Shell client of Tomcat's Manager App's text interface,
# http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html
#
# If invoked without arguments it drops into an interactive subshell where the commands
# are directly defined and commandline completion of webapp context paths are supported.
#
# REQUIRED: This script requires a tomcat-user with role 'manager-script', e.g.:
#
# $ grep manager-script $CATALINA_HOME/conf/tomcat-users.xml
#
#
#
#
# OPTIONAL: The tomcat manager should not be publicly accessible, so it is recommended
# to restrict its access to localhost only:
#
# $ cat $CATALINA_HOME/conf/Catalina/localhost/manager.xml
#
#
#
#
#
MANAGER_URL="http://localhost:8080/manager/text"
MANAGER_USER=manager-script
MANAGER_PASS=manager-script
CURL="curl -s --user $MANAGER_USER:$MANAGER_PASS"
SCRIPT_NAME=${0##*/}
##
function help {
cat < CONTEXT_PATH=/webapp
undeploy CONTEXT_PATH ...
Shutdown and remove the web application attached to CONTEXT_PATH, and remove
the underlying WAR file or document base directory.
resources
Enumerate the available global JNDI resources.
serverinfo
Display system OS and JVM properties.
expire CONTEXT_PATH [IDLE]
List session idle timeinformation about the web application attached to context
path CONTEXT_PATH. If IDLE is specified, expire sessions for the CONTEXT_PATH
which were idle for at least IDLE minutes.
---
startup
Start Catalina.
debug
Start Catalina under JPDA debugger.
shutdown
Stop Catalina, waiting up to 5 seconds for the process to end.
kill [-SIGNAL]
Kill Catalina with 'kill -TERM' or if -SIGNAL is specified .e.g. 'kill -9'
version
What version of tomcat are you running?
---
help
Print this help.
EOF
}
##
function deploy {
local war path tail
war="$1"
path="$2"
if [ -z "$path" ]
then
tail=${1##*/}
path="/${tail%.war}"
fi
$CURL --upload-file "$war" "$MANAGER_URL/deploy?path=$path"
}
##
function expire {
local path="path=$1"
local idle=
if [ -n "$2" ]
then
local idle="&idle=$2"
fi
$CURL "$MANAGER_URL/expire?$path$idle"
}
##
function func_container {
eval "function $1 { $CURL $MANAGER_URL/$1; }"
export -f $1
}
##
function func_context {
eval "function $1 { for path in "\$@"; do $CURL $MANAGER_URL/$1?path=\$path; done; }"
export -f $1
}
##
function list {
$CURL "$MANAGER_URL/list" | awk -F: '\
NR==1 && /^FAIL/ {print $0; exit}\
NR==2 {printf "%-30s %-7s %10s %-s\n", "context_path", "status ", "sessions", "docbase"}\
NR==2 {printf "%-30s %-7s %10s %-s\n", "------------", "-------", "--------", "-------"}\
NR >1 {printf "%-30s %-7s %10s %-s\n", $1, $2, $3, $4}'
}
##
function contexts {
$CURL "$MANAGER_URL/list" | awk -F: '\
NR==1 && /^FAIL/ {print $0; exit}\
NR >1 {printf "%s ", $1}'
}
##
function func_catalina {
local cmd=$1
shift
eval "function $cmd { \$CATALINA_HOME/bin/catalina.sh "$@" || echo 'The CATALINA_HOME environment variable is not defined correctly'; }"
export -f $cmd
}
##
function kill {
ps -ef | awk '/[o]rg.apache.catalina.startup.Bootstrap/ {print $2}' | xargs kill "$@"
}
##
function contexts_complete {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(contexts)" -- ${cur}) )
}
##
function shell {
export -f contexts contexts_complete list expire deploy help kill
export MANAGER_USER MANAGER_PASS MANAGER_URL CURL SCRIPT_NAME
export PS1="${SCRIPT_NAME}> "
RC_FILE=`mktemp -q /tmp/$SCRIPT_NAME.XXXXXX`
if [ $? -ne 0 ]; then
echo "$0: Can't create temp file, exiting..."
exit 1
fi
trap "rm $RC_FILE" EXIT
# completion is not inherited from the parent bash to the child bash
cat >> $RC_FILE <<<"complete -F contexts_complete expire start stop reload undeploy"
bash --rcfile "$RC_FILE" -i
}
func_container resources
func_container serverinfo
func_context start
func_context stop
func_context reload
func_context undeploy
func_catalina startup start
func_catalina debug jpda start
func_catalina shutdown stop
func_catalina version version
if [ $# -eq 0 ]
then
echo "Type 'help' to show list of commands."
shell
else
eval "$@"
fi
# Copyright (c) 2014, Jess Thrysoee
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.