1
0
silverbullet/docker-entrypoint.sh

30 lines
1.3 KiB
Bash
Raw Permalink Normal View History

2023-11-27 14:44:39 +00:00
#!/bin/bash -e
2023-11-27 14:01:02 +00:00
2023-11-27 14:44:39 +00:00
# Check if UID and GID are passed as environment variables, if not, extract from the space folder owner
2023-11-29 12:47:18 +00:00
if [ -z "$PUID" ] && [ "$UID" == "0" ] ; then
2023-11-27 14:01:02 +00:00
# Get the UID of the folder owner
2023-11-27 14:44:39 +00:00
PUID=$(stat -c "%u" "$SB_FOLDER")
echo "Will run SilverBullet with UID $PUID, inferred from the owner of $SB_FOLDER (set PUID environment variable to override)"
2023-11-27 14:01:02 +00:00
fi
2023-11-27 14:44:39 +00:00
if [ -z "$PGID" ]; then
2023-11-27 14:01:02 +00:00
# Get the GID of the folder owner
2023-11-27 14:44:39 +00:00
PGID=$(stat -c "%g" "$SB_FOLDER")
2023-11-27 14:01:02 +00:00
fi
2023-11-29 12:47:18 +00:00
if [ "$PUID" == "0" ] || [ "$UID" != "0" ]; then
# Will run SilverBullet as default user
2023-11-27 14:01:02 +00:00
deno run -A --unstable /silverbullet.js $@
else
2023-11-27 14:44:39 +00:00
# Create silverbullet user and group ad-hoc mapped to PUID and PGID
getent group $PGID &> /dev/null || groupadd -g $PGID silverbullet
getent passwd $PUID &> /dev/null || useradd -M -u $PUID -g $PGID silverbullet
2023-11-27 14:44:39 +00:00
# And make sure /deno-dir (Deno cache) is accessible
chown -R $PUID:$PGID /deno-dir
args="$@"
# And run via su as requested PUID, usually this will be 'silverbullet' but if a user with this idea already exists, we will use that
USERNAME=$(getent passwd $PUID | cut -d ":" -f 1)
echo "Running SilverBullet as $USERNAME (configured as PUID $PUID and PGID $PGID)"
su $USERNAME -s /bin/bash -c "deno run -A --unstable /silverbullet.js $args"
2023-11-27 14:01:02 +00:00
fi
2023-11-27 14:44:39 +00:00