101 lines
7.9 KiB
Bash
101 lines
7.9 KiB
Bash
#!/bin/bash
|
|
|
|
# EXAMPLES
|
|
usage() {
|
|
echo "usage: " \
|
|
"NAME=app " \
|
|
"CONFIGREPLACEASK=setting.conf params.conf " \
|
|
"[MOREGROUPS=dialout " \
|
|
"USERNAMEGROUP=appuser " \
|
|
"LOGFILE=/var/log/app.log " \
|
|
"DATADIR=/var/lib/data]"
|
|
"sh pre-post-build.sh"
|
|
exit 1
|
|
}
|
|
|
|
echo "DEB Builder pre-post-file: https://git.blubbfish.net/vs_utils/CI-Scripts/src/branch/master/deb/pre-post-build.sh"
|
|
|
|
if [ -z "${NAME}" ]; then
|
|
usage
|
|
fi
|
|
|
|
if [ -z "${CONFIGREPLACEASK}" ]; then
|
|
usage
|
|
fi
|
|
|
|
if [ -z "${USERNAMEGROUP}" ]; then
|
|
USERNAMEGROUP="${NAME}bot"
|
|
fi
|
|
|
|
###################################### POSTINST #################################
|
|
echo "#!/bin/bash" > postinst
|
|
echo "" >> postinst
|
|
echo "if [ \"\$1\" = \"configure\" ]; then" >> postinst
|
|
|
|
# Create User and Group(s) if not exist
|
|
echo " if [ -z \"\`id -u ${USERNAMEGROUP} 2> /dev/null\`\" ]; then" >> postinst
|
|
echo " adduser --system --group --home /nonexistent --gecos \"${NAME} User\" --no-create-home --disabled-password --quiet ${USERNAMEGROUP} || true" >> postinst
|
|
if [ -n "${MOREGROUPS}" ]; then
|
|
echo " usermod -G ${USERNAMEGROUP},adm,${MOREGROUPS} ${USERNAMEGROUP}" >> postinst
|
|
else
|
|
echo " usermod -G ${USERNAMEGROUP},adm ${USERNAMEGROUP}" >> postinst
|
|
fi
|
|
echo " fi" >> postinst
|
|
|
|
# Config replacement via ucf
|
|
echo "" >> postinst
|
|
for conffile in ${CONFIGREPLACEASK} ; do
|
|
echo " ucf --three-way /usr/share/${NAME}/etc/${conffile} /etc/${NAME}/${conffile}" >> postinst
|
|
done
|
|
echo "" >> postinst
|
|
|
|
# Create Logfile
|
|
if [ -n "${LOGFILE}" ]; then
|
|
echo " ucf --three-way /usr/share/${NAME}/logrotate.d/${NAME}.conf /etc/logrotate.d/${NAME}" >> postinst
|
|
echo " chown root:root /etc/logrotate.d/${NAME}" >> postinst
|
|
echo " mkdir -p $(dirname ${LOGFILE})" >> postinst
|
|
echo " touch ${LOGFILE}" >> postinst
|
|
echo " chown ${USERNAMEGROUP}:${USERNAMEGROUP} ${LOGFILE}" >> postinst
|
|
echo " chmod 644 ${LOGFILE}" >> postinst
|
|
echo "" >> postinst
|
|
fi
|
|
|
|
# Set userright to binary folder
|
|
echo " chown -R ${USERNAMEGROUP}:${USERNAMEGROUP} /usr/local/bin/${NAME}" >> postinst
|
|
echo "" >> postinst
|
|
|
|
# Set usserrights to config folder
|
|
echo " if [ -d "/etc/${NAME}/" ]; then" >> postinst
|
|
echo " chown -R ${USERNAMEGROUP}:${USERNAMEGROUP} /etc/${NAME}/" >> postinst
|
|
echo " fi" >> postinst
|
|
echo "" >> postinst
|
|
|
|
# Create datadir
|
|
if [ -n "${DATADIR}" ]; then
|
|
echo " mkdir -p ${DATADIR}" >> postinst
|
|
echo " chown -R ${USERNAMEGROUP}:${USERNAMEGROUP} ${DATADIR}" >> postinst
|
|
echo "" >> postinst
|
|
fi
|
|
|
|
echo "fi" >> postinst
|
|
echo "" >> postinst
|
|
|
|
# Enable service
|
|
echo "systemctl enable ${NAME}" >> postinst
|
|
echo "systemctl daemon-reload" >> postinst
|
|
echo "" >> postinst
|
|
|
|
# Start Service if it was running
|
|
echo "if [ -f /tmp/${NAME}_service_runner ]; then" >> postinst
|
|
echo " service ${NAME} start" >> postinst
|
|
echo " rm /tmp/${NAME}_service_runner" >> postinst
|
|
echo "fi" >> postinst
|
|
|
|
################### PRERM ###################
|
|
echo "#!/bin/bash" > prerm
|
|
echo "" >> prerm
|
|
echo "if [[ \$(systemctl is-active ${NAME} || true) == \"active\" ]]" >> prerm
|
|
echo "then" >> prerm
|
|
echo " touch /tmp/${NAME}_service_runner" >> prerm
|
|
echo " service ${NAME} stop" >> prerm
|
|
echo "fi" >> prerm |