Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

absolute pidfile and logfile fixes #176

Merged
merged 10 commits into from
Apr 1, 2024
6 changes: 3 additions & 3 deletions Dockerfile-bullseye
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,15 @@ EOF
#
FROM with-fhem as with-fhem-bats

ADD https://github.com/bats-core/bats-core.git /tmp/bats
ADD https://github.com/bats-core/bats-core.git#v1.10.0 /tmp/bats
RUN <<EOF
/tmp/bats/install.sh /opt/bats
ln -s /opt/bats/bin/bats /usr/local/bin/bats
rm -r /tmp/bats
EOF

ADD https://github.com/bats-core/bats-support.git#master /opt/bats/test_helper/bats-support
ADD https://github.com/bats-core/bats-assert.git#master /opt/bats/test_helper/bats-assert
ADD https://github.com/bats-core/bats-support.git#v0.3.0 /opt/bats/test_helper/bats-support
ADD https://github.com/bats-core/bats-assert.git#v2.1.0 /opt/bats/test_helper/bats-assert
ADD https://github.com/bats-core/bats-file.git /opt/bats/test_helper/bats-file
ADD https://github.com/grayhemp/bats-mock.git /opt/bats/test_helper/bats-mock

Expand Down
6 changes: 3 additions & 3 deletions Dockerfile-threaded-bullseye
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ EOF
#
FROM with-fhem as with-fhem-bats

ADD https://github.com/bats-core/bats-core.git /tmp/bats
ADD https://github.com/bats-core/bats-core.git#v1.10.0 /tmp/bats
RUN <<EOF
/tmp/bats/install.sh /opt/bats
ln -s /opt/bats/bin/bats /usr/local/bin/bats
rm -r /tmp/bats
EOF

ADD https://github.com/bats-core/bats-support.git#master /opt/bats/test_helper/bats-support
ADD https://github.com/bats-core/bats-assert.git#master /opt/bats/test_helper/bats-assert
ADD https://github.com/bats-core/bats-support.git#v0.3.0 /opt/bats/test_helper/bats-support
ADD https://github.com/bats-core/bats-assert.git#v2.1.0 /opt/bats/test_helper/bats-assert
ADD https://github.com/bats-core/bats-file.git /opt/bats/test_helper/bats-file
ADD https://github.com/grayhemp/bats-mock.git /opt/bats/test_helper/bats-mock

Expand Down
51 changes: 30 additions & 21 deletions src/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set -o pipefail # Distribute an error exit status through the whole pipe

#--- Constants -------------------------------------------------------------------------------------------------------

declare -r FHEM_DIR="/opt/fhem"
declare -r FHEM_DIR="/opt/fhem"
declare -i gEnableDebug=0


Expand All @@ -27,7 +27,6 @@ declare -i gEnableDebug=0

export TZ="${TZ:-Europe/Berlin}"


#--- Environment variables, configurable from outside ----------------------------------------------------------------

declare -ri TIMEOUT_STOPPING=${TIMEOUT_STOPPING:-30}
Expand Down Expand Up @@ -289,17 +288,19 @@ function collectDockerInfo() {
#
function setGlobal_LOGFILE() {
local -r defaultLogfile="./log/fhem-%Y-%m-%d.log"




[ -n "${LOGFILE+x}" ] && { is_absolutePath "$LOGFILE" && return; } # PIDFILE is already an absolute path, skip other modifications
[ -n "${LOGFILE+x}" ] && { LOGFILE=$(prependFhemDirPath "$LOGFILE"); return; } # LOGFILE already set => use this
[ "${CONFIGTYPE}" == "configDB" ] && { LOGFILE=$(prependFhemDirPath "$defaultLogfile"); return; } # config is done inside DB => default

local cfgFile="$(prependFhemDirPath "${CONFIGTYPE}")"
[ -r "$cfgFile" ] || { LOGFILE=$(prependFhemDirPath "$defaultLogfile"); return; } # configfile not readable => default

local cfgLogDef="$(getGlobalAttr "$cfgFile" "logfile" )"
[ -n "$cfgLogDef" ] && { LOGFILE=$(prependFhemDirPath "$cfgLogDef"); return; } # found something in the configfile => use this
[ -n "$cfgLogDef" ] && is_absolutePath "$cfgLogDef" && { LOGFILE=$cfgLogDef; return; } # found absolute path in the configfile => use this
[ -n "$cfgLogDef" ] && { LOGFILE=$(prependFhemDirPath "$cfgLogDef"); return; } # found something in the configfile => use this


LOGFILE=$(prependFhemDirPath "$defaultLogfile")
}
Expand Down Expand Up @@ -351,17 +352,20 @@ function setTelnet_DEFINITION()
#
function setLogfile_DEFINITION()
{

[ "${CONFIGTYPE}" == "configDB" ] && {
echo ' HINT: Make sure to have your FHEM configuration properly prepared for compatibility with this Docker Image _before_ using configDB !';
return;
} # config is done inside DB => abort

local cfgFile="$(prependFhemDirPath "${CONFIGTYPE}")"

## Find Logfile definition
if [ -z "$(grep -P "^define Logfile FileLog" ${FHEM_DIR}/${CONFIGTYPE})" ]; then
echo "define Logfile FileLog ${LOGFILE} Logfile" >> ${FHEM_DIR}/${CONFIGTYPE}
if [ -z "$(grep -P "^define Logfile FileLog" $cfgFile)" ]; then
echo "define Logfile FileLog ${LOGFILE} Logfile" >> $cfgFile
else
local logFile=${LOGFILE#${FHEM_DIR}/}
is_absolutePath "$logFile" || logFile="./$logFile" # add ./ if not an absolute path
sed -i "s,define Logfile FileLog ./log/fhem-\S*\ Logfile$,define Logfile FileLog $logFile Logfile," $cfgFile
fi
sed -i "s,define Logfile FileLog ./log/fhem-\S*\ Logfile$,define Logfile FileLog ${LOGFILE#${FHEM_DIR}/} Logfile," ${FHEM_DIR}/${CONFIGTYPE}
}


Expand All @@ -374,15 +378,18 @@ function setLogfile_DEFINITION()
function setGlobal_PIDFILE() {
local -r defaultPidfile="./log/fhem.pid"

[ -n "${PIDFILE+x}" ] && { is_absolutePath "$PIDFILE" && return; } # PIDFILE is already an absolute path, skip other modifications
[ -n "${PIDFILE+x}" ] && { PIDFILE=$(prependFhemDirPath "$PIDFILE"); return; } # PIDFILE already set => use this
[ -n "${PIDFILE+x}" ] && { is_absolutePath "$PIDFILE" && return; } # PIDFILE is already an absolute path, skip other modifications
[ -n "${PIDFILE+x}" ] && { PIDFILE=$(prependFhemDirPath "$PIDFILE"); return; } # PIDFILE already set => use this
[ "${CONFIGTYPE}" == "configDB" ] && { PIDFILE=$(prependFhemDirPath "$defaultPidfile"); return; } # config is done inside DB => default

local cfgFile="$(prependFhemDirPath "${CONFIGTYPE}")"
[ -r "$cfgFile" ] || { PIDFILE=$(prependFhemDirPath "$defaultPidfile"); return; } # configfile not readable => default


# Todo: Absolute pidfilename check!
local cfgPidDef="$(getGlobalAttr "$cfgFile" "pidfilename" )"
[ -n "$cfgPidDef" ] && { PIDFILE=$(prependFhemDirPath "$cfgPidDef"); return; } # found something in the configfile => use this
[ -n "$cfgPidDef" ] && is_absolutePath "$cfgPidDef" && { PIDFILE=$cfgPidDef; return; } # found absolute path in the configfile => use this
[ -n "$cfgPidDef" ] && { PIDFILE=$(prependFhemDirPath "$cfgPidDef"); return; } # found something in the configfile => use this

PIDFILE=$(prependFhemDirPath "$defaultPidfile")
}
Expand Down Expand Up @@ -488,10 +495,10 @@ function fhemCleanInstall() {
printfInfo "Installing FHEM to ${FHEM_DIR}\n"


shopt -s dotglob nullglob 2>&1>/dev/null
shopt -s nullglob 2>&1>/dev/null

svn co https://svn.fhem.de/fhem/trunk/fhem /usr/src/fhem -q
mv /usr/src/fhem/* ${FHEM_DIR} 2>&1>/dev/null
(LC_ALL=en_US.UTF-8 && svn export https://svn.fhem.de/fhem/trunk/fhem /usr/src/fhem -q)
mv /usr/src/fhem/* "${FHEM_DIR}" # 2>&1>/dev/null
cp -f /fhem/FHEM/99_DockerImageInfo.pm ${FHEM_DIR}/FHEM/


Expand All @@ -506,9 +513,6 @@ function fhemCleanInstall() {
getGlobalAttr "${FHEM_DIR}/fhem.cfg" "commandref" >/dev/null || echo "attr global commandref modular" >> ${FHEM_DIR}/fhem.cfg
getGlobalAttr "${FHEM_DIR}/fhem.cfg" "mseclog" >/dev/null || echo "attr global mseclog 1" >> ${FHEM_DIR}/fhem.cfg

printfInfo " Patching fhem.cfg Logfile configuration\n"
setTelnet_DEFINITION

printfInfo " Adding pre-defined devices to fhem.cfg\n"

cat >> $fhemCfgFile <<- END_OF_INLINE
Expand Down Expand Up @@ -574,7 +578,7 @@ function fhemUpdateInstall() {
# Global vars: FHEM_DIR
#
function initialContainerSetup() {
[ -d "/fhem" ] || return # /fhem signals that the container is brand new. It holds the default installation that is moved later.
[ -d "/fhem" ] || { setGlobal_LOGFILE; return; } # /fhem signals that the container is brand new. It holds the default installation that is moved later.
local -i isFhemCleanInstall
[ -s "${FHEM_DIR}/fhem.pl" ] && isFhemCleanInstall=0 || isFhemCleanInstall=1

Expand All @@ -591,6 +595,10 @@ function initialContainerSetup() {
fhemCleanInstall
fi

printfInfo " Patching fhem.cfg Logfile configuration\n"
setGlobal_LOGFILE
setLogfile_DEFINITION

runScript "/post-init.sh"
runScript "/docker/post-init.sh"

Expand Down Expand Up @@ -976,6 +984,7 @@ if [ "$#" -eq 1 ] && [ "$1" = "start" ]; then

collectDockerInfo


initialContainerSetup
if [ ! -s "${FHEM_DIR}/fhem.pl" ]; then
printfErr "Fatal: Unable to find FHEM installation in ${FHEM_DIR}/fhem.pl\n"
Expand All @@ -985,7 +994,7 @@ if [ "$#" -eq 1 ] && [ "$1" = "start" ]; then
# used by other maintenance scripts (can we get rid of that?)
[ ! -f /image_info.EMPTY ] && touch /image_info.EMPTY

setGlobal_LOGFILE

setGlobal_PIDFILE

prepareFhemUser
Expand Down
92 changes: 92 additions & 0 deletions src/tests/bats/aptInstall.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

#!/usr/bin/env bats

setup() {
load '/opt/bats/test_helper/bats-support/load.bash'
load '/opt/bats/test_helper/bats-assert/load.bash'
load '/opt/bats/test_helper/bats-file/load.bash'
load '/opt/bats/test_helper/bats-mock/load.bash'
}


setup_file() {
export BATS_TEST_TIMEOUT=60
export LOG_FILE="${BATS_SUITE_TMPDIR}/log"

set -a
source /entry.sh
set +a
}

teardown_file() {
sleep 0
}



teardown() {
DEBIAN_FRONTEND=noninteractive apt-get remove dummydroid -y && apt-get autoremove -y # cleanup
}

# bats test_tags=integrationTest
@test "check aptInstall() new package" {
bats_require_minimum_version 1.5.0

gAptUpdateHasRun=0
local installLog=${BATS_TEST_TMPDIR}/aptInstall.log
run -0 aptInstall "test message" ${installLog} dummydroid

cat ${installLog}
assert_file_contains ${installLog} "Get:" grep
assert_file_contains ${installLog} "Fetched " grep
assert_file_contains ${installLog} "Setting up dummydroid" grep
assert_output --partial "test message"

}

# bats test_tags=integrationTest
@test "check aptInstall() already installed package" {
bats_require_minimum_version 1.5.0

gAptUpdateHasRun=0
local installLog=${BATS_TEST_TMPDIR}/aptInstall.log
run -0 aptInstall "test message2" ${installLog} grep
cat ${installLog}
assert_file_contains ${installLog} "grep is already the newest version" grep
assert_output --partial "test message2"
}

# bats test_tags=integrationTest
@test "check aptInstall() twice executed" {
bats_require_minimum_version 1.5.0

# remove package lists
rm -rf /var/lib/apt/lists/*

# First Update
gAptUpdateHasRun=0
export gAptUpdateHasRun
local installLog=${BATS_TEST_TMPDIR}/aptInstall.log
run -0 aptInstall "test message2" ${installLog} dummydroid
cat ${installLog}
assert_file_contains ${installLog} "Get:" grep # Packagelist is downloaded
assert_file_not_contains ${installLog} "Hit:" grep

# Second Update
gAptUpdateHasRun=0
export gAptUpdateHasRun
local installLog=${BATS_TEST_TMPDIR}/aptInstall2.log
run -0 aptInstall "test message3" ${installLog} dummydroid
assert_file_contains ${installLog} "Hit:" grep # Packagelist was already there
assert_file_not_contains ${installLog} "Get:" grep
assert_output --partial "test message3"

# Update is skipped
gAptUpdateHasRun=1
export gAptUpdateHasRun
local installLog=${BATS_TEST_TMPDIR}/aptInstall3.log
run -0 aptInstall "test message4" ${installLog} dummydroid
assert_file_not_contains ${installLog} "Hit:" grep # no update command was run
assert_file_not_contains ${installLog} "Get:" grep # no update command was run
assert_output --partial "test message4"
}
Loading
Loading