Skip to content

Commit

Permalink
[Feature] Allow existing docker user #223 (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
GioF71 committed Jul 25, 2023
1 parent 4080417 commit e95c129
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 65 deletions.
81 changes: 44 additions & 37 deletions app/bin/run-squeezelite-alsa.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,22 @@ source logging.sh
add_log_categories

USE_USER_MODE="N"
DEFAULT_UID=1000
DEFAULT_GID=1000

DEFAULT_USER_NAME=sq-user
DEFAULT_GROUP_NAME=sq-user
DEFAULT_HOME_DIR=/home/$DEFAULT_USER_NAME

USER_NAME=$DEFAULT_USER_NAME
GROUP_NAME=$DEFAULT_GROUP_NAME
HOME_DIR=$DEFAULT_HOME_DIR

## User mode support
if [[ "${USER_MODE^^}" == "YES" || "${USER_MODE^^}" == "Y" ]]; then
USE_USER_MODE="Y"
echo "User mode enabled"
echo "Creating user ...";
DEFAULT_UID=1000
DEFAULT_GID=1000
if [ -z "${PUID}" ]; then
PUID=$DEFAULT_UID;
echo "Setting default value for PUID: ["$PUID"]"
Expand All @@ -249,49 +257,48 @@ if [[ "${USER_MODE^^}" == "YES" || "${USER_MODE^^}" == "Y" ]]; then
PGID=$DEFAULT_GID;
echo "Setting default value for PGID: ["$PGID"]"
fi
USER_NAME=sq-user
GROUP_NAME=sq-user
HOME_DIR=/home/$USER_NAME
### create home directory and ancillary directories
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
chown -R $PUID:$PGID $HOME_DIR
ls -la $HOME_DIR -d
ls -la $HOME_DIR
fi
### create group
if [ ! $(getent group $GROUP_NAME) ]; then
echo "group $GROUP_NAME does not exist, creating..."
echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
### create group if it does not exist
if [ ! $(getent group $PGID) ]; then
echo "Group with gid [$PGID] does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
echo "Group [$GROUP_NAME] with gid [$PGID] created."
else
echo "group $GROUP_NAME already exists."
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
fi
### create user
if [ ! $(getent passwd $USER_NAME) ]; then
echo "user $USER_NAME does not exist, creating..."
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
id $USER_NAME
echo "user $USER_NAME created."
### create user if it does not exist
if [ ! $(getent passwd $PUID) ]; then
echo "User with uid [$PUID] does not exist, creating..."
useradd -g $PGID -u $PUID -M $USER_NAME
echo "User [$USER_NAME] with uid [$PUID] created."
else
echo "user $USER_NAME already exists."
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
echo "user with uid [$PUID] name [$USER_NAME] already exists."
HOME_DIR="/home/$USER_NAME"
fi

if [ -z "${AUDIO_GID}" ]; then
echo "AUDIO_GID is mandatory for user mode and alsa output"
exit 3
### create home directory
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
echo ". done."
fi
if [ $(getent group $AUDIO_GID) ]; then
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
chown -R $PUID:$PGID $HOME_DIR
if [ -z "${AUDIO_GID}" ]; then
echo "WARNING: AUDIO_GID is mandatory for user mode and alsa output"
else
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
groupadd -g $AUDIO_GID mpd-audio
if [ $(getent group $AUDIO_GID) ]; then
echo "Alsa Mode - Group with gid $AUDIO_GID already exists"
else
echo "Alsa Mode - Creating group with gid $AUDIO_GID"
groupadd -g $AUDIO_GID sq-audio
fi
echo "Alsa Mode - Adding $USER_NAME [$PUID] to gid [$AUDIO_GID]"
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
usermod -a -G $AUDIO_GRP $USER_NAME
echo "Alsa Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";
fi
echo "Alsa Mode - Adding $USER_NAME to gid $AUDIO_GID"
AUDIO_GRP=$(getent group $AUDIO_GID | cut -d: -f1)
echo "gid $AUDIO_GID -> group $AUDIO_GRP"
usermod -a -G $AUDIO_GRP $USER_NAME
echo "Alsa Mode - Successfully created $USER_NAME (group: $GROUP_NAME)";
else
echo "User mode disabled"
fi
Expand Down
61 changes: 33 additions & 28 deletions app/bin/run-squeezelite-pulse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,43 @@ if [ -z "${PGID}" ]; then
echo "Setting default value for PGID: ["$PGID"]"
fi

USER_NAME=sq-pulse
GROUP_NAME=sq-pulse

HOME_DIR=/home/$USER_NAME

### create home directory and ancillary directories
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
chown -R $PUID:$PGID $HOME_DIR
ls -la $HOME_DIR -d
ls -la $HOME_DIR
fi

### create group
if [ ! $(getent group $GROUP_NAME) ]; then
echo "group $GROUP_NAME does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
DEFAULT_USER_NAME=sq-pulse
DEFAULT_GROUP_NAME=sq-pulse
DEFAULT_HOME_DIR=/home/$DEFAULT_USER_NAME

USER_NAME=$DEFAULT_USER_NAME
GROUP_NAME=$DEFAULT_GROUP_NAME
HOME_DIR=$DEFAULT_HOME_DIR

echo "Ensuring user with uid:[$PUID] gid:[$PGID] exists ...";
### create group if it does not exist
if [ ! $(getent group $PGID) ]; then
echo "Group with gid [$PGID] does not exist, creating..."
groupadd -g $PGID $GROUP_NAME
echo "Group [$GROUP_NAME] with gid [$PGID] created."
else
echo "group $GROUP_NAME already exists."
GROUP_NAME=$(getent group $PGID | cut -d: -f1)
echo "Group with gid [$PGID] name [$GROUP_NAME] already exists."
fi

### create user
if [ ! $(getent passwd $USER_NAME) ]; then
echo "user $USER_NAME does not exist, creating..."
useradd -g $PGID -u $PUID -s /bin/bash -M -d $HOME_DIR $USER_NAME
usermod -a -G audio $USER_NAME
id $USER_NAME
echo "user $USER_NAME created."
### create user if it does not exist
if [ ! $(getent passwd $PUID) ]; then
echo "User with uid [$PUID] does not exist, creating..."
useradd -g $PGID -u $PUID -M $USER_NAME
echo "User [$USER_NAME] with uid [$PUID] created."
else
echo "user $USER_NAME already exists."
USER_NAME=$(getent passwd $PUID | cut -d: -f1)
echo "user with uid [$PUID] name [$USER_NAME] already exists."
HOME_DIR="/home/$USER_NAME"
fi
### create home directory
if [ ! -d "$HOME_DIR" ]; then
echo "Home directory [$HOME_DIR] not found, creating."
mkdir -p $HOME_DIR
echo ". done."
fi
chown -R $PUID:$PGID $HOME_DIR

echo "Pulse Mode - Successfully created user $USER_NAME:$GROUP_NAME [$PUID:$PGID])";

PULSE_CLIENT_CONF="/etc/pulse/client.conf"

Expand Down
1 change: 1 addition & 0 deletions doc/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Older build might be dropped in order to save space on docker-hub and incur in l

Date|Type|Description
:---|:---|:---
2023-07-25|Maintenance|Allow existing docker user, see [#223](https://github.com/GioF71/squeezelite-docker/issues/223)
2023-07-25|Maintenance|Drop `sid` and `trixie`, see [#224](https://github.com/GioF71/squeezelite-docker/issues/224)
2023-07-25|Maintenance|Remove apt proxy config, see [#221](https://github.com/GioF71/squeezelite-docker/issues/221)
2023-07-24|Maintenance|Avoid deprecated features in workflows, see [#219](https://github.com/GioF71/squeezelite-docker/issues/219)
Expand Down

0 comments on commit e95c129

Please sign in to comment.