|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +USERID=1000 |
| 6 | +FIRSTUSER=`getent passwd $USERID | cut -d: -f1` |
| 7 | +FIRSTUSERHOME=`getent passwd $USERID | cut -d: -f6` |
| 8 | + |
| 9 | +CURRENT=`grep User= /etc/systemd/system/octoprint.service | cut -d= -f2` |
| 10 | + |
| 11 | +if [ "$CURRENT" = "pi" -a "$FIRSTUSER" != "pi" ]; then |
| 12 | + # if we get here it means that the first user was renamed but we haven't yet |
| 13 | + # updated all of OctoPi's files that depend on that name, so let's do that now |
| 14 | + |
| 15 | + # first we need to figure out if we can use the new user name in systemd files |
| 16 | + # directly or if we need to use the UID - we do that by checking if the |
| 17 | + # escaped name differes from the plain name, if so something is non ASCII |
| 18 | + # and the UID is the safer bet |
| 19 | + FIRSTUSERESC=`systemd-escape "$FIRSTUSER"` |
| 20 | + if [ "$FIRSTUSER" != "$FIRSTUSERESC" ]; then |
| 21 | + SERVICEUSER=$USERID |
| 22 | + else |
| 23 | + SERVICEUSER=$FIRSTUSER |
| 24 | + fi |
| 25 | + |
| 26 | + # fix OctoPrint service file |
| 27 | + echo "Fixing service file" |
| 28 | + sed -i "s!User=pi!User=$SERVICEUSER!g" /etc/systemd/system/octoprint.service |
| 29 | + sed -i "s!ExecStart=/home/pi/!ExecStart=$FIRSTUSERHOME/!g" /etc/systemd/system/octoprint.service |
| 30 | + systemctl daemon-reload |
| 31 | + |
| 32 | + # fix sudoers files |
| 33 | + echo "Fixing sudoers" |
| 34 | + sed -i "s!^pi!$FIRSTUSER!g" /etc/sudoers.d/octoprint-service |
| 35 | + sed -i "s!^pi!$FIRSTUSER!g" /etc/sudoers.d/octoprint-shutdown |
| 36 | + |
| 37 | + # fix scripts |
| 38 | + echo "Fixing scripts" |
| 39 | + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/scripts/add-octoprint-checkout |
| 40 | + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/scripts/welcome |
| 41 | + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/.bashrc |
| 42 | + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" /root/bin/webcamd |
| 43 | + |
| 44 | + # fix virtualenv |
| 45 | + echo "Fixing paths in virtual environment" |
| 46 | + cd $FIRSTUSERHOME/oprint |
| 47 | + sudo -u $FIRSTUSER $FIRSTUSERHOME/.local/bin/virtualenv-tools --update-path $FIRSTUSERHOME/oprint |
| 48 | + |
| 49 | + # finally, reboot for all of this to actually take affect |
| 50 | + echo "Adjusted scripts to new user, restarting services..." |
| 51 | + systemctl reboot |
| 52 | +fi |
0 commit comments