Making debian file for java application.
ㅇ deployed resources
/opt/testApp/bin/testApp.jar
/etc/testApp/testApp.conf
/etc/testApp/log4j.conf
ㅇ Preparation
1. install dpkg-deb
ㅇ install debian packaging
# install macport
https://guide.macports.org/#installing.macports
cd /opt/local/bin
sudo port selfupdate
sudo port install dpkg
brew install dpkg
2. make jar in dist/lib
3. init file in /testApp/etc/init/testApp.conf
ㅇ Make debian for jar
VERSION="0.0.1"
PROJ_NAME="testApp"
#now we build .deb package
mkdir -p builds
rm -rf builds/$VERSION
mkdir -p builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME
mkdir -p builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/bin
mkdir -p builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/log
mkdir -p builds/$VERSION/$PROJ_NAME/DEBIAN/
# debian control file
echo "Package: $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Architecture: amd64" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Maintainer: Dewey Hong" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Priority: optional" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Version: $VERSION" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Description: $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
# debian conffiles file
echo "etc/$PROJ_NAME/$PROJ_NAME.conf" >> builds/$VERSION/$PROJ_NAME/DEBIAN/conffiles
echo "etc/$PROJ_NAME/log4j.conf" >> builds/$VERSION/$PROJ_NAME/DEBIAN/conffiles
# debian preinst file
echo "#!/bin/sh" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "set -e" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "if [ \"\$(pidof $PROJ_NAME)\" ] " >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "then" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo " echo \"stoping $PROJ_NAME \"" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo " stop $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "fi" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
chmod 775 builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
# copy files where they need to be
cp dist/lib/$PROJ_NAME.jar builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/bin/$PROJ_NAME.jar
cp src/main/resources/localhost.conf builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME/$PROJ_NAME.conf
cp src/main/resources/log4j.conf builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME/log4j.conf
# make debian
dpkg-deb --build builds/$VERSION/$PROJ_NAME
DEBIAN=${PROJ_NAME}-${VERSION}_amd64.deb
echo mv builds/$VERSION/$PROJ_NAME.deb builds/$VERSION/$DEBIAN
mv builds/$VERSION/$PROJ_NAME.deb builds/$VERSION/$DEBIAN
= [testApp.conf] =================================================
ㅇ deployed resources
/opt/testApp/bin/testApp.jar
/etc/testApp/testApp.conf
/etc/testApp/log4j.conf
ㅇ Preparation
1. install dpkg-deb
ㅇ install debian packaging
# install macport
https://guide.macports.org/#installing.macports
cd /opt/local/bin
sudo port selfupdate
sudo port install dpkg
brew install dpkg
2. make jar in dist/lib
3. init file in /testApp/etc/init/testApp.conf
ㅇ Make debian for jar
VERSION="0.0.1"
PROJ_NAME="testApp"
#now we build .deb package
mkdir -p builds
rm -rf builds/$VERSION
mkdir -p builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME
mkdir -p builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/bin
mkdir -p builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/log
mkdir -p builds/$VERSION/$PROJ_NAME/DEBIAN/
# debian control file
echo "Package: $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Architecture: amd64" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Maintainer: Dewey Hong" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Priority: optional" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Version: $VERSION" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
echo "Description: $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/control
# debian conffiles file
echo "etc/$PROJ_NAME/$PROJ_NAME.conf" >> builds/$VERSION/$PROJ_NAME/DEBIAN/conffiles
echo "etc/$PROJ_NAME/log4j.conf" >> builds/$VERSION/$PROJ_NAME/DEBIAN/conffiles
# debian preinst file
echo "#!/bin/sh" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "set -e" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "if [ \"\$(pidof $PROJ_NAME)\" ] " >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "then" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo " echo \"stoping $PROJ_NAME \"" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo " stop $PROJ_NAME" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
echo "fi" >> builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
chmod 775 builds/$VERSION/$PROJ_NAME/DEBIAN/preinst
# copy files where they need to be
cp dist/lib/$PROJ_NAME.jar builds/$VERSION/$PROJ_NAME/opt/$PROJ_NAME/bin/$PROJ_NAME.jar
cp src/main/resources/localhost.conf builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME/$PROJ_NAME.conf
cp src/main/resources/log4j.conf builds/$VERSION/$PROJ_NAME/etc/$PROJ_NAME/log4j.conf
# make debian
dpkg-deb --build builds/$VERSION/$PROJ_NAME
DEBIAN=${PROJ_NAME}-${VERSION}_amd64.deb
echo mv builds/$VERSION/$PROJ_NAME.deb builds/$VERSION/$DEBIAN
mv builds/$VERSION/$PROJ_NAME.deb builds/$VERSION/$DEBIAN
= [testApp.conf] =================================================
#!upstart
description "testApp"
author "Dewey Hong"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on runlevel [2345]
stop on runlevel [016]
# Automatically Respawn:
respawn
respawn limit 99 5
#modify limits
limit nofile 64000 64000
env PROJ_NAME=testApp
env JMX_PORT=28001
env JDB_PORT=28002
script
ulimit -n 65000
# Not sure why $HOME is needed, but it is:
export HOME="/root"
export JARFILE="/opt/$PROJ_NAME/bin/$PROJ_NAME.jar"
export IPADDR=`ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'|head -n 1`
# configure the -Dproperties
#JOPTS="-Xmx xxx -Xms=xxx -Xmn xxx"
JOPTS="$JOPTS -server -Djava.net.preferIPv4Stack=true"
JOPTS="$JOPTS -Xmx1G -Xms1G -Xmn128M -XX:+UseParNewGC -XX:ParallelCMSThreads=2 -XX:+UseConcMarkSweepGC -Xloggc:/var/log/testApp/gc.log -XX:+PrintGCDateStamps"
JOPTS="$JOPTS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$JDB_PORT"
JOPTS="$JOPTS -Dsun.jnu.encoding=ISO-8859-1 -Dfile.encoding=ISO-8859-1"
JOPTS="$JOPTS -Djava.rmi.server.hostname=$IPADDR \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=$JMX_PORT \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false"
# configure application apps
JARGS="$JARGS -c /etc/$PROJ_NAME/$PROJ_NAME.conf"
JARGS="$JARGS -l /etc/$PROJ_NAME/log4j.conf"
JLOGFILE=$LOGS_DIR/$PROJ_NAME_error.log
# Override JOPTS with OVERRIDE_JOPTS from commandline while starting this script
# Override JARGS with OVERRIDE_JARGS from commandline while starting this script
set -x
echo "============ restart process !!!! ============" >> /opt/$PROJ_NAME/log/$PROJ_NAME.log
echo java $JOPTS $OVERRIDE_JOPTS -jar $JARFILE $JARGS $OVERRIDE_JARGS >> /opt/$PROJ_NAME/log/service.log
exec java $JOPTS $OVERRIDE_JOPTS -jar $JARFILE $JARGS $OVERRIDE_JARGS > /opt/$PROJ_NAME/log/$PROJ_NAME.log 2>&1
set +x
end script
post-start script
PID=`ps -ef | grep $PROJ_NAME.jar | egrep -v 'grep|nohup' | awk '{print $2}'`
echo $PID > /opt/run/$PROJ_NAME.pid
end script
pre-start script
if [ -f /opt/run/$PROJ_NAME.pid] ; then
rm /opt/run/$PROJ_NAME.pid
fi
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /opt/$PROJ_NAME/log/$PROJ_NAME.log
end script
pre-stop script
if [ -f /opt/run/$PROJ_NAME.pid] ; then
rm /opt/run/$PROJ_NAME.pid
fi
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /opt/$PROJ_NAME/log/$PROJ_NAME.log
end script
Comments
Post a Comment