Skip to content

Instantly share code, notes, and snippets.

@firxworx
Created January 5, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firxworx/7739a50f7d38fb3bade3ae4b21b673ea to your computer and use it in GitHub Desktop.
Save firxworx/7739a50f7d38fb3bade3ae4b21b673ea to your computer and use it in GitHub Desktop.
Vagrant shell provisioner (bash) - spin up tomcat7 + mysql, and deploy a java webapp (war file) on a fresh Ubuntu/Debian box
#!/bin/bash -e
# Author: Kevin Firko @firxworx (https://firxworx.com) from Bitcurve Systems (https://bitcurve.com) - 2017
# overview:
# installs and configures tomcat+mysql and then deploys a java webapp (war file) to a bare
# ubuntu 16.04+ (and similar Ubuntu/Debian distros) vagrant box when specified as a shell provisioner
# in a Vagrantfile. loading mysql timezone tables and basic postfix smtp config included.
# this script should also work on other ubuntu versions and debian boxes with minimal modification
# notes:
# * this is intended to quickly spin-up local vagrant dev/demo environments (do not use as-is on a production machine!)
# * script assumes a few dependencies exist in the shared /vagrant/ folder (e.g. the war file to deploy, tomcat-users.xml, etc.)
# * replace all 192.168.x.x and any ports mentioned with the IP address + ports of your vagrant setup
# * this script can be modified to support tomcat 8, 9, etc but they will not work out of the box due to evolving security/config/admin
# * an example of the tomcat-users.xml file for tomcat7 is included in a HEREDOC 'COMMENT' at the end of this script
# set package configuration selections
debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
debconf-set-selections <<< "postfix postfix/mailname string 192.168.x.x"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
# install required packages (note: mailutils includes postfix)
apt-get update -qy
DEBIAN_FRONTEND=noninteractive apt-get install -qy mysql-server default-jdk unzip wget mailutils
# set .my.cnf file for root so password prompt is not required when using `mysql` command as root
mysqluser="root"
mysqlpass="root"
cat > /root/.my.cnf <<EOF
[client]
user=${mysqluser}
password="${mysqlpass}"
EOF
# create application database and add application db user
MYSQL_PWD=root mysql -u root <<< 'create user if not exists `app-db`; set password for `app-db` = password("app-db-password");'
MYSQL_PWD=root mysql -u root <<< 'create database if not exists `app-db`; set password for `app-db` = password("app-db-password"); grant all privileges on `app-db`.* to `app-db` identified by "app-db-password";'
# ensure mysql tz tables loaded
echo "ensure mysql timezones loaded..."
mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
# add tomcat user/group if they don't already exist
echo "add tomcat user if it doesn't exist"
id -u tomcat &>/dev/null || useradd -r tomcat --shell /bin/false
# download tomcat7 from utoronto mirror and create symlink
# note: if you are using this script, check the mirror for the latest packages and watch out for 404's
echo "check for /opt/tomcat-old symlink; download tomcat7 if it doesn't exist"
if [ ! -L /opt/tomcat-old ];
then
echo "downloading tomcat 7 via wget"
cd /opt && (
wget --quiet http://mirror.dsrg.utoronto.ca/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz
tar -zxf apache-tomcat-7.0.82.tar.gz
ln -s apache-tomcat-7.0.82 tomcat-old
chown -hR tomcat: tomcat-old apache-tomcat-7.0.82.tar.gz
)
fi
# setup environment variables required by tomcat7
cat > /etc/environment <<EOF
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
export CATALINA_HOME=/opt/tomcat-old
export CATALINA_BASE=/opt/tomcat-old
export CATALINA_TMPDIR=/opt/tomcat-old/temp
export CLASSPATH=/opt/tomcat-old/bin/bootstrap.jar:/opt/tomcat-old/bin/tomcat-juli.jar
export JAVA_HOME=/usr/lib/jvm/default-java
export JRE_HOME=/usr/lib/jvm/default-java/jre
EOF
# copy dependencies and chmod to ensure desired perms
# note: these are just a few examples, your webapp will have its own dependencies to include!
cp /vagrant/jars/mail.jar /opt/tomcat-old/lib
cp /vagrant/jars/commons*.jar /opt/tomcat-old/lib
cp /vagrant/jars/mysql-connector*.jar /opt/tomcat-old/lib
cp /vagrant/jars/jfreechart*.jar /opt/tomcat-old/lib
chmod 644 /opt/tomcat-old/lib/*
# copy over tomcat-users.xml template to setup 'manager'/'manager' and 'admin'/'admin' web gui
# users using file template. refer to tomcat docs for good examples of this file.
cp /vagrant/conf/tomcat-users.xml /opt/tomcat-old/conf/tomcat-users.xml
# deploy fresh copy of webapp war file - delete old if it exists then bring over fresh copy
rm -rf /opt/tomcat-old/webapps/client-project.war
rm -rf /opt/tomcat-old/webapps/client-project
cp /vagrant/app/client-project.war /opt/tomcat-old/webapps/
chmod 644 /opt/tomcat-old/webapps/client-project.war
# load application database if it doesn't exist
ret=$(mysql -u root -D app-db -s -N -e "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'app-db' AND table_name = 'Users';")
if [ "$ret" -eq "0" ]; then
echo "application database has not been initialized... importing sql files..."
# note: you may have only one sql file, such as from mysqldump; tailor to your needs
mysql -u root app-db < /vagrant/sql/app-db-schema.sql
mysql -u root app-db < /vagrant/sql/app-demo-data.sql
else
echo "application database exists... leaving as-is..."
fi
# change perms on all tomcat folders so it can be run by tomcat user
chown -R tomcat:root $(realpath /opt/tomcat-old)
# run tomcat as tomcat user
sudo -u tomcat -- /opt/tomcat-old/bin/shutdown.sh &>/dev/null || true
sleep 2
sudo -u tomcat -- /opt/tomcat-old/bin/startup.sh
echo '...'
echo 'please wait a few seconds to allow an old tomcat to fully spin up ...'
echo '...'
echo 'http://192.168.x.x:8000/client-project/pub -- credentials: example@example.com/secret-password'
echo 'http://192.168.x.x:8000/client-project/admin/ -- credentials: admin@example.com/secret-password'
echo '...'
echo 'the tomcat manager app is available at: http://192.168.x.x:8000/'
echo '...'
echo 'provisioning complete'
echo '...'
# example tomcat-users.xml file:
<<COMMENT
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="manager" roles="manager-gui" />
<!-- user admin can access both manager and admin section -->
<role rolename="admin-gui" />
<user username="admin" password="admin" roles="manager-gui,admin-gui" />
</tomcat-users>
COMMENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment