Skip to content

Instantly share code, notes, and snippets.

@n1chre
Created January 7, 2017 15:20
Show Gist options
  • Save n1chre/09612eafb6e10d006b2be72e531c59d7 to your computer and use it in GitHub Desktop.
Save n1chre/09612eafb6e10d006b2be72e531c59d7 to your computer and use it in GitHub Desktop.
Vagrant provision script: set up Derby database and Tomcat server
#! /usr/bin/env bash
APPNAME=
# tomcat conf
TADM_USER=admin
TADM_PASS=admin
# derby conf
DERBY_DB=/home/vagrant/database
DBNAME=
DBUSER=
DBPASS=
# ===========================================================================
# install java
sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
sudo apt-get install -y oracle-java8-installer
JAVA_=/usr/lib/jvm/java-8-oracle
# install tomcat
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
cd ~
wget http://ftp.carnet.hr/misc/apache/tomcat/tomcat-8/v8.0.35/bin/apache-tomcat-8.0.35.tar.gz
sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
cd /opt/tomcat
sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*
sudo chown -R tomcat work/ temp/ logs/
sudo update-alternatives --config java
sudo tee /etc/init/tomcat.conf > /dev/null << EOF
description "Tomcat Server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
setuid tomcat
setgid tomcat
env JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
env CATALINA_HOME=/opt/tomcat
# Modify these options as needed
env JAVA_OPTS="-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
env CATALINA_OPTS="-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
exec \$CATALINA_HOME/bin/catalina.sh run
# cleanup temp directory after stop
post-stop script
rm -rf \$CATALINA_HOME/temp/*
end script
EOF
sudo initctl reload-configuration
sudo sed -ie "s%</tomcat-users>% <user username=\"$TADM_USER\" password=\"$TADM_PASS\" roles=\"manager-gui,admin-gui\"/>\n&%" /opt/tomcat/conf/tomcat-users.xml
#sudo initctl start tomcat
# install ant and maven
sudo apt-get install -y ant maven
sudo apt-get remove maven2
sudo apt-get update
# configure derby
rm -r ${DERBY_DB}
mkdir -p ${DERBY_DB}
DERBY_HOME=/usr/lib/jvm/java-8-oracle/db
DERBY_RUN=$DERBY_HOME/lib/derbyrun.jar
cat > ${DERBY_DB}/derby.properties << EOF
derby.database.sqlAuthorization=true
derby.connection.requireAuthentication=true
derby.database.defaultConnectionMode=noAccess
derby.database.fullAccessUsers=sa
derby.database.readOnlyAccessUsers=sb
derby.authentication.provider=NATIVE:credentialsDB:LOCAL
derby.authentication.native.passwordLifetimeMillis=157680000000
EOF
# configure database
java -Dderby.system.home=${DERBY_DB} -jar ${DERBY_RUN} server start &
sleep 30 # wait for derby to configure
cat | java -jar $DERBY_RUN ij << EOF
connect 'jdbc:derby://localhost:1527/credentialsDB;user=sa;password=sapwd22;create=true';
disconnect;
connect 'jdbc:derby://localhost:1527/${DBNAME};user=sa;password=sapwd22;create=true';
CALL SYSCS_UTIL.SYSCS_CREATE_USER('${DBUSER}', '${DBPASS}');
CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.fullAccessUsers', '${DBUSER}');
disconnect;
quit;
EOF
mkdir -p /opt/tomcat/conf/Catalina/localhost
sudo tee /opt/tomcat/conf/Catalina/localhost/${APPNAME}.xml > /dev/null << EOF
<Context path="/medic" docBase="/home/vagrant/web/{APPNAME}" />
EOF
sudo initctl start tomcat
sudo initctl restart tomcat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment