wmde-rp/wmde-rp-init.sh

162 lines
3.8 KiB
Bash
Raw Normal View History

2025-04-24 19:57:52 +00:00
#!/bin/sh
PREFIX=/usr/local/bin/wmde
. /etc/wmde-rp.conf
#set -x
#set -e
cd /tmp
. $PREFIX/wmde-rp-check-pam-user.sh #exit if user does not belong to rp criteria
#echo "WMDE Roaming Profile"
export HOMEDIR=$(getent passwd "$PAM_USER" | cut -d: -f6)
export UNISON=$HOMEDIR/.unison-wmde
export PAM_USER=$PAM_USER
export SSHARGS="-o UserKnownHostsFile=$HOMEDIR/.wmdesync/known_hosts"
. $PREFIX/wmde-rp-common.sh
2025-05-02 06:59:30 +00:00
TIMEOUT=10
2025-04-24 19:57:52 +00:00
2025-05-01 10:40:53 +00:00
echo "WAIT_FOR_FULLSYNC=$WAIT_FOR_FULLSYNC" >> /tmp/wtforsyc.txt
echo "WAIT_FOR_CONFIGC=$WAIT_FOR_CONFIG" >> /tmp/wtforsyc.txt
2025-04-24 19:57:52 +00:00
2025-05-01 10:40:53 +00:00
if [ "$WAIT_FOR_CONFIG" = "never" ] && [ "$WAIT_FOR_FULLSYNC" = "never" ] ; then
echo "Both, WAIT_FOR_CONFIG and WAIT_FOR_FULLSYNC are never - exit 0" >> /tmp/wtforsyc.txt
exit 0
fi
2025-04-24 19:57:52 +00:00
2025-05-02 06:59:30 +00:00
echo "Loading roaming profile, please be patient ..."
2025-04-24 19:57:52 +00:00
if [ ! -f "$FIRSTSYNC" ]; then
FIRSTRUN="first"
2025-05-01 10:40:53 +00:00
echo "File $FIRSTSYNC does not exists, setting FIRSTRUN=$FIRSTRUN." >> /tmp/wtforsyc.txt
2025-04-24 19:57:52 +00:00
else
FIRSTRUN="no"
2025-05-01 10:40:53 +00:00
echo "File $FIRSTSYNC found, setting FIRSTRUN=$FIRSTRUN." >> /tmp/wtforsyc.txt
2025-04-24 19:57:52 +00:00
fi
# wait for pid file to be created (max 30 seconds)
i=0
while [ $i -lt $TIMEOUT ]; do
2025-05-01 10:40:53 +00:00
if [ -f "$PIDFILE" ]; then
PID=`cat $PIDFILE`
echo "Found pid file $PIDFILEE with content: $PID" >> /tmp/wtforsyc.txt
if kill -0 "$PID" 2>/dev/null; then
break
fi
2025-04-24 19:57:52 +00:00
fi
2025-05-01 10:40:53 +00:00
echo "No pid file $PIDFILE found, waiting sice $seconds" >> /tmp/wtforsyc.txt
sleep 1
i=$((i + 1))
2025-04-24 19:57:52 +00:00
done
if [ $i -eq $TIMEOUT ]; then
2025-05-02 06:59:30 +00:00
echo "Roaming profile failed - timeout."
2025-04-24 19:57:52 +00:00
echo "NO PIDFILE - ERROR" >> /tmp/wtforsyc.txt
2025-05-02 06:59:30 +00:00
exit 0
2025-04-24 19:57:52 +00:00
fi
echo "PID is $PID my PID $$" >> /tmp/wtforsyc.txt
wait_for_sync() {
# $1 dir to watch
# $2 pid_file
while kill -0 "$PID" 2>/dev/null && [ "$PID" != "`cat $2`" ]; do
size=$(du -sh $1 2>/dev/null | cut -f1)
#echo "Loading roaming profile: $size / $xsize"
echo "$3 $size $4"
echo "PID: $PID in file $2: `cat $2`" >> /tmp/wtforsyc.txt
sleep 3
done
echo "FINAL PID: $PID in file $2: `cat $2`" >> /tmp/wtforsyc.txt
# echo "Parameter 1: $1"
# echo "Parameter 2: $2"
}
if [ "$WAIT_FOR_CONFIG" = "always" ] || [ "$WAIT_FOR_CONFIG" = "$FIRSTRUN" ]; then
echo "Wait for Config" >> /tmp/wtforsyc.txt
2025-05-01 10:40:53 +00:00
wait_for_sync $HOMEDIR/ $CONFIGSYNC "Loading config:" ""
2025-04-24 19:57:52 +00:00
fi
if [ "$WAIT_FOR_FULLSYNC" = "always" ] || [ "$WAIT_FOR_FULLSYNC" = "$FIRSTRUN" ]; then
echo "Wait for fullsync" >> /tmp/wtforsyc.txt
i=0
while [ $i -lt $TIMEOUT ]; do
if [ -f "$HOMEDIR/.wmdesync/known_hosts" ]; then
break
fi
sleep 1
i=$((i + 1))
done
if [ $i -eq $TIMEOUT ]; then
xsize="unknown"
else
xsize=$(sudo -E -u $PAM_USER ssh $SSHARGS $NFSSERVER "du -sh $HOMDIR | cut -f1")
fi
wait_for_sync $HOMEDIR/ $FULLSYNC "Loading profile:" "/$xsize"
fi
echo "Finished" >> /tmp/wtforsyc.txt
exit 0
PID=`cat $PIDFILE`
while kill -0 "$PID" 2>/dev/null; do
if [ -f "$FULLSYNC" ]; then
exit 0
fi
size=$(du -sh $HOMEDIR | cut -f1)
echo "Loading roaming profile: $size / $xsize"
sleep 3
done
echo "Failed to initialize roaming profile."
sleep 5
exit 1
if [ ! -f "$HOMEDIR/.wmdesync/known_hosts" ]; then
#sudo -E -u $PAM_USER /bin/sh -c "$PREFIX/wmde-rp-unison-initial-sync-nfs.sh"
sudo -E -u $PAM_USER /bin/sh -c "$PREFIX/wmde-rp-usersync-ssh.sh pam"
LASTERROR=$?
if [ $LASTERROR -ne 0 ]; then
echo "Initial sync with WMDE roaming profile failed. $LASTERROR"
sleep 20
rm -f "$HOMEDIR/.wmdesync/known_hosts"
exit $LASTERROR
fi
fi
#if [ ! -f "$HOMEDIR/.config/systemd/user/default.target.wants/wmde-rp-usersync.service" ]; then
# sudo -E -u $PAM_USER mkdir -p $HOMEDIR/.config/systemd/user/default.target.wants #2>>/tmp/login.txt
# sudo -E -u $PAM_USER ln -s /etc/systemd/user/wmde-rp-usersync.service $HOMEDIR/.config/systemd/user/default.target.wants/wmde-rp-usersync.service #2>>/tmp/login.txt
#fi
#sudo -E -u $PAM_USER systemctl --user start wmde-rp-usersync.service