Skip to content

Commit

Permalink
entry.sh
Browse files Browse the repository at this point in the history
Updated PrintNewLines:
 - Load lines only once
 - search for extended regex
 - changed calculation what already printed

 Fixed some shellcheck warnings
  • Loading branch information
sidey79 committed May 24, 2023
1 parent 007ca0c commit 2f1b196
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TIMEOUT="${TIMEOUT:-10}"
RESTART="${RESTART:-1}"
TELNETPORT="${TELNETPORT:-7072}"
CONFIGTYPE="${CONFIGTYPE:-"fhem.cfg"}"
DNS=$( cat /etc/resolv.conf | grep -m1 nameserver | sed -e 's/^nameserver[ \t]*//' )
DNS=$(grep -m1 nameserver /etc/resolv.conf | sed -e 's/^nameserver[ \t]*//' )
export DOCKER_GW="${DOCKER_GW:-$(netstat -r -n | grep ^0.0.0.0 | awk '{print $2}')}"
export DOCKER_HOST="${DOCKER_HOST:-${DOCKER_GW}}"
FHEM_UID="${FHEM_UID:-6061}"
Expand Down Expand Up @@ -56,7 +56,7 @@ else
echo 0 > /docker.hostnetwork
export DOCKER_HOSTNETWORK=0
fi
cat /proc/self/cgroup | grep "memory:" | cut -d "/" -f 3 > /docker.container.id
grep "memory:" /proc/self/cgroup | cut -d "/" -f 3 > /docker.container.id
captest --text | grep -P "^Effective:" | cut -d " " -f 2- | sed "s/, /\n/g" | sort | sed ':a;N;$!ba;s/\n/,/g' > /docker.container.cap.e
captest --text | grep -P "^Permitted:" | cut -d " " -f 2- | sed "s/, /\n/g" | sort | sed ':a;N;$!ba;s/\n/,/g' > /docker.container.cap.p
captest --text | grep -P "^Inheritable:" | cut -d " " -f 2- | sed "s/, /\n/g" | sort | sed ':a;N;$!ba;s/\n/,/g' > /docker.container.cap.i
Expand Down Expand Up @@ -448,16 +448,24 @@ chmod 640 ${FHEM_DIR}/.ssh/id_ed25519.pub ${FHEM_DIR}/.ssh/id_rsa.pub
(( i++ ))

# Function to print FHEM log in incremental steps to the docker log.
# ToDo: Increment by 1
[ -s "$( date +"${LOGFILE}" )" ] && OLDLINES=$( wc -l < "$( date +"${LOGFILE}" )" ) || OLDLINES=0
NEWLINES=${OLDLINES}
[ "${OLDLINES}" -gt 0 ] && ((OLDLINES++))

FOUND=false
MAXLINES=0
function PrintNewLines {
if [ -s "$( date +"${LOGFILE}" )" ]; then
NEWLINES=$(wc -l < "$(date +"${LOGFILE}")")
(( OLDLINES <= NEWLINES )) && LINES=$(( NEWLINES - OLDLINES )) || LINES=${NEWLINES}
tail -n "${LINES}" "$(date +"${LOGFILE}")"
[ -n "$1" ] && grep -q "$1" <(tail -n "$LINES" "$(date +"${LOGFILE}")") && FOUND=true || FOUND=false
OLDLINES=${NEWLINES}
LOGFILENAME=$( date +"${LOGFILE}" )
if [ -s "${LOGFILENAME}" ]; then
mapfile -t logArray < <(tail -n "+${OLDLINES}" "${LOGFILENAME}" )
printf '%s\n' "${logArray[@]}"
[ -n "$1" ] && printf '%s\n' "${logArray[@]}" | grep -q -e "$1" && FOUND=true || FOUND=false
if [ ${#logArray[@]} -eq 0 ]; then
MAXLINES=$( wc -l < "${LOGFILENAME}" )
[ ${OLDLINES} -gt ${MAXLINES} ] && OLDLINES=0 # logfile rotation
else
OLDLINES=$((${OLDLINES} + ${#logArray[@]} + 1))
fi
fi
}

Expand All @@ -469,9 +477,10 @@ function StopFHEM {
echo -e 'Waiting for FHEM process to terminate before stopping container:\n'

# Wait for FHEM to complete shutdown
FOUND=false;
until $FOUND; do
sleep $SLEEPINTERVAL
PrintNewLines "Server shutdown"
PrintNewLines "Server shutdown$"
done

# Wait for FHEM normal process exit
Expand Down Expand Up @@ -513,20 +522,20 @@ function StartFHEM {
if [ -s ${FHEM_DIR}/${CONFIGTYPE} ]; then

## Find Telnet access details
if [ -z "$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^define .* telnet ${TELNETPORT}")" ]; then
CUSTOMPORT="$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P '^define .* telnet ' | head -1 | cut -d ' ' -f 4)"
if [ -z "${CUSTOMPORT}"]; then
if [ -z "$(grep -P "^define .* telnet ${TELNETPORT}" ${FHEM_DIR}/${CONFIGTYPE})" ]; then
CUSTOMPORT="$(grep -P '^define .* telnet ' ${FHEM_DIR}/${CONFIGTYPE} | head -1 | cut -d ' ' -f 4)"
if [ -z "${CUSTOMPORT}" ]; then
echo "define telnetPort telnet ${TELNETPORT}" >> ${FHEM_DIR}/${CONFIGTYPE}
else
TELNETPORT=${CUSTOMPORT}
fi
fi
TELNETDEV="$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^define .* telnet ${TELNETPORT}" | head -1 | cut -d " " -f 2)"
TELNETDEV="$(grep -P "^define .* telnet ${TELNETPORT}" ${FHEM_DIR}/${CONFIGTYPE} | head -1 | cut -d " " -f 2)"
TELNETALLOWEDDEV="$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^attr .* validFor .*${TELNETDEV}.*" | head -1 | cut -d " " -f 2)"

## Enforce local telnet access w/o password
if [ -n "$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^attr ${TELNETALLOWEDDEV} password.*")" ]; then
if [ -n "$(cat ${FHEM_DIR}/${CONFIGTYPE} | grep -P "^attr ${TELNETALLOWEDDEV} globalpassword.*")" ]; then
if [ -n "$(grep -P "^attr ${TELNETALLOWEDDEV} password.*" ${FHEM_DIR}/${CONFIGTYPE})" ]; then
if [ -n "$(grep -P "^attr ${TELNETALLOWEDDEV} globalpassword.*" ${FHEM_DIR}/${CONFIGTYPE})" ]; then
echo " - Removed local password from Telnet allowed device '${TELNETALLOWEDDEV}'"
sed -i "/attr ${TELNETALLOWEDDEV} password/d" ${FHEM_DIR}/${CONFIGTYPE}
else
Expand Down Expand Up @@ -592,9 +601,10 @@ function StartFHEM {
fi

# Wait for FHEM to start up
FOUND=false;
until $FOUND; do
sleep $SLEEPINTERVAL
PrintNewLines "Server started"
PrintNewLines "Server started$"
done

if [ -s /post-start.sh ]; then
Expand Down

0 comments on commit 2f1b196

Please sign in to comment.