Puppet3.7.5 Tomcat8 installation automation
Centos7,Ubuntu14.04 Test by puppet automation? framework.
*Todo: separation of env variables to other file.pp
root@puppet:/etc/puppet/modules/tomcat8/manifests# cat init.pp
class tomcat8 {
include tomcat8::account_add
$instance_owner = "tomcat8"
$instance_group = "tomcat8"
$java_opts = "-Djava.awt.headless=true -Xmx128M -XX:+UseConcMarkSweepGC"
#$java_opts = "-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -XX:MaxPermSize=128m -Xms512m -Xmx512m"
$catalina_home = "/usr/local/tomcat8"
$catalina_base = "/usr/local/tomcat8"
$catalina_pid= "/usr/local/tomcat8/tomcat8.pid"
$java_security_manager = "no"
case $::operatingsystem {
'RedHat', 'CentOS', 'Scientific', 'OracleLinux', 'OEL': {
#java install
class { 'java': }
$java_home = "/usr/local/java"
$instance_name = "centos7-tomcat8"
#In case of new config as in tomcat8
include tomcat8::centos7_config
# do something RHEL specific
# execute 'yum update'
#exec { 'yum-update':
# command => '/bin/yum -y update'
#}
include tomcat8::get_tomcat
#running service
exec { 'tomcat8-start':
path => ['/bin','/usr/bin','/usr/sbin','/sbin'],
command => 'systemctl daemon-reload && systemctl start tomcat8 && systemctl enable tomcat8',
}
}
'ubuntu': {
#java install
class { 'java': }
#In case of new config as in tomcat8
include tomcat8::ubuntu_config
#tomcat version and compressed file
$java_home = "/usr/lib/jvm/java-7-oracle"
$default_file = "/etc/default/tomcat8"
$instance_name = "ubuntu-trusty-tomcat8"
include tomcat8::get_tomcat
# running tomcat8
service { 'tomcat8':
ensure => running,
}
}
default: {
# ...
}
}
}
root@puppet:/etc/puppet/modules/tomcat8/manifests# cat get_tomcat.pp
class tomcat8::get_tomcat {
$TOMCAT="apache-tomcat"
$TOMCAT_DIR="tomcat-8"
$TOMCAT_VERSION="8.0.21"
$COMP=".tar.gz"
$INSTALL_DIR="/usr/local/tomcat8"
$POLICY_DIR="$INSTALL_DIR/conf/policy.d"
$instance_owner = "tomcat8"
$instance_group = "tomcat8"
exec { 'wget-tomcat':
path => ['/bin','/usr/bin'],
cwd => '/tmp',
#using mirror
#command => "wget http://mirrors.advancedhosters.com/apache/tomcat/$TOMCAT_DIR/v$TOMCAT_VERSION/bin/$TOMCAT-$TOMCAT_VERSION$COMP",
#using local web server
command => "wget http://10.0.0.100/$TOMCAT-$TOMCAT_VERSION$COMP && tar xvzf $TOMCAT-$TOMCAT_VERSION$COMP",
unless => "ls /tmp/$TOMCAT-$TOMCAT_VERSION",
}
exec { 'tomcat-install':
path => ['/bin','/usr/bin'],
cwd => '/tmp',
command => "mv $TOMCAT-$TOMCAT_VERSION $INSTALL_DIR && mkdir $POLICY_DIR && chown -R $instance_owner:$instance_group $INSTALL_DIR && chmod 0755 $INSTALL_DIR/conf && chown -R root:root $INSTALL_DIR/conf && chown -R root:$instance_group $INSTALL_DIR/conf/* && chmod 644 $INSTALL_DIR/conf/* && chmod 0755 $POLICY_DIR",
unless => "ls $INSTALL_DIR",
require => Exec['wget-tomcat'],
}
}
root@puppet:/etc/puppet/modules/tomcat8/manifests# cat centos7_config.pp
class tomcat8::centos7_config {
file { "/etc/systemd/system/tomcat8.service":
ensure => present,
owner => 'root',
group => 'root',
mode => 0700,
#enable => true,
content => template('tomcat8/centos7-tomcat8.erb'),
}
}
root@puppet:/etc/puppet/modules/tomcat8/manifests# cat account_add.pp
class tomcat8::account_add {
user { "tomcat8":
comment => "tomcat8user",
home => "/usr/local/tomcat8",
managehome => "false",
shell => "/bin/false",
uid => 109,
gid => 118,
}
group { "tomcat8":
gid => 118,
}
}
root@puppet:/etc/puppet/modules/tomcat8/manifests# cat ubuntu_config.pp
class tomcat8::ubuntu_config {
file { "/etc/init.d/tomcat8":
ensure => present,
owner => 'root',
group => 'root',
mode => 0755,
content => template('tomcat8/ubuntu-tomcat8.erb'),
}
file { "/etc/default/tomcat8":
ensure => present,
owner => 'root',
group => 'root',
mode => 0640,
content => template('tomcat8/ubuntu-default-tomcat8.erb'),
}
}
root@puppet:/etc/puppet/modules/tomcat8# cat templates/centos7-tomcat8.erb
[Unit]
Description=Tomcat8
After=network.target
[Service]
Type=forking
User=<%= @instance_owner %>
Group=<%= @instance_group %>
Environment=CATALINA_PID=<%= @catalina_pid %>
Environment=JAVA_HOME=<%= @java_home %>
Environment=TOMCAT_JAVA_HOME=<%= @java_home %>
Environment=CATALINA_HOME=<%= @catalina_home %>
Environment=CATALINA_BASE=<%= @catalina_base %>
Environment=CATALINA_OPTS=
Environment="JAVA_OPTS=<%= @java_opts %>"
ExecStart=<%= @catalina_home %>/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
[Install]
WantedBy=multi-user.target
root@puppet:/etc/puppet/modules/tomcat8# cat templates/ubuntu-default-tomcat8.erb
# File Managed by Puppet
# Run Tomcat as this user ID. Not setting this or leaving it blank will use the
# default of tomcat7.
TOMCAT8_USER=<%= @instance_owner %>
# Run Tomcat as this group ID. Not setting this or leaving it blank will use
# the default of tomcat7.
TOMCAT8_GROUP=<%= @instance_group %>
# The home directory of the Java development kit (JDK). You need at least
# JDK version 1.5. If JAVA_HOME is not set, some common directories for
# OpenJDK, the Sun JDK, and various J2SE 1.5 versions are tried.
<% if @java_home != '' -%>
JAVA_HOME=<%= @java_home %>
<% end -%>
# You may pass JVM startup parameters to Java here. If unset, the default
# options will be: -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC
#
# Use "-XX:+UseConcMarkSweepGC" to enable the CMS garbage collector (improved
# response time). If you use that option and you run Tomcat on a machine with
# exactly one CPU chip that contains one or two cores, you should also add
# the "-XX:+CMSIncrementalMode" option.
<% if @java_opts != '' -%>
JAVA_OPTS="<%= @java_opts %>"
<% end -%>
#<% if @catalina_opts != '' -%>
#CATALINA_OPTS="<%= @catalina_opts %>"
#<% end -%>
# Java compiler to use for translating JavaServer Pages (JSPs). You can use all
# compilers that are accepted by Ant's build.compiler property.
#JSP_COMPILER=javac
# Use the Java security manager? (yes/no, default: no)
#TOMCAT8_SECURITY=no
# Number of days to keep logfiles in /var/log/tomcat7. Default is 14 days.
#LOGFILE_DAYS=14
# Location of the JVM temporary directory
# WARNING: This directory will be destroyed and recreated at every startup !
#JVM_TMP=/tmp/tomcat7-temp
# If you run Tomcat on port numbers that are all higher than 1023, then you
# do not need authbind. It is used for binding Tomcat to lower port numbers.
# NOTE: authbind works only with IPv4. Do not enable it when using IPv6.
# (yes/no, default: no)
#AUTHBIND=no
root@puppet:/etc/puppet/modules/tomcat8# cat templates/ubuntu-default-tomcat8.erb
# File Managed by Puppet
# Run Tomcat as this user ID. Not setting this or leaving it blank will use the
# default of tomcat7.
TOMCAT8_USER=<%= @instance_owner %>
# Run Tomcat as this group ID. Not setting this or leaving it blank will use
# the default of tomcat7.
TOMCAT8_GROUP=<%= @instance_group %>
# The home directory of the Java development kit (JDK). You need at least
# JDK version 1.5. If JAVA_HOME is not set, some common directories for
# OpenJDK, the Sun JDK, and various J2SE 1.5 versions are tried.
<% if @java_home != '' -%>
JAVA_HOME=<%= @java_home %>
<% end -%>
# You may pass JVM startup parameters to Java here. If unset, the default
# options will be: -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC
#
# Use "-XX:+UseConcMarkSweepGC" to enable the CMS garbage collector (improved
# response time). If you use that option and you run Tomcat on a machine with
# exactly one CPU chip that contains one or two cores, you should also add
# the "-XX:+CMSIncrementalMode" option.
<% if @java_opts != '' -%>
JAVA_OPTS="<%= @java_opts %>"
<% end -%>
#<% if @catalina_opts != '' -%>
#CATALINA_OPTS="<%= @catalina_opts %>"
#<% end -%>
# Java compiler to use for translating JavaServer Pages (JSPs). You can use all
# compilers that are accepted by Ant's build.compiler property.
#JSP_COMPILER=javac
# Use the Java security manager? (yes/no, default: no)
#TOMCAT8_SECURITY=no
# Number of days to keep logfiles in /var/log/tomcat7. Default is 14 days.
#LOGFILE_DAYS=14
# Location of the JVM temporary directory
# WARNING: This directory will be destroyed and recreated at every startup !
#JVM_TMP=/tmp/tomcat7-temp
# If you run Tomcat on port numbers that are all higher than 1023, then you
# do not need authbind. It is used for binding Tomcat to lower port numbers.
# NOTE: authbind works only with IPv4. Do not enable it when using IPv6.
# (yes/no, default: no)
#AUTHBIND=no
root@puppet:/etc/puppet/modules/tomcat8# cat templates/ubuntu-
ubuntu-default-tomcat8.erb ubuntu-tomcat8.erb
root@puppet:/etc/puppet/modules/tomcat8# cat templates/ubuntu-tomcat8.erb
#!/bin/sh
# File Managed by Puppet
#
# Modification of https://raw.githubusercontent.com/example42/puppet-tomcat/master/templates/instance/init7-Debian.erb
# /etc/init.d/tomcat8 -- startup script for the Tomcat 8 servlet engine
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
# Modified for Tomcat6 by Thierry Carrez <thierry.carrez@ubuntu.com>.
# Modified for Tomcat7 by Ernesto Hernandez-Novich <emhn@itverx.com.ve>.
# Modified for Tomcat8 by OhYoungJooung <wnapdlf05@gmail.com>.
# Additional improvements by Jason Brittain <jason.brittain@mulesoft.com>.
#
### BEGIN INIT INFO
# Provides: tomcat8-<%= @instance_name %>
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: $named
# Should-Stop: $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Tomcat.
# Description: Start the Tomcat servlet engine.
### END INIT INFO
set -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=`basename $0`
DESC="Tomcat8 servlet engine"
DEFAULT=/etc/default/$NAME
JVM_TMP=/tmp/tomcat8-$NAME-tmp
if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi
# Make sure tomcat is started with system locale
if [ -r /etc/default/locale ]; then
. /etc/default/locale
export LANG
fi
. /lib/lsb/init-functions
if [ -r /etc/default/rcS ]; then
. /etc/default/rcS
fi
# The following variables can be overwritten in $DEFAULT
# Run Tomcat 8 as this user ID and group ID
TOMCAT8_USER=<%= @instance_owner %>
TOMCAT8_GROUP=<%= @instance_group %>
# this is a work-around until there is a suitable runtime replacement
# for dpkg-architecture for arch:all packages
# this function sets the variable OPENJDKS
find_openjdks()
{
for jvmdir in /usr/lib/jvm/java-7-openjdk-*
do
if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-7-openjdk-common" ]
then
OPENJDKS=$jvmdir
fi
done
for jvmdir in /usr/lib/jvm/java-6-openjdk-*
do
if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-6-openjdk-common" ]
then
OPENJDKS="${OPENJDKS} ${jvmdir}"
fi
done
}
OPENJDKS=""
find_openjdks
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-7-oracle"
# Look for the right JVM to use
for jdir in $JDK_DIRS; do
if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
JAVA_HOME="$jdir"
fi
done
export JAVA_HOME
# Directory where the Tomcat 6 binary distribution resides
CATALINA_HOME=<%= @catalina_home %>
# Directory for per-instance configuration files and webapps
CATALINA_BASE=<%= @catalina_base %>
# Use the Java security manager? (yes/no)
TOMCAT8_SECURITY=<%= @java_security_manager %>
# Default Java options
# Set java.awt.headless=true if JAVA_OPTS is not set so the
# Xalan XSL transformer can work without X11 display on JDK 1.4+
# It also looks like the default heap size of 64M is not enough for most cases
# so the maximum heap size is set to 128M
if [ -z "$JAVA_OPTS" ]; then
JAVA_OPTS="<%= @java_opts %>"
fi
# End of variables that can be overwritten in $DEFAULT
# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
. <%= $default_File %>
fi
if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then
log_failure_msg "$NAME is not installed"
exit 1
fi
POLICY_CACHE="$CATALINA_BASE/work/catalina.policy"
if [ -z "$CATALINA_TMPDIR" ]; then
CATALINA_TMPDIR="$JVM_TMP"
fi
# Set the JSP compiler if set in the tomcat8.default file
if [ -n "$JSP_COMPILER" ]; then
JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""
fi
SECURITY=""
if [ "$TOMCAT8_SECURITY" = "yes" ]; then
SECURITY="-security"
fi
# Define other required variables
CATALINA_PID="/var/run/$NAME.pid"
CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"
# Look for Java Secure Sockets Extension (JSSE) JARs
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
JSSE_HOME="${JAVA_HOME}/jre/"
fi
catalina_sh() {
# Escape any double quotes in the value of JAVA_OPTS
JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"
AUTHBIND_COMMAND=""
if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c "
fi
# Define the command to run Tomcat's catalina.sh as a daemon
# set -a tells sh to export assigned variables to spawned shells.
TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; source \"$DEFAULT\"; \
CATALINA_HOME=\"$CATALINA_HOME\"; \
CATALINA_BASE=\"$CATALINA_BASE\"; \
JAVA_OPTS=\"$JAVA_OPTS\"; \
CATALINA_PID=\"$CATALINA_PID\"; \
CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \
LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \
cd \"$CATALINA_BASE\"; \
\"$CATALINA_SH\" $@"
if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
TOMCAT_SH="'$TOMCAT_SH'"
fi
# Run the catalina.sh script as a daemon
set +e
touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
chown $TOMCAT8_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
start-stop-daemon --start -b -u "$TOMCAT8_USER" -g "$TOMCAT8_GROUP" \
-c "$TOMCAT8_USER" -d "$CATALINA_TMPDIR" -p "$CATALINA_PID" \
-x /bin/bash -- -c "$AUTHBIND_COMMAND $TOMCAT_SH"
status="$?"
set +a -e
return $status
}
case "$1" in
start)
if [ -z "$JAVA_HOME" ]; then
log_failure_msg "no JDK found - please set JAVA_HOME"
exit 1
fi
if [ ! -d "$CATALINA_BASE/conf" ]; then
log_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE"
exit 1
fi
log_daemon_msg "Starting $DESC" "$NAME"
if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
--user $TOMCAT8_USER --exec "$JAVA_HOME/bin/java" \
>/dev/null; then
# Regenerate POLICY_CACHE file
umask 022
echo "// AUTO-GENERATED FILE from /etc/tomcat8/policy.d/" \
> "$POLICY_CACHE"
echo "" >> "$POLICY_CACHE"
if [ $(ls -1 $CATALINA_BASE/conf/policy.d/ | grep ".policy$" | wc -l) -ne 0 ]; then
cat $CATALINA_BASE/conf/policy.d/*.policy \
>> "$POLICY_CACHE"
fi
# Remove / recreate JVM_TMP directory
rm -rf "$JVM_TMP"
mkdir -p "$JVM_TMP" || {
log_failure_msg "could not create JVM temporary directory"
exit 1
}
chown $TOMCAT8_USER "$JVM_TMP"
catalina_sh start $SECURITY
sleep 5
if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
--user $TOMCAT8_USER --exec "$JAVA_HOME/bin/java" \
>/dev/null; then
if [ -f "$CATALINA_PID" ]; then
rm -f "$CATALINA_PID"
fi
log_end_msg 1
else
log_end_msg 0
fi
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
set +e
if [ -f "$CATALINA_PID" ]; then
start-stop-daemon --stop --pidfile "$CATALINA_PID" \
--user "$TOMCAT8_USER" \
--retry=TERM/20/KILL/5 >/dev/null
if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $CATALINA_PID`"
log_failure_msg "Failed to stop $NAME (pid $PID)"
exit 1
fi
rm -f "$CATALINA_PID"
rm -rf "$JVM_TMP"
else
log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;
status)
set +e
start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
--user $TOMCAT8_USER --exec "$JAVA_HOME/bin/java" \
>/dev/null 2>&1
if [ "$?" = "0" ]; then
if [ -f "$CATALINA_PID" ]; then
log_success_msg "$DESC is not running, but pid file exists."
exit 1
else
log_success_msg "$DESC is not running."
exit 3
fi
else
log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
fi
set -e
;;
restart|force-reload)
if [ -f "$CATALINA_PID" ]; then
$0 stop
sleep 1
fi
$0 start
;;
try-restart)
if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
--user $TOMCAT8_USER --exec "$JAVA_HOME/bin/java" \
>/dev/null; then
$0 start
fi
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
exit 1
;;
esac
exit 0
Test) on ubuntu14.04
root@ts:~# puppet agent --test
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for ts
Info: Applying configuration version '1428914999'
Notice: /Stage[main]/Tomcat8::Get_tomcat/Exec[wget-tomcat]/returns: executed successfully
Notice: /Stage[main]/Tomcat8::Get_tomcat/Exec[tomcat-install]/returns: executed successfully
Notice: /Stage[main]/Tomcat8::Ubuntu_config/File[/etc/init.d/tomcat8]/ensure: created
Notice: /Stage[main]/Tomcat8/Service[tomcat8]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Tomcat8/Service[tomcat8]: Unscheduling refresh on Service[tomcat8]
Notice: /Stage[main]/Tomcat8::Ubuntu_config/File[/etc/default/tomcat8]/ensure: created
Notice: Finished catalog run in 5.95 seconds
root@ts:~# ps -ef | grep tomcat
tomcat8 2557 1 28 17:50 ? 00:00:02 /usr/lib/jvm/java-7-oracle/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Xmx128M -XX:+UseConcMarkSweepGC -Djava.endorsed.dirs=/usr/local/tomcat8/endorsed -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/tmp/tomcat8-tomcat8-tmp org.apache.catalina.startup.Bootstrap start
Test)on Centos7
[root@ct7 ~]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for ct7
Info: Applying configuration version '1428914350'
Notice: /Stage[main]/Tomcat8::Get_tomcat/Exec[wget-tomcat]/returns: executed successfully
Notice: /Stage[main]/Tomcat8::Get_tomcat/Exec[tomcat-install]/returns: executed successfully
Notice: /Stage[main]/Tomcat8/Exec[tomcat8-start]/returns: executed successfully
Notice: /Stage[main]/Java::Oracle_jdk7/Exec[jdk7-install-to-path]/returns: executed successfully
Notice: Finished catalog run in 3.22 seconds
[root@ct7 ~]# ps -ef | grep tomcat
tomcat8 10140 1 4 08:38 ? 00:00:02 /usr/local/java/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Xmx128M -XX:+UseConcMarkSweepGC -Djava.endorsed.dirs=/usr/local/tomcat8/endorsed -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start
root 10388 6127 0 08:39 pts/0 00:00:00 grep --color=auto tomcat
No comments:
Post a Comment