Skip to content

Commit 2d589cc

Browse files
committed
✨ Add user-fix script
Makes sure that IF the user decided decided to change the username, contrary to setup guide instructions, stuff will still work because the user name change also gets applied to anything OctoPi specific.
1 parent f5c71dd commit 2d589cc

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

.github/workflows/custopize.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ jobs:
159159
* OctoPi ${{ env.OCTOPI_VERSION }}
160160
* OctoPrint ${{ env.OCTOPRINT_VERSION }}
161161
* Latest kernel & bootloader
162+
* Service to complete username changes on first-boot as needed
162163

163164
Created with [CustoPiZer](https://github.com/OctoPrint/CustoPiZer)
164165

scripts/90-install-user-fix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set -x
2+
set -e
3+
4+
export LC_ALL=C
5+
6+
source /common.sh
7+
install_cleanup_trap
8+
9+
# we need to install virtualenv-tools3, so let's get pip and that
10+
apt install -y python3-pip
11+
sudo -u pi pip3 install --user virtualenv-tools3
12+
13+
cp /files/user-fix /root/bin/user-fix
14+
chmod +x /root/bin/user-fix
15+
16+
cp /files/user-fix.service /etc/systemd/system/user-fix.service
17+
systemctl enable user-fix.service

scripts/files/user-fix

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

scripts/files/user-fix.service

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Ensure that user name changes are applied as needed
3+
4+
DefaultDependencies=no
5+
6+
Before=network-pre.target
7+
Wants=network-pre.target
8+
9+
After=local-fs.target
10+
Wants=local-fs.target
11+
12+
[Service]
13+
Type=oneshot
14+
ExecStart=/root/bin/user-fix
15+
16+
[Install]
17+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)